有时候为了数据完整我们会启用到事务。正常的时候一帆风顺,如果rollback 呢?
最简单的一个回滚IF OBJECT_ID('PROC1') IS NOT NULL DROP PROCEDURE dbo.PROC1;GOCREATE PROCEDURE PROC1AS BEGIN BEGIN TRY BEGIN TRAN; SELECT 'Tran1' ,1; RAISERROR(16,1,1); SELECT 'Tran1' ,2; COMMIT TRAN; END TRY BEGIN CATCH SELECT 'Tran1' ,3; ROLLBACK TRAN; SELECT 'Tran1' ,4; RETURN -1; END CATCH; END;GO执行一下,抛出错误后继续向下执行。当事务有嵌套的时候。在抛出错误的事务里面,会继续执行批处理剩下的语句,最后才回滚上去并往上层抛错误。非抛出层和最外层事务,语句运行至rollback就往上层抛。如下IF OBJECT_ID('PROC2') IS NOT NULLDROP PROCEDURE dbo.PROC2goCREATE PROCEDURE PROC2ASBEGINBEGIN TRYBEGIN TRAN SELECT 'Tran2',1 EXEC dbo.PROC1 SELECT 'Tran2',2COMMIT TRANEND TRYBEGIN CATCH SELECT 'Tran2',3 ROLLBACK TRAN SELECT 'Tran2',4 RETURN -2END CATCHENDGOIF OBJECT_ID('PROC3') IS NOT NULLDROP PROCEDURE dbo.PROC3goCREATE PROCEDURE PROC3ASBEGINBEGIN TRYBEGIN TRAN SELECT 'PROC3',1 EXEC dbo.PROC2 SELECT 'PROC3',2COMMIT TRANEND TRYBEGIN CATCH SELECT 'PROC3',3 ROLLBACK TRAN SELECT 'PROC3',4 RETURN -3END CATCHENDGO执行存储过程SET NOCOUNT ON;DECLARE @INT INTEXEC @INT=dbo.PROC3SELECT @INTrollback在嵌套触发器中,如果在触发器的事务里面抛出错误,回滚之前触发器的语句修改,继续执行rollback之后的语句。但并不会激活之后的触发器CREATE TABLE TestTR(ID INT PRIMARY KEY,Name NVARCHAR(50))CREATE TABLE TRTB1(ID INT PRIMARY KEY,Col1 INT)CREATE TRIGGER TR_TESTTR1 ON dbo.TestTR AFTERINSERT ASBEGIN TRYBEGIN TRANINSERT INTO dbo.TRTB1 ( ID, Col1 )VALUES ( 1, CONVERT(INT,'a') )COMMIT TRANEND TRYBEGIN CATCHROLLBACK TRANINSERT INTO dbo.TestTR ( ID, Name )VALUES ( 1, CONVERT(INT,'2') )END CATCHGOINSERT INTO TestTR ( ID, Name )VALUES ( 4, CONVERT(INT,'3') )新闻热点
疑难解答