DECLARE @userid INT , @gameid NVARCHAR(50) --声明变量,需要读取的数据 DECLARE cur CURSOR --去掉STATIC关键字即可 FOR WITH Emp AS (SELECT acc.* FROM GXSpreadDB.dbo.gxs_account acc LEFT JOIN RYAccountsDB.dbo.AccountsInfo Account ON acc.userid=Account.userid WHERE parentuserid=7213 AND Account.AgentID=0 UNION ALL SELECT d.* FROM Emp ,GXSpreadDB.dbo.gxs_account d WHERE d.parentuserid =Emp.userid ) select userid,gameid from Emp OPEN cur --打开游标 FETCH NEXT FROM cur INTO @userid, @gameid --取数据 WHILE ( @@fetch_status = 0 ) --判断是否还有数据 BEGIN SELECT '数据: ' + RTRIM(@userid)+';' + @gameid --UPDATE #T SET name='测试' WHERE id=4 --测试静态动态用 FETCH NEXT FROM cur INTO @userid, @gameid --这里一定要写取下一条数据 END CLOSE cur --关闭游标 DEALLOCATE cur
查询结果: