Generally, when we use SELECT query statements, they are all for one row of records.
If you want to read multiple rows of records (i.e., recordset) in the query analyzer, you need to use a cursor Or WHILE and other loops
Types of cursors:
1. Static cursor (does not detect changes in data rows)
2. Dynamic cursor (reflects changes in all data rows)
3. Forward-only cursor (does not support scrolling)
4. Keyset cursor (can reflect modifications, but cannot accurately reflect insertions and deletions)
The order of cursor use:
1. Define the cursor
2. Open the cursor
3. Use the cursor
4. Close the cursor
5. Release the cursor
Transact-SQL:
declare cursor name cursor [LOCAL | GLOBAL][FORWARD_ONLY | SCROLL][STATIC | KEYSET | DYNAMIC] [READ_ONLY | SCROLL_LOCKS]
for selet statement [for update[of column name[, column name]]
Note: LOCAL local cursor GLOBAL global cursor
FORWARD_ONLY forward only SCROLL scroll
STATIC static KEYSET key set DYNAMIC dynamic
READ_ONLY read-only SCROLL_LOCKS lock the current row of the cursor
get the cursor data
FETCH PRI [[NEXT | OR | FIRST | LAST |
ABSOLUTE{ n | @nvar | RELATIVE { n | @nvar}]
From ] Cursor name [into variable]
Note:
NEXT Next line PRIOR Previous line FIRST First line
LAST Last line ABSOLUTE n Line n
RELATIVE n The nth row starting from the current position
into variable Assign each field value of the current row to the variable
Cursor status variable:
@@fetch_status Cursor status
Number of rows
N Number of rows - 1 The cursor is dynamic 0 Empty set cursor
Current row of the cursor:
current of Cursor name