开始尝试结束尝试开始捕获更新表结束捕获

时间:2019-07-31 09:07:12

标签: sql-server tsql

我正在开发存储过程。

我有一个暂存表,称为ServiceAssetsBilling,据此我会将帐单信息导入到所有数据都应转到的相关表中。

我的查询如下:

select  @loop = min(idServiceAssetsBilling) from ServiceAssetsBilling where bImported = 0
while   @loop is not null

begin

    begin
        begin transaction
            begin try

                    insert into _smtblContractMatrixPeriodService   (   cCode, cDescription, fAmount, cExternalRef, bInvoiceWithoutConfirmation, bBillInAdvance, cBillingSchedule, dNextDate, iFrequency
                                                                    ,   iJan, iFeb, iMar, iApr, iMay, iJun, iJul, iAug, iSep, iOct, iNov, iDec, iUserId, bDeleted, dtStamp, iAccountId, iContractMatrixID
                                                                    ,   _smtblContractMatrixPeriodService_iBranchID, bIsEscalation, fEscalationPer, fEscalationAmt, fEscFrequency, fEscalationDays
                                                                    ,   iRepeatEscType, bIsPercentage, fOriginalAmount , iProjectId )
                    select
                        cContractNo     cCode
                    ,   cContractDesc   cDescription
                    ,   fAmountExcl     fAmount
                    ,   cOrderNo        cExternalRef
                    ,   0               bInvoiceWithoutConfirmation
                    ,   0               bBillInAdvance
                    ,   case when cBillingSchedule = 'Date Based' then 'DAT' else 'MON' end cBillingSchedule
                    ,   dNextBillDate   dNextDate
                    ,   iRepeatDays     iFrequency
                    ,   iJanuary        iJan
                    ,   iFebruary       iFeb
                    ,   iMarch          iMar
                    ,   iApril          iApr
                    ,   iMay
                    ,   iJune           iJun
                    ,   iJuly           iJul
                    ,   iAugust         iAug
                    ,   iSeptember      iSep
                    ,   iOctober        iOct
                    ,   iNovember       iNov
                    ,   iDecember       iDec
                    ,   1               iUserId
                    ,   0               bDeleted
                    ,   getdate()       dtStamp
                    ,   (select top 1 DCLink from Client where Account = cCustomerCode) iAccountId
                    ,   (select top 1 AutoIdx from _smtblContractMatrix where cCode = cContractNo) iContractMatrixID
                    ,   0               _smtblContractMatrixPeriodService_iBranchID
                    ,   0               bIsEscalation
                    ,   0               fEscalationPer
                    ,   0               fEscalationAmt
                    ,   0               fEscFrequency
                    ,   0               fEscalationDays
                    ,   -1              iRepeatEscType
                    ,   1               bIsPercentage
                    ,   fAmountExcl     fOriginalAmount
                    ,   (select top 1 ProjectLink from Project where ProjectCode = cProject) iProjectId
                    from    ServiceAssetsBilling
                    where   idServiceAssetsBilling = @loop

                    update ServiceAssetsBilling set bImported = 1, cErrorDetails = (case isnull(cErrorDetails,'')   when '' then 'Import Successful!' else  cErrorDetails+' | Import Successful!' end) where idServiceAssetsBilling = @loop

                commit
            end try
            begin catch
                if @@trancount > 0
                    begin

                        set @err = (select error_message())

                        update ServiceAssetsBilling set bImported = 1, bError = 1, cErrorDetails = (case isnull(cErrorDetails,'')   when '' then substring(@err,1,200) else cErrorDetails+' | '+substring(@err,1,200) end)
                        where idServiceAssetsBilling = @loop

                    end
                    rollback transaction;  
            end catch;  

            if @@trancount > 0  
                commit transaction;
    end

    select @loop = min(idServiceAssetsBilling) from ServiceAssetsBilling where idServiceAssetsBilling > @loop
end

如您所见,我正在使用循环。

如果插入失败,我需要使用错误详细信息更新相同的登台表,但不会更新。

当我添加时:

SELECT   
    ERROR_NUMBER() AS ErrorNumber  
    ,ERROR_SEVERITY() AS ErrorSeverity  
    ,ERROR_STATE() AS ErrorState  
    ,ERROR_PROCEDURE() AS ErrorProcedure  
    ,ERROR_LINE() AS ErrorLine  
    ,ERROR_MESSAGE() AS ErrorMessage; 

它向我显示了错误,但我需要将此错误更新到登台表的cErrorDetails

为了获得答案,我已经在Google上进行了尽可能多的搜索,但是我被困住了。

请告知我我做错了什么?

1 个答案:

答案 0 :(得分:0)

我发现了我的错误。

在下面的脚本中(从问题中删除):

        begin catch
            if @@trancount > 0
                begin

                    set @err = (select error_message())

                    update ServiceAssetsBilling set bImported = 1, bError = 1, cErrorDetails = (case isnull(cErrorDetails,'')   when '' then substring(@err,1,200) else cErrorDetails+' | '+substring(@err,1,200) end)
                    where idServiceAssetsBilling = @loop

                end
                rollback transaction;  
        end catch; 

我正在回滚事务,导致它不更新登台表。

将其更改为如下所示:

        begin catch
            if @@trancount > 0
                begin
                rollback transaction; 

                    set @err = (select error_message())

                    update ServiceAssetsBilling set bImported = 1, bError = 1, cErrorDetails = (case isnull(cErrorDetails,'')   when '' then substring(@err,1,200) else cErrorDetails+' | '+substring(@err,1,200) end)
                    where idServiceAssetsBilling = @loop

                end 
        end catch; 

成功了!