当前位置: 代码迷 >> Sql Server >> sql2000 事务有关问题
  详细解决方案

sql2000 事务有关问题

热度:73   发布时间:2016-04-27 14:19:10.0
sql2000 事务问题
代码如下:其中三条语句,中间那条语句是会出错,我的想法是,如果任何一条语句出错,都回滚,但像现在这样写法,即使中间条语句出错,第一条和第三条还是照样运行,请教该如何写?
SQL code
begin tran delete from temp_001 where id>37000 insert into temp_001 select null,0,0,0,0,0,0,0,0,0,0,0 delete from temp_002 where id>37000if @@error<>0 --判断如果三条语句有任何一条出现错误begin     rollback tran --–开始执行事务的回滚,end else   --如何三条都执行成功begin     commit tran --执行这个事务的操作end



------解决方案--------------------
SQL code
begin tran    delete from temp_001 where id>37000    insert into temp_001 select null,0,0,0,0,0,0,0,0,0,0,0    delete from temp_002 where id>37000commit tran --执行这个事务的操作if @@error<>0 --判断如果三条语句有任何一条出现错误         rollback tran --–开始执行事务的回滚,
------解决方案--------------------
SQL code
begin try    begin tran     delete from temp_001 where id>37000     insert into temp_001 select null,0,0,0,0,0,0,0,0,0,0,0     delete from temp_002 where id>37000     commit tranend trybegin catch     rollback tran end  catch
------解决方案--------------------
SQL code
set xact_abort onbegin tran delete from temp_001 where id>37000 insert into temp_001 select null,0,0,0,0,0,0,0,0,0,0,0 delete from temp_002 where id>37000commit tran
  相关解决方案