重写一个proc以避免插入然后回滚

时间:2016-08-31 10:46:02

标签: sql sql-server tsql

我遇到了proc问题,当我尝试验证以下内容时:

  • @invoicenumber + @invoiceid连接在一起制作 整体Invoicenumber

  • 如果数据库中已存在整体invoicenumber,则抛出' Invoice
    号码已存在'。

我遇到的问题是,即使发票编号不存在,它仍然会抛出此错误。我认为这是因为它已经插入了数据,但是在插入之后又回滚了,所以虽然它看起来没有插入到表格中,但它可能已经被插入,或者说我相信的是

我的问题是,有没有办法重新编写此proc以使其正常工作?也许首先执行SELECT并进行验证检查,如果确定,那么开始在事务中执行插入?

暂时坚持这个问题,以便看看是否有更有效的方法来确定是否可以避免这个问题?

以下是proc和exec:

exec SupportAudit.BI.CreateMCCInvoiceReversal 'ABCD/000', 29923, 'ABC', 1
USE [SupportAudit]
GO
/****** Object:  StoredProcedure [BI].[CreateMCCInvoiceReversal]    Script Date: 29/08/2016 07:23:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [BI].[CreateMCCInvoiceReversal]
(

    @InvoiceNumber      varchar(255),
    @InvoiceID          int,
    @DocType            varchar(15),
    @TaskLogid          int

)

AS

BEGIN

SET NOCOUNT ON;
SET XACT_ABORT ON;

declare @OutputList         [Core].[RollbackOutputList];
declare @procname           sysname; 


Set @procname = OBJECT_NAME(@@ProcID)


BEGIN TRY

BEGIN TRAN MCCInvoiceReversal

        INSERT INTO [Jet2Fees].Discount.Invoice
                (
                InvoiceNumber,
                DocType,
                )

        OUTPUT '[Jet2Fees].Discount.Invoice', 'InvoiceID', inserted.InvoiceId,
                                Core.insXMLFragment('InvoiceId')+Core.addnlXMLFragment('InvoiceId', inserted.InvoiceId)


            INTO @OutputList

            SELECT CONCAT(@InvoiceNumber, cast(InvoiceID as varchar(50))),
                    @DocType,

            FROM Jet2Fees.Discount.Invoice
            WHERE InvoiceId = @InvoiceId

    -- see if invoice number already exisits
if exists (select 1 from Jet2Fees.Discount.Invoice where InvoiceNumber = CONCAT(@InvoiceNumber, cast(@InvoiceID as varchar(50))))
            BEGIN;
                ROLLBACK TRAN MCCInvoiceReversal
                set @errormsg = 'Invoice Number already exists';
                THROW 99999, @errormsg, 1
            END;

exec Core.insertRollbackXML @outputList, @TaskLogid, @procname

COMMIT TRANSACTION MCCInvoiceReversal


END TRY

1 个答案:

答案 0 :(得分:2)

修改您的TRY块,如下所示..

BEGIN TRY


IF exists (select 1 from Jet2Fees.Discount.Invoice where InvoiceNumber = CONCAT(@InvoiceNumber, cast(@InvoiceID as varchar(50))))
            BEGIN;
                set @errormsg = 'Invoice Number already exists';
                THROW 99999, @errormsg, 1
            END
ELSE 

     BEGIN
        INSERT INTO [Jet2Fees].Discount.Invoice
                (
                InvoiceNumber,
                DocType
                )

        OUTPUT '[Jet2Fees].Discount.Invoice', 'InvoiceID', inserted.InvoiceId,
                                Core.insXMLFragment('InvoiceId')+Core.addnlXMLFragment('InvoiceId', inserted.InvoiceId)


            INTO @OutputList

            SELECT CONCAT(@InvoiceNumber, cast(InvoiceID as varchar(50))),
                    @DocType
            FROM Jet2Fees.Discount.Invoice
            WHERE InvoiceId = @InvoiceId
          END

END TRY