在事务语句最前面加上 set xact_abort on
1 go 2 set quoted_identifier off 3 go 4 alter procedure [dbo].[test] 5 @a int, 6 @b int, 7 @c int, 8 @d int, 9 @e int, 10 @f int 11 as 12 13 set xact_abort on 14 begin tran 15 insert into testtb values(@a,@b,@c,@d) 16 insert into test4 values(@e,@f) 17 commit tran
当xact_abort选项为on时,sql server在遇到错误时会终止执行并rollback整个事务。