簡體   English   中英

COMMIT TRANSACTION請求沒有相應的BEGIN TRANSACTION

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

此代碼有什么問題。

出現此錯誤The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION. 當程序中出現某些異常時。 我該如何解決?

    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

具有:

IF @@ERROR <> 0
BEGIN
     ROLLBACK
END

在游標循環內是一個不好的信號-您回滾事務,然后繼續進行下一個迭代。 當循環最終結束時,您將嘗試提交,並且-糟糕-不再有打開的事務,並且回滾后的所有操作都保留在原處。

您可能想在回滾后使用GOTO退出循環,或以其他方式處理錯誤。 很難說出最佳策略是什么。

您可以使用命名交易:

-初期的大交易
開始交易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 

提交/回滾事務BIG_TRANSACTION

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM