Thursday, March 29, 2012

Cursor question

I've recently began studying cursors. The following code is from my server but in don't understand the purpose of the highlighted part.


declare c_cursor cursor for
select distinct suniq from Stustat where trkuniq in (select trkuniq from track where schoolc = @.schoolc) and
graden >= @.beginningGradeForCalc


open c_cursor
fetch next from c_cursor into @.variable
while (@.@.fetch_status= 0)
begin
-- CHECK GPA (FIRES OFF SINGLE STUDENT DYNAMIC GPA CALCULATION FOR EACH SUNIQ IN STUGRP_ACTIVE FOR A SPECIFIED SCHOOL)
-- exec siSp_dyngpa @.suniq, @.schyear, @.graden example: exec sisp_dyngpa @.variable, 2007, 9, 1
exec sisp_dyngpa @.variable, @.beginningYrForCalc, @.beginningGradeForCalc, '1'
fetch next from c_cursor into @.variable

end
fetch next from c_cursor into @.variable
deallocate c_cursor

Is there an obvious reason for the FETCH after teh END statment?

I don't think it should be there as its outside the while loop. Doesn't serve any purpose as you are deallocating the cursor straight afterwards anyway and so no processing is done with the results.

HTH!

|||

No, there is no obvious reason. also, remember to close the cursor before deallocating it.

AMB

|||What are the ramification of not closing the cursor before deallocating it? Or it is more a matter of procedure?|||

I am not familiar with the internals, but I think that the description in BOL is clear.

AMB

sql

No comments:

Post a Comment