SET XACT_ABORT ON not Worked in Create Procedure

混江龙づ霸主 提交于 2019-12-13 06:41:58

问题


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


来源:https://stackoverflow.com/questions/11382105/set-xact-abort-on-not-worked-in-create-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!