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 2008 Forums
 Transact-SQL (2008)
 Nested cusor

Author  Topic 

Swati Jain
Posting Yak Master

139 Posts

Posted - 2009-07-24 : 11:36:12

----outer loop query gives output as 3 Rows

--inner loop query gives

for (outer) Row1 -- 1 Row(inner)

for (outer) Row2 -- 3 Row(inner)


for (outer) Row3 -- 1 Row(inner)

but still loop is executing more than times output of the inner rows
means
for (outer) Row1 -- 1 Row(inner) --Executing twice

for (outer) Row2 -- 3 Row(inner) --Executing 4 times



for (outer) Row3 -- 1 Row(inner) --Executing twice


whats going wrong

with following cursor

Declare cCurLinkNew Cursor For
Select distinct linkidentifierid from rs_identifier where pageTypeid=@PageTypeID and recordid=@RecordID



OPEN cCurLinkNew

FETCH NEXT FROM cCurLinkNew INTO @varlinkidentifierid

PRINT 'OUTER LOOP START'



WHILE (@@FETCH_STATUS = 0) -- sql cursor fetch_status

BEGIN

set @temp=0

----cursor to set continuous with page no----

Declare cCurLinkGroupNew Cursor For
select serialNo,identifierid from RS_Identifier where linkidentifierid=@varlinkidentifierid group by IdentifierID,serialNo



OPEN cCurLinkGroupNew
WHILE (@@FETCH_STATUS = 0)
BEGIN
FETCH NEXT FROM cCurLinkGroupNew INTO @varSerialNo,@varidentifierID

PRINT 'INNER LOOP START'



-- WHILE (@@FETCH_STATUS = 0)



print 'swati'
print @temp
print @varidentifierID
if(@PageTypeID=28)
begin
Update RS_BillingSummary
set ContinuousWP=@temp
where IdentifierID=@varidentifierID
end

if(@PageTypeID=27)
begin
Update RS_VisitSummary
set Continwithpage=@temp
where IdentifierID=@varidentifierID
end

if(@PageTypeID<>27)
begin
if(@PageTypeID<>28)
begin
Update RS_Identifier
set ContinuationWithPg=@temp
where IdentifierID=@varidentifierID

end
end




set @temp=@varSerialNo

PRINT 'INNER LOOP'

end-- inner while



CLOSE cCurLinkGroupNew

DEALLOCATE cCurLinkGroupNew






FETCH NEXT FROM cCurLinkNew INTO @varlinkidentifierid

PRINT 'OUTER LOOP'

End -- outer while

CLOSE cCurLinkNew

DEALLOCATE cCurLinkNew

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-07-24 : 12:03:45
Take that fetch (inner)
FETCH NEXT FROM cCurLinkGroupNew INTO @varSerialNo,@varidentifierID

one time before the inner WHILE-statement
and one time before the inner END-WHILE

like you doing already in the outer loop


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Swati Jain
Posting Yak Master

139 Posts

Posted - 2009-07-25 : 04:54:20
quote:
Originally posted by webfred

Take that fetch (inner)
FETCH NEXT FROM cCurLinkGroupNew INTO @varSerialNo,@varidentifierID

one time before the inner WHILE-statement
and one time before the inner END-WHILE

like you doing already in the outer loop


No, you're never too old to Yak'n'Roll if you're too young to die.



hi T O P I C R E V I E W
Swati Jain Posted - 07/24/2009 : 11:36:12
--------------------------------------------------------------------------------

----outer loop query gives output as 3 Rows

--inner loop query gives

for (outer) Row1 -- 1 Row(inner)

for (outer) Row2 -- 3 Row(inner)


for (outer) Row3 -- 1 Row(inner)

but still loop is executing more than times output of the inner rows
means
for (outer) Row1 -- 1 Row(inner) --Executing twice

for (outer) Row2 -- 3 Row(inner) --Executing 4 times



for (outer) Row3 -- 1 Row(inner) --Executing twice


whats going wrong

with following cursor

Declare cCurLinkNew Cursor For
Select distinct linkidentifierid from rs_identifier where pageTypeid=@PageTypeID and recordid=@RecordID



OPEN cCurLinkNew

FETCH NEXT FROM cCurLinkNew INTO @varlinkidentifierid

