天天看點

sql server批量處理資料,遊标使用

DECLARE MyCursor CURSOR FOR     /*定義遊标*/
select count(*) as irowno,dls.DLID from DispatchLists dls where dls.DLID in (select dl.DLID from DispatchList dl where (dl.dDate <= '2014-12-31')) group by dls.DLID 

OPEN MyCursor

declare @cc nvarchar(50),@bid nvarchar(50),@cdlcode nvarchar(50) /*定義變量*/
fetch next from MyCursor into @cc,@bid   /*寫入資料*/
while @@FETCH_STATUS = 0                 /*開始循環*/
begin
print 'DLID'[email protected]
print @cc
	declare TCursor CURSOR FOR
	select irowno,DLID,AutoID from DispatchLists where DLID = @bid
	OPEN TCursor
	declare @cli nvarchar(50),@bidd nvarchar(50),@cid nvarchar(50),@i int=0
	fetch next from TCursor into @cli,@bidd,@cid
	while @@FETCH_STATUS = 0
	begin
	set @i = @i +1
	print @cid
	update DispatchLists set irowno = @i where AutoID = @cid

	fetch next from TCursor into @cli,@bidd,@cid
	end
	close TCursor
	deallocate TCursor

	select @cdlcode = cDLCode from DispatchList where (dDate <= '2014-12-31') And DLID = @bid
    update SaleBillVouchs set iDLsId = @bid where cbdlcode = @cdlcode
fetch next from MyCursor into @cc,@bid
end
close Mycursor
deallocate Mycursor
           

淘寶小店:給個關注呗