I would like to clarify in my own mind what a 'cursor' is within SQL.
Whenever I hear the phrase I associate it with a row by row scripted loop through a table to do some fancy
row by row work. It's effectively a DO WHILE loop to read all rows one at a time and do something to it within the loop. I have an old example I made a note of years ago below.
However I often see the phrase ‘cursor’ used in more conceptual descriptions that do not relate to a hard coded loop.
For example:-
In a book description covering CROSS APPLY, I have the text ‘You don’t want to use a cursor to iterate through the suppliers table one at a time and invoke a separate query for each’.
Question
Am I correct that in the context of the above text a ‘cursor’ be considered any statement that reads through all rows in a select statement?
Context
Dazed and confused.
Remember.. 'Depression is just anger without enthusiasum'.
Whenever I hear the phrase I associate it with a row by row scripted loop through a table to do some fancy
row by row work. It's effectively a DO WHILE loop to read all rows one at a time and do something to it within the loop. I have an old example I made a note of years ago below.
However I often see the phrase ‘cursor’ used in more conceptual descriptions that do not relate to a hard coded loop.
For example:-
In a book description covering CROSS APPLY, I have the text ‘You don’t want to use a cursor to iterate through the suppliers table one at a time and invoke a separate query for each’.
Question
Am I correct that in the context of the above text a ‘cursor’ be considered any statement that reads through all rows in a select statement?
Context
Code:
--
--We Declare The Cursor To Equal A SQL Statement
DECLARE MyCursor CURSOR
SCROLL
DYNAMIC
FOR
SELECT OHORDN, OHSALA, OHCUST, OHCOMP FROM FGBOIH
WHERE OHSALA = ‘A1’
--
--Next We Declare The Local Variables To Hold The ‘Record Format’
DECLARE @Order Char(6),
@SalesArea Char(2),
@Customer Char(6),
@Company Char(2)
--
--We Now Open The Cursor ( Lock of selection begins )
OPEN MyCursor
Read Ahead ( no and type of vars must match SELECT )
FETCH NEXT FROM MyCursor INTO @Order,
@SalesArea,
@Customer,
@Company
--
--Loop Through Cursor Until Fetch Status Indicates Failure
WHILE @@FETCH_STATUS = 0 BEGIN
--Read Next
FETCH NEXT FROM MyCursor INTO @Order,
@SalesArea,
@Customer,
@Company
END
--
--We Close The Cursor ( Locks of selection end )
CLOSE MyCursor
Dazed and confused.
Remember.. 'Depression is just anger without enthusiasum'.