PRINT 'OUTER LOOP START'



WHILE (@@FETCH_STATUS = 0) -- sql cursor fetch_status

BEGIN

set @temp=0

----cursor to set continuous with page no----

Declare cCurLinkGroupNew Cursor For
select serialNo,identifierid from RS_Identifier where linkidentifierid=@varlinkidentifierid group by IdentifierID,serialNo



OPEN cCurLinkGroupNew
WHILE (@@FETCH_STATUS = 0)
BEGIN
FETCH NEXT FROM cCurLinkGroupNew INTO @varSerialNo,@varidentifierID

PRINT 'INNER LOOP START'



-- WHILE (@@FETCH_STATUS = 0)



print 'swati'
print @temp
print @varidentifierID
if(@PageTypeID=28)
begin
Update RS_BillingSummary
set ContinuousWP=@temp
where IdentifierID=@varidentifierID
end

if(@PageTypeID=27)
begin
Update RS_VisitSummary
set Continwithpage=@temp
where IdentifierID=@varidentifierID
end

if(@PageTypeID<>27)
begin
if(@PageTypeID<>28)
begin
Update RS_Identifier
set ContinuationWithPg=@temp
where IdentifierID=@varidentifierID

end
end




set @temp=@varSerialNo

PRINT 'INNER LOOP'

end-- inner while



CLOSE cCurLinkGroupNew

DEALLOCATE cCurLinkGroupNew






FETCH NEXT FROM cCurLinkNew INTO @varlinkidentifierid

PRINT 'OUTER LOOP'

End -- outer while

CLOSE cCurLinkNew

DEALLOCATE cCurLinkNew
1 L A T E S T R E P L I E S (Newest First)

Hi webfred ,

thanks for replying , got the exact mistake


Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-07-25 : 05:08:12
You havn't done what I said.
Do it like this:

Declare cCurLinkNew Cursor For
Select distinct linkidentifierid from rs_identifier where pageTypeid=@PageTypeID and recordid=@RecordID

OPEN cCurLinkNew

FETCH NEXT FROM cCurLinkNew INTO @varlinkidentifierid

PRINT 'OUTER LOOP START'

WHILE (@@FETCH_STATUS = 0) -- sql cursor fetch_status
BEGIN
set @temp=0

----cursor to set continuous with page no----
Declare cCurLinkGroupNew Cursor For
select serialNo,identifierid from RS_Identifier where linkidentifierid=@varlinkidentifierid group by IdentifierID,serialNo
OPEN cCurLinkGroupNew

FETCH NEXT FROM cCurLinkGroupNew INTO @varSerialNo,@varidentifierID

WHILE (@@FETCH_STATUS = 0)
BEGIN


PRINT 'INNER LOOP START'
print 'swati'
print @temp
print @varidentifierID

if(@PageTypeID=28)
begin
Update RS_BillingSummary
set ContinuousWP=@temp
where IdentifierID=@varidentifierID
end

if(@PageTypeID=27)
begin
Update RS_VisitSummary
set Continwithpage=@temp
where IdentifierID=@varidentifierID
end

if(@PageTypeID<>27)
begin
if(@PageTypeID<>28)
begin
Update RS_Identifier
set ContinuationWithPg=@temp
where IdentifierID=@varidentifierID
end
end

set @temp=@varSerialNo
PRINT 'INNER LOOP'

FETCH NEXT FROM cCurLinkGroupNew INTO @varSerialNo,@varidentifierID

end-- inner while

CLOSE cCurLinkGroupNew
DEALLOCATE cCurLinkGroupNew

FETCH NEXT FROM cCurLinkNew INTO @varlinkidentifierid

PRINT 'OUTER LOOP'

End -- outer while

CLOSE cCurLinkNew
DEALLOCATE cCurLinkNew


I havn't had a look what your code is doing so the next poster may come up with a solution that don't need a cursor!

Greetings
Fred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2009-07-27 : 13:50:44
Keep in mind that since you are not using an order by in your cursor declaration the following code will get random results.

set @temp=@varSerialNo



It's very unlikely that you need a cursor.





An infinite universe is the ultimate cartesian product.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-27 : 15:23:57
Why aren't you using 3 single UPDATE statements instead?



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -