title: How to replace cursor for set-based approach?
body: <p>What would be a more efficient way to replace a RABR situation into a set-based one, where there is a procedure that receives two parameters, a code, and a date, and the result is inserted into a temp table.</p>
  
  <pre><code>CREATE TABLE #RESULTADO(CODE INT, DATA DATETIME);
  DECLARE @CODE
  SELECT @CODE = MIN(CODIGO)
  FROM TABELACOMDADOS
  WHERE CODE &lt;&gt; ''
  AND CODE IS NOT NULL
  
  WHILE @CODE IS NOT NULL
  BEGIN
      INSERT INTO #RESULTADO
      EXECUTE PR_PROCESSACALCULO @CODE, @DATA;
      -- NEXT VALUE FOR THE LOOP
      SELECT @CODE = MIN(CODIGO)
      FROM TABELACOMDADOS
      WHERE CODE &gt; @CODE
      ORDER BY CODE;
  END;
  </code></pre>
  
  <p>I have tried to replace the current cursor with this while loop, but the total duration is 2 minutes minus only.</p>