Skip to content

Instantly share code, notes, and snippets.

@Sebfh
Created March 9, 2012 12:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sebfh/2006275 to your computer and use it in GitHub Desktop.
Save Sebfh/2006275 to your computer and use it in GitHub Desktop.
SQL: Cursor
declare @message nvarchar(250)
DECLARE #cursor_name# CURSOR FOR
#select_statement#
OPEN #cursor_name#;
FETCH NEXT FROM #cursor_name#
INTO @#variable#;
WHILE @@FETCH_STATUS = 0
BEGIN
select @message = '#execute_statement#'
execute sp_executesql @message;
FETCH NEXT FROM #cursor_name#
INTO @#variable#;
END
CLOSE #cursor_name#;
DEALLOCATE #cursor_name#;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment