SQL Server 2012:提交与存储过程中的开始 tran 不匹配

Jac*_*cek 0 sql sql-server stored-procedures transactions

我在 SQL Server 2012 Express 中编写了这个存储过程。

ALTER PROCEDURE [Dictionaries].[InsertCountry]
(@p_countryName nvarchar(128)
 , @ret_countryId int OUTPUT)
AS
BEGIN
    DECLARE @TransactionName VARCHAR(20) = 'INST_COUNTRY';

    BEGIN TRANSACTION @TransactionName;

IF len(@p_countryName) <= 3
BEGIN
        SET @ret_countryId = null
        ROLLBACK TRANSACTION @TransactionName;
END

INSERT INTO [Dictionaries].[CountryDetails] (name)
VALUES (@p_countryName);    

SET @ret_countryId = @@IDENTITY

INSERT INTO [Dictionaries].[Places] (name, Discriminator , CountryDetails_Id, RegionDetails_Id, CityDetails_Id)
VALUES (@p_countryName, 'Country' , @ret_countryId, null, null);

COMMIT TRANSACTION @TransactionName;
END
Run Code Online (Sandbox Code Playgroud)

我以这种方式使用这个方法,但我收到消息

COMMIT TRANSACTION 请求没有对应的 BEGIN TRANSACTION。

代码:

declare @ret int = null
EXEC [Dictionaries].[InsertCountry] 'us', @ret output
Run Code Online (Sandbox Code Playgroud)

这段代码有什么问题?

usr*_*usr 5

首先回滚,然后提交。这是一个错误。也许,您打算在回滚后返回。