SQL cursor用法实例

复制代码 代码如下:

declare @oldid varchar(50)  

declare @customerid varchar(50)  

declare my_cursor cursor –定义游标  

for (select customerid,oldid from customer where area=’bj’) –查出需要的集合放到游标中  

open my_cursor; –打开游标  

fetch next from my_cursor into @customerid,@oldid; –读取第一行数据  

while @@fetch_status = 0     

    begin    

       declare @otherpro varchar(500)  

       declare @statusid varchar(200)  

       declare @userid varchar(200)  

       declare @finaluserid varchar(200)  

      select @otherpro=otherpro,@statusid=customerstatusid,@userid=userid from bjsunmis.dbo.customer where customerid=@oldid;  

      select @finaluserid=userid from users where oldid=@userid  

      insert into customerotherinfo(customerid,otherpro,customerstatusid,userid)values(@customerid,@otherpro,@statusid,@finaluserid)  

     fetch next from my_cursor into @customerid,@oldid; –读取下一行数据      

    end 

close my_cursor; –关闭游标  

deallocate my_cursor; –释放游标  

go

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