FETCH
moves the cursor and returns one row
What changes
- Input
- the cursor's current position
- Output or change
- variables (INTO) or a one-row result set; @@FETCH_STATUS
Syntax at a glance
FETCH [ [ NEXT | PRIOR | FIRST | LAST | ABSOLUTE { n | @nvar } | RELATIVE { n | @nvar } ] FROM ] { { [ GLOBAL ] cursor_name } | @cursor_variable_name } [ INTO @variable_name [ ,...n ] ]
Work example
A cursor loop needs the next row and a clear end-of-data condition.
Check @@FETCH_STATUS immediately after each FETCH. Do not perform row work after the fetch reports no row.
Check your work FETCH. Every fetched row is handled once and the loop exits when @@FETCH_STATUS is not zero.
Open Microsoft Learn for full syntax, permissions, and details for your version.