Click to See Complete Forum and Search --> : [RESOLVED] For Loop Cursor


NinaWilliam
06-03-2008, 07:58 AM
I am trying to write a for loop cursor, and this is my code


DECLARE
slip_Update CURSOR FOR
SELECT * FROM SlipsTEST, SlipdetailsTEST
WHERE SlipsTEST.SlipIdOld = SlipdetailsTEST.SlipIdOld
FOR UPDATE;
BEGIN
OPEN slip_Update;
FOR slip_Update IN slip_Update
LOOP
UPDATE SlipdetailsTEST
SET SlipdetailsTEST.SNO = SlipsTEST.SNO
WHERE CURRENT OF slip_Update;
END LOOP;
CLOSE slip_Update;
END;



Can any one tell me what is wrong with my cursor? I run it in SQL Query Analyzer and it always shows this message:


Server: Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'FOR'.
Server: Msg 170, Level 15, State 1, Line 14
Line 14: Incorrect syntax near 'LOOP'.
Server: Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'END'.

russell
06-03-2008, 06:42 PM
wow, that syntax isn't even close. (sorry, not trying to be a wise guy). if you are using query analyzer, hit the TEMPLATES tab and look at CURSOR. there is a cursor template that you can copy/paste and then edit for you own purposes.

also look at this (http://msdn.microsoft.com/en-us/library/ms180169.aspx)

NinaWilliam
06-04-2008, 06:18 AM
Thank you russell, you showed me something great ^_^

I appreciate you help

mattyblah
06-06-2008, 12:44 AM
can you post what you are trying to do? cursors are terribly inefficient and i can only name only a few situations where they would actually be necessary.

NinaWilliam
07-07-2008, 12:03 AM
I used query analyzer to get cursor template as russell told me and modify it to meet my needs and it worked :)