如何才能保障存储过程嵌套调用的原子性

7/14/2008来源:SQL技巧人气:7346

此代码用于解决存储过程间嵌套调用时的全局原子性问题:

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