473,505 Members | 14,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to break out of a cursor and out of a SP all together

Hi

I am trying to figure out how to exit a cursor loop if a specified
condition occurs.

I have a select count(*) on a table like this

select lagplats, count(*) from arsi where artnr = '1440'

if that count(*) is > 1 I want to break that cursor loop and fetch the
next row into the cursor and continue.

however if that count(*) > 5 I need to exit the entire procedure and
not try and fetch the next row into the cursor

I find this hard to explain and the code is long and complicated so I
hope it is possible to understand what I am after

rgds

Matt
Jul 20 '05 #1
3 20994
Not the answer you expect maybe, but why are you writing a cursor at all?
Your code already sounds very complex. Are you sure there isn't a set-based
solution? Cursors should be a last resort so if you need help with a
non-cursor solution please post DDL, some sample data as INSERT statements
and show your required result.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
Your loop logic could be something like this. As I suggested before, this
sort of thing can usually be replaced by more efficient set-based
SELECT/INSERT/UPDATE/DELETE statements.

WHILE @@FETCH_STATUS = 0
BEGIN
...
DECLARE @cnt INTEGER

SET @cnt =
(SELECT COUNT(*)
FROM arsi
WHERE artnr = '1440')

IF @cnt > 5
RETURN

IF ()<=1
BEGIN
...
END

FETCH NEXT...

END
--
David Portas
SQL Server MVP
--
Jul 20 '05 #3
Matt (ma**@fruitsalad.org) writes:
I am trying to figure out how to exit a cursor loop if a specified
condition occurs.

I have a select count(*) on a table like this

select lagplats, count(*) from arsi where artnr = '1440'

if that count(*) is > 1 I want to break that cursor loop and fetch the
next row into the cursor and continue.

however if that count(*) > 5 I need to exit the entire procedure and
not try and fetch the next row into the cursor

I find this hard to explain and the code is long and complicated so I
hope it is possible to understand what I am after


To exit a loop, you can use BREAK. You can also use CONTINUE to
immediately start from the beginning of the loop.

But you should probably not use a statement like the above in a loop,
since it produces a result set. (Also the statement you posted does
not compile, since there is no GROUP BY for the lagplats column.)

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2215
by: Philip Mette | last post by:
Does anyone have any good references they could recommend on Cursor based SQL writing? I have to create SQL that can loop though records simular to VB loops and I have been told that this is the...
15
3802
by: Philip Mette | last post by:
I am begginner at best so I hope someone that is better can help. I have a stored procedure that updates a view that I wrote using 2 cursors.(Kind of a Inner Loop) I wrote it this way Because I...
2
11589
by: Anshul Seth | last post by:
I have been dealing with the page-break problem for long, and have not been able to come to a solution. I am using XSL:FO to generate PDF report for my XML, generating it in tables. I want that...
2
4565
by: Csaba2000 | last post by:
The following code has me so confused, I don't even know the right questions to ask. Opera 7.01 and IE 5.5 both exhibit behaviours I don't understand while NN 6.1 seems to ignore me altogether. ...
7
7248
by: Michael Beumers | last post by:
Hello NG I've defined a cursor like the following in my COBOL Programm: DECLARE testc CURSOR FOR SELECT ... FROM ... WHERE field1 LIKE :hostvariable1 field2 LIKE :hostvariable2
1
6840
by: pemigh | last post by:
I have a report with pages breaking in the middle of text box controls. And it's not just on a line-by-line basis, but sometimes (in a situation that I can no longer recreate) just the bottom...
2
1392
by: Fu Chen | last post by:
Hi! I have really weird break point. Look at my screen shoot http://www.mapsea.com/vs.jpg First break is normal and can stop when program run to that position. Second break show with a question...
7
1881
by: Abraham Luna | last post by:
how do i stop the dynamic validators from breaking explorer if i use a dynamic validator and move to a different control it breaks explorer and i can type in the page when i'm not supposed to....
0
1056
by: Clanguage | last post by:
Hello, does anyone knows of a way to group a set of records everytime a break in a sequence occurs. Let me explain. I have a sequence of numbers like 1,2,3,4,5,7,8,9 when I reach the break (6) I...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.