Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Term 'Cursor'

Status
Not open for further replies.

Skittle

ISP
Sep 10, 2002
1,528
US
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
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'.
 
I don't know how you think this is not referring to the exact meaning of a cursor. Yes, this is talking about cursors.
You won't want a per row of a cursor parameterized query but a join instead, as the means one (hopefully/normally well optimizable) query instead of N (also normally optimizable but less good optimizable) queries.

Bye, Olaf.
 
CURSOR is an acronym for CURrent Set Of Rows. It's the set of data returned by a SQL query. That said, the word cursor is usually applied to a set of data returned by a query that is accessed by the application one row at a time (FETCH records). Each database has extensions to the ANSI-SQL language to handle cursors. Oracle=PL/SQL, Sybase and Microsoft = Transact-SQL, etc.

==================================
adaptive uber info galaxies (bigger, better, faster, and more adept than agile innovative cognitive big data clouds)


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top