sql server - Cursor logic error -
i wrote sp store data 2 tables in second table contain sectionid want distribute student section
alter proc [dbo].[studentdistribution] -- '6,7,8,9,10','1,2',1,1 ( @puserid varchar(8000), @psectionid varchar(8000), @pclassid int, @pmodifiedby int ) declare @cntr int set @cntr = (select count(*) split(@puserid,',')) select sum(capacity) sectionsclasses classid=@pclassid , sectionid in (select vitem split(@psectionid,',')) declare fetchstudent cursor select vitem split(@puserid,',') declare @vuserid int declare @vsecid int declare @vout_status int declare @vinner_status int open fetchstudent fetch next fetchstudent @vuserid set @vout_status= @@fetch_status while @vout_status = 0 begin declare fetchsection cursor select vitem split(@psectionid,',') open fetchsection fetch next fetchsection @vsecid set @vinner_status= @@fetch_status while @vinner_status = 0 begin print(@vuserid) print(@vsecid) --if @cntr <= 0 -- break --else -- set @cntr=@cntr-1 fetch next fetchstudent @vuserid set @vout_status= @@fetch_status fetch next fetchsection @vsecid set @vinner_status= @@fetch_status end close fetchsection deallocate fetchsection --if @cntr <= 0 -- break -- else -- set @cntr=@cntr-1 fetch next fetchstudent @vuserid end close fetchstudent deallocate fetchstudent i need result
6 1 7 2 8 1 9 2 10 1 but got is
6 1 7 2 9 1 10 2 can figure out problem ??
this answer
alter proc [dbo].[studentdistribution] --'6,7,8,9,10,11,12,13','1',3,1 ( @puserid varchar(8000), @psectionid varchar(8000), @pclassid int, @pmodifiedby int ) declare @vcntr int declare @vcapacity int set @vcntr = (select count(*) split(@puserid ,',')) select @vcapacity = sum(capacity) sectionsclasses classid=@pclassid , sectionid in (select vitem split(@psectionid,',')) if @vcntr > @vcapacity begin print('operation cannot complete; number of student exceeds capacity') return end declare fetchstudent cursor scroll select vitem split(@puserid,',') declare @vuserid int declare @vsecid int declare @vout_status int declare @vinner_status int open fetchstudent fetch next fetchstudent @vuserid set @vout_status= @@fetch_status while (@vout_status = 0 ) begin declare fetchsection cursor scroll select vitem split(@psectionid,',') open fetchsection fetch next fetchsection @vsecid set @vinner_status= @@fetch_status while (@vinner_status = 0 ) begin print(@vuserid) print(@vsecid) fetch next fetchsection @vsecid set @vinner_status= @@fetch_status --if @vinner_status = -1 -- break fetch next fetchstudent @vuserid set @vout_status= @@fetch_status if @vout_status = -1 break end close fetchsection deallocate fetchsection if @vinner_status =-1 fetch prior fetchstudent @vuserid fetch next fetchstudent @vuserid set @vout_status= @@fetch_status end close fetchstudent deallocate fetchstudent
Comments
Post a Comment