sql 游标、sql server 游标使用、实例

if object_Id('tempdb..#Temp1010')is not null
    drop table #Temp1010
go
if object_Id('tempdb..#Temp2020')is not null
    drop table #Temp2020
go
create table #Temp2020(
        it_id nvarchar(100) null   
        );
insert #Temp2020 (it_id)values(1)
insert #Temp2020 (it_id)values(2)
insert #Temp2020 (it_id)values(3)
create table #Temp1010(
        it_id nvarchar(100) null,   
        pc_total nvarchar(100) null,
        note_total nvarchar(100) null
        );
DECLARE
        @it_id nvarchar(100),
        @pc_total nvarchar(100),
        @note_total nvarchar(100);

--声明游标

DECLARE MainCursor CURSOR FOR select it_id from  #Temp2020

    --打开游标
    OPEN MainCursor

        --提取数据
        FETCH NEXT FROM MainCursor into @it_id 
        WHILE @@FETCH_STATUS = 0
            BEGIN
                begin
                set @pc_total = N'101'
                set @note_total = N'102'
                insert into #Temp1010 values(@it_id,@pc_total,@note_total)
                end           
            FETCH NEXT FROM MainCursor INTO @it_id 
            END

    --关闭游标
    CLOSE MainCursor
DEALLOCATE MainCursor

select * from #Temp1010

原文地址:https://www.cnblogs.com/ok519/p/1580358.html