sql编写技巧
1、利用xact_abort简化事务的编写
CREATE PROCEDURE Tran1
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 tran1
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 tran1
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