存储过程错误:EXECUTE之后的事务计数表示BEGIN和COMMIT语句的数量不匹配

时间:2017-03-25 07:47:32

标签: sql sql-server-2012

当我在表SET @strResp = 'Free bet already exists' 上传递新的输入值时,我的存储过程工作正常,当我传递已经存在的值时会抛出错误

  

EXECUTE之后的事务计数表示BEGIN和COMMIT语句的数量不匹配。先前的计数= 0,当前计数= 1. 1行有效

当我尝试将值设置为输出

时,获取上述错误消息
ALTER PROCEDURE sp_CreateNewFreeBet
    (@matchId int,
     @tournamentId int,
     @freeBetAmount int,
     @freeBetMsg varchar(500),
     @userEmail nvarchar(256),
     @strResp varchar(256) output)
AS
BEGIN
    SET XACT_ABORT ON
    SET NoCount ON

    DECLARE @TransactionNewFreeBet varchar(20) = 'TranNewfreeBet'
    DECLARE @FreeBetCount int
BEGIN TRY
BEGIN TRAN @TransactionNewFreeBet
    SELECT 
        @FreeBetCount = COUNT(FreeBetID) 
    FROM
        tbl_CreateFreeBet 
    WHERE
        MatchID = @matchId AND TournamentID = @tournamentId

    IF @FreeBetCount < 50
    BEGIN
        IF NOT EXISTS (SELECT FreeBetID FROM tbl_CreateFreeBet 
                       WHERE FreeBetDescription = @freeBetMsg 
                         AND TournamentID = @tournamentId 
                         AND MatchID = @matchId)
        BEGIN
            INSERT INTO tbl_CreateFreeBet 
            VALUES (@freeBetMsg, @freeBetAmount, @tournamentId, @matchId)

            DECLARE @FreeBetId int

            SELECT @FreeBetId = FreeBetID 
            FROM tbl_CreateFreeBet 
            WHERE FreeBetDescription = @freeBetMsg 
              AND TournamentID = @tournamentId 
              AND MatchID = @matchId

            DECLARE @UserId nvarchar(128)

            SELECT @UserId = Id 
            FROM AspNetUsers 
            WHERE Email = @userEmail

            INSERT INTO tbl_UserFreeBets (FreeBetID, TournamentID, MatchID, UserForYes, UserForNo, UserForNoBets) 
            VALUES (@FreeBetId, @tournamentId, @matchId, @UserId, null, null)

            COMMIT TRAN @TransactionNewFreeBet

            SET @strResp = 'success'

            SELECT DISTINCT(tbl_CreateFreeBet.FreeBetID), 
                tbl_CreateFreeBet.FreeBetDescription, 
                tbl_CreateFreeBet.FreeBetAmount, 
                tbl_CreateFreeBet.TournamentID, 
                tbl_CreateFreeBet.MatchID, 
                tbl_UserFreeBets.UserForYes, 
                tbl_UserFreeBets.UserForNo, 
                tbl_UserFreeBets.UserForNoBets  
            FROM
                tbl_CreateFreeBet, tbl_UserFreeBets 
            WHERE
                tbl_CreateFreeBet.MatchID = tbl_UserFreeBets.MatchID 
                AND tbl_CreateFreeBet.TournamentID = tbl_UserFreeBets.MatchID 
                AND tbl_CreateFreeBet.FreeBetID = tbl_UserFreeBets.FreebetID 
                AND tbl_UserFreeBets.FreebetID = @FreeBetId
    END
    ELSE
    BEGIN
        SET @strResp = 'Free bet already exist'

        ROLLBACK TRAN @TransactionNewFreeBet
    END
END
ELSE
BEGIN
    SET @strResp = 'MAX 50 user bets can be created for a match'
    ROLLBACK TRAN @TransactionNewFreeBet
END
END TRY
BEGIN CATCH
    SET @strResp = ERROR_MESSAGE()
    ROLLBACK transaction sp_CreateNewFreeBet
END CATCH
END

返回“1行有效”,但在这种情况下,不会有任何可执行的插入语句。

PFB我的存储过程:

Runtime.getRuntime().exec(pathToTheExecutableFile);

请让我知道我应该怎么做才能解决这个问题。

0 个答案:

没有答案