0

I have this stored procedure that I want to use for my SAP crystal report.

SELECT
    t0.DocNum AS ReceiptNo, t2.DocEntry AS InvoiceNo, 
    t0.DocDate, t1.CheckAct,
    t1.CheckNum, t3.AcctName, 
    CASE 
        WHEN (t0.CardName IS NULL) THEN t0.Address
                                   ELSE t0.CardName
    END AS CustName,
    CASE 
        WHEN ((t1.CheckNum IS NULL OR t1.CheckNum = 0)
               AND t0.TrsfrRef IS NULL) THEN 'Cash'
        WHEN (t1.CheckNum > 0
               AND t0.TrsfrRef IS NULL) THEN 'Cheque'
        ELSE 'Transfer'
    END AS PayType, 
    CASE 
        WHEN (t1.CheckNum > 0 AND t0.TrsfrRef IS NULL) THEN t1.CheckSum
        ELSE t0.DocTotal
    END AS DocTotal
FROM
    ORCT t0
LEFT OUTER JOIN 
    RCT1 t1 ON t1.docnum = t0.docnum
LEFT OUTER JOIN 
    RCT2 t2 ON t0.DocEntry = t2.DocNum
LEFT OUTER JOIN 
    OACT t3 ON t1.CheckAct = t3.AcctCode
WHERE
    t0.DocDate BETWEEN @BeginDate AND @EndDate
    AND t0.Canceled = 'N'
    AND t3.AcctCode = @Account

On the last line, I have declared a variable @Account that filters results by account number. I want my report to work such that if an account is not selected(null), results of all account numbers are returned.

How do I achieve this?

Any help appreciated.

4

3 回答 3

3

It sounds like you want the last clause to be effectively omitted if @Account is null. If so, then try this:

... AND t0.Canceled = 'N' AND
(@Account IS NULL OR t3.AcctCode = @Account)
于 2012-06-25T08:02:47.377 回答
1

add to the end

or t3.AcctCode is null
于 2012-06-25T07:59:55.717 回答
1

You could use IF ELSE e.g.

IF @Account IS NULL
    BEGIN
        --Your Query without "AND t3.AcctCode = @Account"
    END
ELSE
    BEGIN
        -- Your Query
    END

You could also change your last line to:

AND (t3.AcctCode = @Account OR @Account IS NULL)
于 2012-06-25T08:06:53.883 回答