Created
September 28, 2017 13:00
-
-
Save marcelodeandrade/334fe0dfc8cddcfaff6aaaf10824bd8e to your computer and use it in GitHub Desktop.
Example for use a CURSOR in SQL SERVER
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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