游标的使用

    declare @BusinessID int
declare @MSMnum bigint

DECLARE MSMnum_cursor CURSOR
FOR
select BusinessID,MSMnum from #temp1
OPEN MSMnum_cursor

FETCH NEXT FROM MSMnum_cursor INTO @BusinessID,@MSMnum
WHILE (@@FETCH_STATUS <> -1)
BEGIN

update A
set A.BusinessID=@BusinessID
from (select ROW_NUMBER() OVER(ORDER BY seq_no DESC) as RowSeq,*
from Fact_TSMSInfo where BusinessID =5
and Createdate>=CONVERT(varchar(10),@BeginDate,112)
and CreateDate<CONVERT(varchar(10),DATEADD(d,1,@EndDate),112)) A
where A.RowSeq<=@MSMnum

print @@rowcount

FETCH NEXT FROM MSMnum_cursor INTO @BusinessID,@MSMnum
END
close MSMnum_cursor
DEALLOCATE MSMnum_cursor
原文地址:https://www.cnblogs.com/arnold/p/2314939.html