Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 cursor

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-02-05 : 10:43:49
I am using two cursors (cursor1, cursor2). Cursor2 is within cursor1 in my sql operations.
While within cursor2, how can I exit cursor2 and continue with cursor1?
Thank you

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-05 : 10:46:58
can i ask for what purpose you need two cursors?
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-02-05 : 11:04:28
the direct answer to your question is answered by this:

WHILE 1 = 1 BEGIN

PRINT 'foo'
BREAK

END

However, if you tell us what you are doing with your nested cursors then we'll likely be able to show you a much better way of doing things.




Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-02-05 : 11:05:10
The existing stored procedure loops through a set of records by cursor1 to get several values of a field, then these values are used in the second cursor.
This is an existing stored procedure which the business is using...
Thanks
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-02-05 : 11:07:21
Assuming cursor2 is the inner cursor, don't do the FETCH on Cursor2 and let it continue to Cursor1's FETCH.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-02-05 : 11:09:43
you may also want to close and deallocate cursor2 if its build using the variables et for cursor 1.




Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-02-05 : 11:37:51
quote:
Originally posted by Transact Charlie

you may also want to close and deallocate cursor2 if its build using the variables et for cursor 1.
Yes, absolutly. I'd say it is a MUST to deallocate the inner cursor if it is declared inside the outer one.
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-02-05 : 14:28:16
so doiing a break within cursor 2 means that it jumps out of cursor2 and continues with cursor1 ?
Go to Top of Page
   

- Advertisement -