简体   繁体   中英

Cannot set IMPLICIT_TRANSACTIONS to OFF for stored procedure?

I have a Stored Procedure in which I'm NOT using any explicit Transaction related code (ie begin/rollback/commit transaction), and yet @@Trancount is set to 2 (I'm monitoring this by writing this value into a Table's row entry during the Stored Procedure). This obviously means thats IMPLICIT_TRANSACTIONS is set to ON somewhere.

I'm adding the following lines to the start of my Stored Procedure ....

SET IMPLICIT_TRANSACTIONS OFF
GO

.... So that it becomes this:

USE [RentTrackingSystem]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET IMPLICIT_TRANSACTIONS OFF
GO
ALTER PROCEDURE [RTS].[GenerateAnnualPenalty] 
    -- Add the parameters for the stored procedure here

    @dueDate Date = NULL ,
    @fiscalYear numeric(4),
    @createdBy Varchar(50),
    @referenceForm Varchar(50),
    @referenceFormNo Varchar(50),
    @PENALTY_NO int ,
    @PenaltyCutOffDate date = NULL

AS

-- Rest of the body here ..

However when I execute the query (to Alter the Stored Procedure), close that window and then again open the same Stored Procedure's code, that addition goes away, and the Stored Procedure again becomes:

USE [RentTrackingSystem]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [RTS].[GenerateAnnualPenalty] 
    -- Add the parameters for the stored procedure here

    @dueDate Date = NULL ,
    @fiscalYear numeric(4),
    @createdBy Varchar(50),
    @referenceForm Varchar(50),
    @referenceFormNo Varchar(50),
    @PENALTY_NO int ,
    @PenaltyCutOffDate date = NULL

AS

-- Rest of the body here ..

So what's going on here ?

You can't get SET IMPLICIT_TRANSACTIONS OFF before the body of the stored procedure because this is not a value that is captured when a stored procedure is created. When you set the implicit_transactions to off all you're doing is setting the the value for your connection so when you run your alter statement you'll be running it under the context of having implicit_transactions turned off, but there is no relation to that setting and what is captured for the stored procedure definition.

To put it another way there are many settings that are affecting your queries at any given time, but SQL Server only captures ANSI_NULLS and QUOTED_IDENTIFIER settings when creating a stored procedure.

Per MSDN:

When a stored procedure is created, the SET QUOTED_IDENTIFIER and SET ANSI_NULLS settings are captured and used for subsequent invocations of that stored procedure.

If you want the content of the sproc to be affected by the SET IMPLICIT_TRANSACTIONS OFF setting simply set it as the first thing after the Create Proc Name (variables Datatypes) As... sproc declaration.

Note that this is not going to clear out existing transactions. If you have transactions outside of the stored procedure they will still exist, this setting is simply going to prevent any new implied transactions from being created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM