此代码用于解决存储过程间嵌套调用时的全局原子性问题:
DECLARE @TranStarted bit
SET @TranStarted = 0
IF( @@TRANCOUNT = 0 ) -- 用于嵌套
BEGIN
BEGIN TRANSACTION
SET @TranStarted = 1
END
ELSE
SET @TranStarted = 0
.....
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
COMMIT TRANSACTION
END