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


how to exit from a cursor


how to exit from a cursor

Author
Message
DURGESH-769360
DURGESH-769360
SSC-Enthusiastic
SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)

Group: General Forum Members
Points: 144 Visits: 319
hi all,
i have used a cursor. i want to stop execution of remaining rounds of cursour when a particular condition fails. To do this I had use
close cur_sd
deallocate cur_sd

but it is giving the error as
A cursor with the name 'cur_sd' does not exist.

can anybody help me to solve this issue

thanks in advance

regards
Durgesh J
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87765 Visits: 45272
Post the code please?

My guess is you're referencing the cursor after it's deallocated.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


karthik M
karthik M
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2949 Visits: 2584
You can use BREAK statement. But as Gail told, it should be useful to you, if you post the code.

But i did some test to come out from the cursor. Here is the code

create table test
(
col1 varchar(10),
col2 varchar(10)
)
go
insert into test
select 'aa','ram'
union all
select 'bb','ram'
union all
select 'cc','ravi'
union all
select 'dd','rahi'
go

create proc p1
as
begin

declare @col1 varchar(10)

declare c1 cursor
for select col1 from test
for read only

open c1

fetch c1 into @col1

while @@fetch_status = 0

begin

if @col1 = 'cc'
break
select @col1

fetch c1 into @col1
end

close c1
deallocate c1
end

go

exec p1

karthik
J-440512
J-440512
Right there with Babe
Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)

Group: General Forum Members
Points: 727 Visits: 949
Simple. Add a loop continuation condition in your WHILE statement.

DECLARE @lb_Exit  bit
SET @lb_Exit = 0

WHILE (@@FETCH_STATUS = 0) AND (@lb_Exit = 0) BEGIN

IF your_condition SET @lb_Exit = 1

END



In fact, should @@ERROR become something else than 0 within the loop, I also use this method to exit the loop.
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