Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

SQL Cursor

DECLARE @CONTACTID INT 

--will need to declare at least one variable

--in this case, to store values when iterating through cursor

DECLARE SIMPLE_CURSOR CURSOR FOR 
SELECT ID
FROM CONTACT

OPEN SIMPLE_CURSOR

FETCH NEXT FROM SIMPLE_CURSOR  --Start the cursor
INTO @CONTACTID

WHILE @@FETCH_STATUS = 0  --while there is a loaded record, keep processing
BEGIN

--do whatever you need to do
print ('This is where the magic happens! 
Do whatever you need to do (update/insert/delete/stored proc/etc.')

FETCH NEXT FROM SIMPLE_CURSOR INTO @CONTACTID  --fetch next record
END

CLOSE SIMPLE_CURSOR   --close and deallocate
DEALLOCATE SIMPLE_CURSOR
Source by www.sqlservertutorial.net #
 
PREVIOUS NEXT
Tagged: #SQL #Cursor
ADD COMMENT
Topic
Name
3+9 =