SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Droping out of a cusor


Droping out of a cusor

Author
Message
Edward-445599
Edward-445599
Old Hand
Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

Group: General Forum Members
Points: 311 Visits: 532
Hi what’s the best to way to drop out of a cursor if a condition is meet?

Many thanks
Lowell
Lowell
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27990 Visits: 39923
You are looking for the BREAK keyword.
a simple example to break out of what would otherwise be an endless loop:

--RAISERROR immediate message
--print error immediately in batch
DECLARE
@i INT,
@err VARCHAR(100)
--set @i=1
WHILE 0 = 0
BEGIN
SET @err = 'Progress So Far: Step ' + CONVERT(VARCHAR(30), ISNULL(@i, 1)) + ' completed.'
RAISERROR (@err,
0,
1) WITH nowait
WAITFOR delay '00:00:02'
SET @i=ISNULL(@i, 1) + 1
IF @i > 5
BREAK;
END
PRINT 'We Broke out of the loop!'




Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Edward-445599
Edward-445599
Old Hand
Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

Group: General Forum Members
Points: 311 Visits: 532
Thats great thank you
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25869 Visits: 17509
Edward-445599 (5/1/2013)
Hi what’s the best to way to drop out of a cursor if a condition is meet?

Many thanks


Are you sure you even need a cursor? Given that you want to break out of one I am guessing you are doing some sort of RBAR processing. Maybe the best way out of your cursor is to not have one at all.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
TheSQLGuru
TheSQLGuru
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12280 Visits: 8542
Sean Lange (5/1/2013)
Edward-445599 (5/1/2013)
Hi what’s the best to way to drop out of a cursor if a condition is meet?

Many thanks


Are you sure you even need a cursor? Given that you want to break out of one I am guessing you are doing some sort of RBAR processing. Maybe the best way out of your cursor is to not have one at all.



+100

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search