T-SQL Cursor
A cursor is a kind of foreach loop in SQL.
DECLARE @NEW_VALUE nvarchar(200)
DECLARE @CONTRACT_NUMBER nvarchar(100)
DECLARE CONTRACT_CURSOR CURSOR LOCAL FAST_FORWARD FOR
/* Fetch candidates that will be updated */
SELECT ac.CONTRACT_NUMBER, ac.NEW_VALUE FROM CONTRACT_TABLE ac
OPEN CONTRACT_CURSOR
FETCH NEXT FROM CONTRACT_CURSOR INTO @CONTRACT_NUMBER, @NEW_VALUE
WHILE @@FETCH_STATUS = 0 BEGIN
/**************************************** Begin cursor ****************************************/
PRINT 'Updating contract ' + @CONTRACT_NUMBER update ais set ais.UPDATE_FIELD = @NEW_VALUE from DEMO.CONTRACTS ais where ais.CONTRACT_NUMBER = @CONTRACT_NUMBER
/**************************************** End cursor ****************************************/
FETCH NEXT FROM CONTRACT_CURSOR INTO @CONTRACT_NUMBER, @NEW_VALUE END
CLOSE CONTRACT_CURSOR; DEALLOCATE CONTRACT_CURSOR;
- Previous: Docker Retrieve IP of the container
- Next: YAML multiline syntax