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.
| 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 cursorDeclare cCurLinkNew Cursor ForSelect distinct linkidentifierid from rs_identifier where pageTypeid=@PageTypeID and recordid=@RecordID OPEN cCurLinkNewFETCH NEXT FROM cCurLinkNew INTO @varlinkidentifieridPRINT 'OUTER LOOP START' WHILE (@@FETCH_STATUS = 0) -- sql cursor fetch_statusBEGIN 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 cCurLinkGroupNewWHILE (@@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 whileCLOSE cCurLinkNewDEALLOCATE 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,@varidentifierIDone time before the inner WHILE-statementand one time before the inner END-WHILElike you doing already in the outer loop No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
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,@varidentifierIDone time before the inner WHILE-statementand one time before the inner END-WHILElike 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 timesfor (outer) Row3 -- 1 Row(inner) --Executing twice whats going wrong with following cursorDeclare cCurLinkNew Cursor ForSelect distinct linkidentifierid from rs_identifier where pageTypeid=@PageTypeID and recordid=@RecordID OPEN cCurLinkNewFETCH NEXT FROM cCurLinkNew INTO @varlinkidentifieridPRINT 'OUTER LOOP START'WHILE (@@FETCH_STATUS = 0) -- sql cursor fetch_statusBEGINset @temp=0----cursor to set continuous with page no----Declare cCurLinkGroupNew Cursor Forselect serialNo,identifierid from RS_Identifier where linkidentifierid=@varlinkidentifierid group by IdentifierID,serialNoOPEN cCurLinkGroupNewWHILE (@@FETCH_STATUS = 0)BEGINFETCH NEXT FROM cCurLinkGroupNew INTO @varSerialNo,@varidentifierIDPRINT 'INNER LOOP START'-- WHILE (@@FETCH_STATUS = 0)print 'swati'print @temp print @varidentifierIDif(@PageTypeID=28)beginUpdate RS_BillingSummaryset ContinuousWP=@tempwhere IdentifierID=@varidentifierIDendif(@PageTypeID=27)beginUpdate RS_VisitSummaryset Continwithpage=@tempwhere IdentifierID=@varidentifierIDendif(@PageTypeID<>27)beginif(@PageTypeID<>28)beginUpdate RS_Identifierset ContinuationWithPg=@tempwhere IdentifierID=@varidentifierIDendendset @temp=@varSerialNoPRINT 'INNER LOOP'end-- inner whileCLOSE cCurLinkGroupNewDEALLOCATE cCurLinkGroupNewFETCH NEXT FROM cCurLinkNew INTO @varlinkidentifieridPRINT 'OUTER LOOP'End -- outer whileCLOSE cCurLinkNewDEALLOCATE 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 |
 |
|
|
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 ForSelect distinct linkidentifierid from rs_identifier where pageTypeid=@PageTypeID and recordid=@RecordID OPEN cCurLinkNewFETCH NEXT FROM cCurLinkNew INTO @varlinkidentifieridPRINT 'OUTER LOOP START'WHILE (@@FETCH_STATUS = 0) -- sql cursor fetch_statusBEGIN 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 whileCLOSE cCurLinkGroupNewDEALLOCATE cCurLinkGroupNewFETCH NEXT FROM cCurLinkNew INTO @varlinkidentifieridPRINT 'OUTER LOOP'End -- outer whileCLOSE cCurLinkNewDEALLOCATE 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!GreetingsFred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
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=@varSerialNoIt's very unlikely that you need a cursor.An infinite universe is the ultimate cartesian product. |
 |
|
|
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" |
 |
|
|
|
|
|
|
|