SET XACT_ABORT ON not Worked in Create Procedure

2019-08-27 21:03发布

问题:

I use SQL Server 2008 SP3 (10.0.5500) And I have some Problems with Rollback Transactions, at first I need to know something. This is My create Procedure script:

USE [MYDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET XACT_ABORT ON
GO

If Exists(Select * From Sys.Objects Where [object_id]=Object_Id(N'[Members].[MyProc]') And ObjectProperty([object_id], N'IsProcedure') = 1)
Begin
Drop Procedure [Members].[MyProc]
Print 'Procedure [Members].[MyProc] Dropped!'
End
GO

CREATE PROCEDURE [Members].[MyProc](

 ....

)
AS 
BEGIN
    BEGIN TRANSACTION [MyProcCHK]
    ....
    COMMIT TRANSACTION [MyProcCHK]
END
GO

And After run this script, I check Procedure from: MyDB ->Programmability->StoredProcedures And Click to Modify [Members].[MyProc] Then this is the script shown:

USE [MYDB]
GO
/****** Object:  StoredProcedure ... ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [Members].[MyProc](

 ....

)
AS 
BEGIN
    BEGIN TRANSACTION [MyProcCHK]
    ....
    COMMIT TRANSACTION [MyProcCHK]
END
GO

So Where is SET XACT_ABORT ON? And why that not displayed here? As I have some problems with rollback transactions in stored procedures I think the SET XACT_ABORT ON never saved. Am I right? and if yes, what is the solution? I can't use SET XACT_ABORT ON in stored procedures? or where is my fault?

回答1:

You need to mention set xact_abort on inside the Create procedure statement

 CREATE PROCEDURE [Members].[MyProc](
 ........

 )
 AS
 SET XACT_ABORT ON
BEGIN
BEGIN TRANSACTION [MyProcCHK]
....
COMMIT TRANSACTION [MyProcCHK]
END
GO