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

时间:2014-09-01 12:36:13

标签: sql sql-server tsql transactions

此Sql Server存储过程为我提供了

错误:

(Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.):

SQL:

DECLARE @MessageType sysname
DECLARE @ConversationHandle uniqueidentifier
DECLARE @RawRemittanceAdviceId int
DECLARE @ClrReturnCode int
DECLARE @WebServiceUrl nvarchar(512)
DECLARE @ErrorMessage nvarchar(MAX)

BEGIN 
BEGIN TRANSACTION

WAITFOR (
            RECEIVE TOP (1)
                  @MessageType = message_type_name
                , @RawRemittanceAdviceId = message_body
                , @ConversationHandle = conversation_handle
            FROM [RawRATranslationChannel]
        ), TIMEOUT 5000 -- 5 seconds

IF (@@ROWCOUNT = 0)
    BEGIN
        ROLLBACK TRANSACTION
    END
ELSE
    BEGIN
    -- Check for end of dialog messages
    IF (@MessageType = 'http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog')
        BEGIN
            END CONVERSATION @ConversationHandle
        END
    ELSE IF (@MessageType = 'http://schemas.microsoft.com/SQL/ServiceBroker/Error')
        BEGIN
            -- Handle the error
            BEGIN
                -- Add Error Message
                UPDATE [dbo].[RawRemittanceAdvice]
                SET DiagnosticMessage = N'Service Broker Error', ProcessingStatus = 2, LastUpdated = GETUTCDATE()
                WHERE RawRemittanceAdviceId = @RawRemittanceAdviceId
            END
            END CONVERSATION @ConversationHandle
        END
    ELSE IF (@MessageType = 'TranslateRawRA')
        -- Perform Translation of the Raw Remittance Advice
        BEGIN
            BEGIN TRY

                SELECT @WebServiceUrl = SettingValue
                FROM dbo.GlobalSettings
                WHERE SettingName = N'TranslationWebServiceUrl'

                -- Call CLR Sproc to perform Translation
                EXEC @ClrReturnCode = [dbo].[TranslateRawRemittanceAdvice] @RawRemittanceAdviceId, @WebServiceUrl, @ErrorMessage OUTPUT

                IF @ClrReturnCode = 0
                    BEGIN
                    -- Send Response
                    SEND ON CONVERSATION @ConversationHandle
                    MESSAGE TYPE [TranslateRawRAResponse]
                    (CAST(@RawRemittanceAdviceId AS NVARCHAR))
                    END
                ELSE
                    BEGIN
                        -- Add Error Message
                        UPDATE [dbo].[RawRemittanceAdvice]
                        SET DiagnosticMessage = @ErrorMessage, ProcessingStatus = 2, LastUpdated = GETUTCDATE()
                        WHERE RawRemittanceAdviceId = @RawRemittanceAdviceId
                    END

            END TRY

            BEGIN CATCH
                BEGIN
                    -- Add Error Message
                    UPDATE [dbo].[RawRemittanceAdvice]
                    SET DiagnosticMessage = ERROR_MESSAGE(), ProcessingStatus = 2, LastUpdated = GETUTCDATE()
                    WHERE RawRemittanceAdviceId = @RawRemittanceAdviceId
                END
            END CATCH

            END CONVERSATION @ConversationHandle

        END
    COMMIT TRANSACTION
END
END  

1 个答案:

答案 0 :(得分:0)

请参阅以下变通方法以供参考:

  1. 在可疑的sql代码区域尝试PRINT @@ TranCount,以获取交易次数。
  2. 尝试通过启用DEBUG执行相同的操作。