Skip to content

Instantly share code, notes, and snippets.

@marcelodeandrade
Created September 28, 2017 13:00
Show Gist options
  • Save marcelodeandrade/334fe0dfc8cddcfaff6aaaf10824bd8e to your computer and use it in GitHub Desktop.
Save marcelodeandrade/334fe0dfc8cddcfaff6aaaf10824bd8e to your computer and use it in GitHub Desktop.
Example for use a CURSOR in SQL SERVER
DECLARE
@variable1 INT,
@variable2 INT
-- Create a new cursor
DECLARE cursor_example CURSOR FOR
SELECT field1, field2 FROM [table]
-- Open a cursor
OPEN cursor_example
-- Read the fields selected and insert into variables previous declared
FETCH NEXT FROM cursor_example INTO @variable1, @variable2
-- Your code here
-- While result
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM cursor_example INTO @variable1, @variable2
END
-- Close cursor
CLOSE cursor_example
-- Destroy cursor
DEALLOCATE cursor_example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment