简体   繁体   English

COMMIT TRANSACTION请求没有相应的BEGIN TRANSACTION

[英]The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION

What is the problem with this code. 此代码有什么问题。

It is giving this error The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION. 出现此错误The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION. when some exception is coming in the procedure. 当程序中出现某些异常时。 How can I solve it? 我该如何解决?

    BEGIN 
    BEGIN TRANSACTION 
    DECLARE  
         @Id bigint 
        ,@Month nvarchar(100) 
        ,@Year nvarchar(100) 
        ,@CountryofExport nvarchar(100)
        ,@CountryofOrigin nvarchar(100) 
        ,@HSCode nvarchar(100)
        ,@Unit nvarchar(100)
        ,@Quantity nvarchar(100)
        ,@CustomValue nvarchar(255)
        ,@Type nvarchar(100)
        ,@TypeBit bit
        ,@CountryofExportID int
        ,@CountryofOriginID int
        ,@MeasurementId int
        ,@Remarks nvarchar(500)
        ,@CommodityId int
        ,@SDate nvarchar(100)
        ,@SameRec int
        ,@counts int


    DECLARE @Cursor_TradeFlow CURSOR
    SET @Cursor_TradeFlow = CURSOR FOR

    SELECT [Id],[Months],[Years],[CountryofExport],[CountryofOrigin],[HSCode],[Quantity],[Unit],[CustomValue],[Type] FROM [Temp_Trading]    

    OPEN @Cursor_TradeFlow
    FETCH NEXT FROM @Cursor_TradeFlow INTO @Id, @Month, @Year, @CountryofExport, @CountryofOrigin, @HSCode,@Quantity, @Unit, @CustomValue, @Type

    WHILE @@FETCH_STATUS = 0

    BEGIN
    Set @Remarks='';




   Declare @EICountry varchar(100),
   @Checkbit bit,
    @CheckYearIsNumeric bit,
    @CheckMonthIsNumeric bit


      BEGIN TRY        

         SET @CheckMonthIsNumeric= convert(INT, @Month);

      END TRY 

      BEGIN CATCH
    begin

             set @Checkbit=1;
         set @Remarks = @Remarks + 'Invalid Month'
         set @CheckMonthIsNumeric=1 
         end
      END CATCH



      BEGIN TRY

          set @CheckYearIsNumeric=  convert(INT, @Year);

      END TRY
      BEGIN CATCH


        SET @CheckYearIsNumeric= 1;
        set @Checkbit=1;
        set @Remarks = @Remarks + 'Invalid Year'

      END CATCH      


    Set @SameRec = (Select COUNT(*) From TradeFlow Where int_Month = @CheckMonthIsNumeric and int_Year = @CheckYearIsNumeric
                  and int_OriginLocationId = @CountryofExportID and int_DestinationLocationId = @CountryofOriginID and int_CommodityId = @CommodityId
                  and int_MeasurementId = @MeasurementId)   


    IF @@ERROR <> 0
    BEGIN
         ROLLBACK
    END

    FETCH NEXT FROM @Cursor_TradeFlow INTO @Id, @Month, @Year, @CountryofExport, @CountryofOrigin, @HSCode,@Quantity, @Unit, @CustomValue, @Type

    END
    CLOSE @Cursor_TradeFlow
    DEALLOCATE @Cursor_TradeFlow
    COMMIT
END

Having: 具有:

IF @@ERROR <> 0
BEGIN
     ROLLBACK
END

inside a cursor loop is a bad sign - you rollback the transaction, and then continue into the next iteration. 在游标循环内是一个不好的信号-您回滚事务,然后继续进行下一个迭代。 When the loop finally finishes, you attempt to commit and - Oops - there's no open transaction any longer, and every operation after the rollback has been left in place. 当循环最终结束时,您将尝试提交,并且-糟糕-不再有打开的事务,并且回滚后的所有操作都保留在原处。

You might want to exit the loop after the rollback using a GOTO , or deal with the errors in a different way. 您可能想在回滚后使用GOTO退出循环,或以其他方式处理错误。 It's too hard to tell what the best strategy might be. 很难说出最佳策略是什么。

You could use named transactions: 您可以使用命名交易:

-- big transaction in the beginning -初期的大交易
BEGIN TRANSACTION BIG_TRANSACTION 开始交易BIG_TRANSACTION

 -- your code here -- a transaction for each fetched item BEGIN TRANSACTION FETCH_TRANSACTION -- your code here if OK COMMIT TRANSACTION FETCH_TRANSACTION else ROLLBACK TRANSACTION FETCH_TRANSACTION 

COMMIT/ROLLBACK TRANSACTION BIG_TRANSACTION 提交/回滚事务BIG_TRANSACTION

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ROLLBACK TRANSACTION 请求没有对应的 BEGIN TRANSACTION - ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION 消息3902,级别16,状态1. COMMIT TRANSACTION请求没有相应的BEGIN TRANSACTION - Msg 3902, Level 16, State 1. The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION 收到错误:COMMIT TRANSACTION请求在SQL脚本中没有相应的BEGIN TRANSACTION - Receiving an error: The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION in SQL Script 获取错误“回滚事务请求没有相应的开始事务” - Getting error “The rollback transaction request has no corresponding begin transaction” ROLLBACK TRANSACTION 请求没有对应的BEGIN TRANSACTION,但是还是报错 - The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION, but still getting an error ROLLBACK TRANSACTION请求在sql server中没有相应的BEGIN TRANSACTION错误 - ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION error in sql server 开始交易…提交交易问题 - Begin Transaction … Commit Transaction Issue SQLException - 事务BEGIN / COMMIT不匹配 - SQLException - Transaction BEGIN / COMMIT mismatch IMPLICIT TRANSACTIONS ON / OFF和BEGIN / COMMIT交易 - IMPLICIT TRANSACTIONS ON/OFF and BEGIN /COMMIT Transaction 交易提交? - transaction commit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM