日期:2014-05-18  浏览次数:20432 次

求一个游标中事务控制的方法.
declare   my_cur   cursor   for
        select   a,b,c,d   from   tbl
    open   my_cur
    fetch   next   from   my_cur   into   @a,@b,@c,@d
    while   @@fetch_status=0
    begin
        insert   into   tbl_tmp   (a,b,c,d)values(@a,@b,@c,@d)
        fetch   next   from   my_cur   into     @a,@b,@c,@d
    end
    close   my_cur
    deallocate   my_cur

    update   tbl_test   set   flag=1   where   a=@a
---------------------------
以上就要需在过程里面实现的内容.要达到的目的如下:
1.该过程执行中,不允许其它用户执行此过程.
2.执行过程中,只要有错误,全部回滚.(不能是回滚出错语句,继续执行下面语句)

------解决方案--------------------
1、在开始执行后,可以在某个表中做个标志,其他用户在开始执行前,先检查是否有人在执行,如果有人在执行,则等待。


2、如下:

set xact_abort on --设置事务中任一SQL出错,回滚整个事务

begin tran --开始事务

declare my_cur cursor for
select a,b,c,d from tbl

open my_cur
fetch next from my_cur into @a,@b,@c,@d

while @@fetch_status=0
begin
insert into tbl_tmp (a,b,c,d)values(@a,@b,@c,@d)
fetch next from my_cur into @a,@b,@c,@d
end

close my_cur
deallocate my_cur

update tbl_test set flag=1 where a=@a


commit tran --提交事务

------解决方案--------------------
mark
------解决方案--------------------
一樓正解