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://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)