代码如下:其中三条语句,中间那条语句是会出错,我的想法是,如果任何一条语句出错,都回滚,但像现在这样写法,即使中间条语句出错,第一条和第三条还是照样运行,请教该如何写?
- 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