title: Use "WHERE CURRENT OF" clause to update only the specific row on which the CURSOR is positioned in SQL
body: <p>I have the following cursor in SQL:</p>
  
  <pre><code>DECLARE @Script varchar(max)
  
  DECLARE @getScript CURSOR
  
  SET @getScript = CURSOR FOR
  
  SELECT [Script]
  FROM ScriptTable
  
  OPEN @getScript
  FETCH NEXT
  FROM @getScript INTO @Script
  
  WHILE @@FETCH_STATUS = 0
  BEGIN
      BEGIN TRY
          EXEC(@Script) --Working part.  This executes the query stored in the Script column.
                        --For example INSERT INTO zTest VALUES(VAL1, VAL2, etc etc..)
  
          UPDATE ScriptTable
          SET DateDone = GETDATE(), IsDone = 1, Err = NULL
          FETCH NEXT
          FROM @getScript INTO @Script 
      END TRY
      BEGIN CATCH
          DECLARE @Err varchar(max)
          SET @Err = ERROR_MESSAGE()
  
          UPDATE ScriptTable
          SET DateDone = GETDATE(), Err = @Err
  
      END CATCH
  END
  
  CLOSE @getScript
  DEALLOCATE @getScript
  </code></pre>
  
  <p><strong>Q1:</strong>
  Currently, I am getting the values inserted into the "zTest" table specified in my comments when I execute EXEC(@Script).  </p>
  
  <p>However, the second part where the "Update ScriptTable" is, updates all the rows in my Script Table.  I know I need to specify the ID for the appropriate row that the cursor is moving through.  Question is, how can I do that?  I wan't to only update the appropriate row, move to the next then update that one.</p>
  
  <p><strong>Q2:</strong>
  My next question is, in the CATCH block, I think I am creating an infinite loop as soon as there is an error in one of the queries in the Script Column of the ScriptTable as when I look at results, it just keeps going and going.  I don't want to BREAK; the procedure as I want to write an error to the Err column and continue with the next rows till it reaches the end of @Script, then stop.</p>
  
  <p>IDENT_CURRENT, Scope_Identity etc doesn't work because I haven't inserted anything into the Scripts Table.</p>
  
  <p>Please help. </p>