存储过程里使用游标

游标中用到的函数,就是前一篇文章中创建的那个函数。

另外,为了方便使用,把游标放在存储过程中,这样就可以方便地直接使用存储过程来执行游标了

create procedure UpdateHKUNo    --存储过程里面放置游标
as
begin

    declare UpdateHKUNoCursor cursor    --声明一个游标,查询满足条件的数据
        for select psn_code from person where type='E' and hku_no is null
   
    open UpdateHKUNoCursor    --打开
   
    declare @noToUpdate varchar(20)    --声明一个变量,用于读取游标中的值
        fetch next from UpdateHKUNoCursor into @noToUpdate
   
    while @@fetch_status=0    --循环读取
        begin
        --print @noToUpdate
        update person set hku_no=dbo.GetExtUserHKUNo() where psn_code=@noToUpdate
        fetch next from UpdateHKUNoCursor into @noToUpdate
        end
   
    close UpdateHKUNoCursor    --关闭
   
    deallocate UpdateHKUNoCursor    --删除
   
end

--exec UpdateHKUNo

if exists (select * from sysobjects where name= 'UpdateHKUNo' and xtype ='P')
print 'yse'
else
print 'no'


原文地址:https://www.cnblogs.com/JUSTSOSOBLOG/p/5016951.html