如何在/之前检查一下

时间:2016-08-31 07:19:45

标签: sql sql-server tsql

我在下面有一个exec proc,它会查找发票号,ID和文档类型:

exec SupportAudit.BI.CreateMCCInvoiceReversal 'APCCI/000', 29923, 'APCCI'

现在,作为示例的文档类型(APCCI)将始终是/之前的发票编号的一部分(作为示例APCCI)。

我的问题是如何编码检查以声明如果@invoiceNumber中的'/'之前的任何内容不等于@DocType,那么抛出异常?

以下是proc本身:

ALTER Procedure [BI].[CreateMCCInvoiceReversal]
(

    @InvoiceNumber      varchar(255),
    @InvoiceId          int,
    @DocType            varchar(15)

)
AS
BEGIN

SET NOCOUNT ON;
SET XACT_ABORT ON;

declare @errormsg           varchar(250);
declare @OutputList         [Core].[RollbackOutputList];
declare @CountRows          int;

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 @InvoiceNumber  +  cast(InvoiceID as varchar),
                @DocType
            FROM Jet2Fees.Discount.Invoice
            WHERE InvoiceId = @InvoiceId

2 个答案:

答案 0 :(得分:1)

IF (@invoiceNumber NOT LIKE  @DocType +'/%') RAISERROR('Error Message',1,1);

答案 1 :(得分:0)

在插入语句之前添加匹配条件..

IF ( SUBSTRING(@InvoiceNumber,1,CHARINDEX('/',@InvoiceNumber)-1)) != @DocType

SELECT 'ERROR : Not Matching'

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 @InvoiceNumber  +  cast(InvoiceID as varchar),
                @DocType
            FROM Jet2Fees.Discount.Invoice
            WHERE InvoiceId = @InvoiceId
END