存储过程添加事务
CREATE PROCEDURE P_Proc
as
begin tran
set xact_abort on
Insert Into trantest (id,test)values(1,'test')
Insert Into trantest (id,test)values(2,'test')
commit tran
GO
set xact_abort on 表示遇到错误立即回滚
当然你也可以这么写
CREATE PROCEDURE PR_P
as
begin tran
insert into trantest(id,test)values(1,'test')
if(@@error<>0)
rollback tran
else
begin
insert into trantest(id,test)values(2,'test')
if(@@error<>0)
rollback tran
else
commit tran
end
GO
as
begin tran
set xact_abort on
Insert Into trantest (id,test)values(1,'test')
Insert Into trantest (id,test)values(2,'test')
commit tran
GO
set xact_abort on 表示遇到错误立即回滚
当然你也可以这么写
CREATE PROCEDURE PR_P
as
begin tran
insert into trantest(id,test)values(1,'test')
if(@@error<>0)
rollback tran
else
begin
insert into trantest(id,test)values(2,'test')
if(@@error<>0)
rollback tran
else
commit tran
end
GO