关于事务
creat proc name
as
insert table1 select tb from ..
insert table2 select tb from...
.....
.....
我想加个事务,如果这个过程中出现错误,我想让之前插入的数据全部回滚,应该怎么写,是直接在这里写,还是建个触发器比较好?
------解决方案--------------------creat proc name
as
set xact_abort on
begin
begin tran
insert table1 select tb from ..
insert table2 select tb from...
.....
.....
commit tran
end
------解决方案--------------------creat proc name
as
begin tran
insert table1 select tb from ..
insert table2 select tb from...
if @@error> 0
begin
rollback tran
end
else
begin
commit tran
end