select * from 一个不在的表 if @@error<>0 print '这个没有输出' go raiserror('',16,3) if @@error<>0 print '这个输出了' go exec('select * from 一个不在的表') if @@error<>0 print '这个输出了' go exec sp_executesql N'select * from 一个不在的表' if @@error<>0 print '这个输出了' |
select @@trancount 当前连接的活动事务数 --当前连接的活动事务数为0 begin tran select * from 一个不在的表 if @@error<>0 begin print '没有执行到这里来!' if @@trancount<>0 rollback tran end commit tran |
print @@trancount print '当前连接的活动事务数' --当前连接的活动事务数为0 if @@trancount<>0 rollback tran --在这里写可以让孤立事务只保持到下次你的过程被调用 begin tran select * from 一个不在的表 if @@error<>0 begin print '没有执行到这里来!' if @@trancount<>0 rollback tran end commit tran |
create table Table1 (a int check(a>100)) go set xact_abort on begin tran insert table1 values(10) print '这里没有被执行' commit tran go print '' print '==============================================' print '' set xact_abort off begin tran insert table1 values(10) print '这里被执行' commit tran go drop table table1 但 set xact_abort 对于编译产生的错误确没有起作用,且同样会产生孤立事务 set xact_abort on begin tran insert 一个不在的表 values(10) print '这里没有被执行' commit tran go print '' print '==============================================' print '' set xact_abort off begin tran insert 一个不在的表 values(10) print '这里没有被执行' commit tran go select @@trancount 当前连接的活动事务数 ---有两个孤立事务 if @@trancount<>0 rollback tran |
欢迎光临 DIY编程器网 (http://diybcq.com/) | Powered by Discuz! X3.2 |