• 推荐
  • 评论
  • 收藏

SQL 游标基本用法

2022-12-21    2995次浏览
CREATE PROCEDURE [dbo].[pro_CURSOR]
AS
BEGIN

--声明一个游标
DECLARE MyCURSOR CURSOR FOR 
SELECT userid,depid FROM users

--打开游标
open MyCURSOR

--声明两个变量
declare @userid varchar(50)
declare @depid varchar(50)

--循环移动
fetch next from MyCURSOR into @userid,@depid
while(@@fetch_status=0)
  begin
    update users set description=(select dep_des from department where depid=@depid)
where userid=@userid
    fetch next from MyCURSOR into @userid,@depid
  end

close MyCURSOR
deallocate MyCURSOR

END
原文地址:https://www.cnblogs.com/Leo_wl/p/2117922.html