title: How to optimize this query in SQL Server
body: <p>I have the following query:</p>
  
  <pre><code>DECLARE @codeItem VARCHAR(100);
  DECLARE @Price DECIMAL(18, 5);
  DECLARE @EndDate DATE;
  DECLARE @StartDate DATE;
  
  DECLARE Cur CURSOR FOR
      SELECT *
      FROM myTable
  
  OPEN Cur
  
  FETCH NEXT FROM Cur INTO @codeItem, @Price, @EndDate, @StartDate
  
  WHILE @@fetch_status = 0 BEGIN
  
      EXEC sp_Proc @codeItem, @Price, @EndDate, @StartDate
  
      FETCH NEXT FROM Cur INTO @codeItem, @Price, @EndDate, @StartDate
  END
  
  CLOSE Cur
  DEALLOCATE Cur
  </code></pre>
  
  <p>Stored procedure <code>sp_Proc</code>:</p>
  
  <pre><code>UPDATE SRC
  SET enddate = DATEADD(DAY, -1, @StartDate)
  FROM test SRC
  WHERE codeItem = @codeItem
      AND (@StartDate BETWEEN startdate AND enddate)
  
  INSERT INTO test (codeItem, price, startdate, enddate)
  VALUES (@codeItem, @price, @StartDate, @EndDate)
  </code></pre>
  
  <p>How can I optimize the queries?</p>
  
  <p>Delete the cursor?</p>
  
  <p>How can I make it faster?</p>