分类: WINDOWS
2012-04-16 21:14:47
先看一下SQL Server Online Help相关的说明
--Variable for keeping @@ERROR
DECLARE @_Error INT;
SET @_Error = 0;
--Begin 3 nested transaction
BEGIN TRANSACTION OUTERTRAN;
BEGIN TRANSACTION INNER1;
BEGIN TRANSACTION INNER2;
INSERT INTO TESTTRAN VALUES (3,'ccc');--Inner2
--raiserror('Inner2 error', 16, 1)
SET @_Error = @@ERROR
IF @_Error = 0
COMMIT TRAN INNER2;
ELSE
IF @@TRANCOUNT > 1
COMMIT TRANSACTION INNER2;
ELSE
ROLLBACK TRANSACTION INNER2;
INSERT INTO TESTTRAN VALUES (2,'bbb');--Inner1
IF @_Error = 0
SET @_Error = @@ERROR
IF @_Error = 0
COMMIT TRAN INNER1;
ELSE
IF @@TRANCOUNT > 1
COMMIT TRANSACTION INNER1;
ELSE
ROLLBACK TRANSACTION INNER1;
INSERT INTO TESTTRAN VALUES (1,'aaa');--OuterTran
RAISERROR ('OuterTran error',16,1)
-- rollback transaction OuterTran
SET @_Error = @_Error + @@ERROR
IF @_Error = 0
COMMIT TRAN OUTERTRAN;
ELSE
IF @@TRANCOUNT > 1
COMMIT TRANSACTION;
ELSE
ROLLBACK TRANSACTION OUTERTRAN;
SELECT * FROM TESTTRAN (NOLOCK)