CREATE PROCEDURE [dbo].[pro_CURSOR]
AS
BEGIN
--声明
DECLARE @myStr NVARCHAR(200)
SET @myStr = ''
--声明一个游标
DECLARE MyCURSOR CURSOR
FOR
--SELECT userid,depid FROM users
SELECT emplid ,
Deptid
FROM LegalBusinessUnitMapping
--打开游标
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
SET @myStr = @myStr + 'EmplID:' + @userid + 'DeptID:' + @depid
FETCH NEXT FROM MyCURSOR INTO @userid, @depid
END
CLOSE MyCURSOR
DEALLOCATE MyCURSOR
SELECT @myStr
END