SQL 游标 Fetch
一个游标的例子,记下以备查阅:
--开始事务
BEGIN TRAN
--不显示计数信息
SET NOCOUNT ON
Declare @UserID Int ,@Point Int
--声明游标
DECLARE User_cursor CURSOR FOR
SELECT UserID,Point FROM Member
--打开游标
OPEN User_cursor
--取第一行的值给变量
FETCH NEXT FROM User_cursor
INTO @UserID,@Point
WHILE @@FETCH_STATUS = 0
Begin
update ..
--执行错误回滚
if @@error!=0
begin
rollback tran
return
end
FETCH NEXT FROM User_cursor
INTO @UserID,@Point
End
--关闭游标
CLOSE CRMPSContact_cursor
--释放游标
DEALLOCATE CRMPSContact_cursor
--提交所有变更
COMMIT TRAN
--恢复设置
SET NOCOUNT OFF
--开始事务
BEGIN TRAN
--不显示计数信息
SET NOCOUNT ON
Declare @UserID Int ,@Point Int
--声明游标
DECLARE User_cursor CURSOR FOR
SELECT UserID,Point FROM Member
--打开游标
OPEN User_cursor
--取第一行的值给变量
FETCH NEXT FROM User_cursor
INTO @UserID,@Point
WHILE @@FETCH_STATUS = 0
Begin
update ..
--执行错误回滚
if @@error!=0
begin
rollback tran
return
end
FETCH NEXT FROM User_cursor
INTO @UserID,@Point
End
--关闭游标
CLOSE CRMPSContact_cursor
--释放游标
DEALLOCATE CRMPSContact_cursor
--提交所有变更
COMMIT TRAN
--恢复设置
SET NOCOUNT OFF