SQL 游标的使用
--声明游标的变量和类型 DECLARE @UserId int DECLARE @Days int --创建游标,定义游标curfuntioncode,用select给游标赋值 DECLARE curfuntioncode CURSOR FOR SELECT UserId,[Day] as [Days] FROM [Cycles] where CycleType = 1 and UserId < 2900 --创建游标 OPEN curfuntioncode --打开游标 FETCH NEXT FROM curfuntioncode INTO @UserId,@Days --给游标变量赋值 WHILE @@FETCH_STATUS=0 --判断FETCH语句是否成功获取数据 BEGIN --执行代码 --INSERT INTO [Permissions] (DepartmentPositionId,PermissionOperation) values (@DepartmentPositionId,3409) PRINT @UserId --打印数据(对每一行数据进行操作) FETCH NEXT FROM curfuntioncode INTO @UserId,@Days --下一个游标变量赋值 END CLOSE curfuntioncode --关闭游标 DEALLOCATE curfuntioncode --释放游标