SQL嵌套的IF-ELSE语句

时间:2015-08-13 23:01:00

标签: sql-server if-statement case

好吧,我正式感到茫然。我正在尝试创建一个遍历每个后续检查的过程并执行相应的查询。

但是,当我使用一个/两个/所有参数执行该过程时,它总是被最终的ELSE捕获并吐出“全部覆盖”结果集。 WHY ???

我怀疑它可能是: - 如果/其他语法 - 使用括号 - 开始/结束关键字 - 很多IF条件?

非常感谢任何帮助!

奖励积分:我可以使用WHERE子句中的搜索CASE对其进行优化吗?我知道我可以真正削减这些代码 - 但我真的很好奇为什么这不起作用。

CREATE PROC spBalanceRange
    @VendorVar varchar(50) = NULL,
    @BalanceMax money = NULL,
    @BalanceMin money = NULL

AS
    IF
    (
        @VendorVar != NULL
        AND @BalanceMin != NULL             
        AND @BalanceMax != NULL
    )

    BEGIN
        SELECT VendorName, InvoiceNumber, InvoiceTotal - (PaymentTotal + CreditTotal) AS BalanceDue
        FROM Invoices JOIN Vendors
            ON Invoices.VendorID = Vendors.VendorID
        WHERE
            VendorName = @VendorVar
            AND InvoiceTotal - (PaymentTotal + CreditTotal) >= @BalanceMin
            AND InvoiceTotal - (PaymentTotal + CreditTotal) <= @BalanceMax
    END
ELSE IF
    (
        @VendorVar != NULL
        AND @BalanceMin = NULL
        AND
            (
                @BalanceMax = NULL
                OR @BalanceMax = 0
            )
    )
    BEGIN
        SELECT VendorName, InvoiceNumber, InvoiceTotal - (PaymentTotal + CreditTotal) AS BalanceDue
        FROM Invoices JOIN Vendors
            ON Invoices.VendorID = Vendors.VendorID
        WHERE
            VendorName = @VendorVar
            AND InvoiceTotal - (PaymentTotal + CreditTotal) > 0
    END
ELSE
    BEGIN
        SELECT VendorName, InvoiceNumber, InvoiceTotal - (PaymentTotal + CreditTotal) AS BalanceDue
        FROM Invoices JOIN Vendors
            ON Invoices.VendorID = Vendors.VendorID
        WHERE
            InvoiceTotal - (PaymentTotal + CreditTotal) > 0
    END
;

4 个答案:

答案 0 :(得分:4)

理想情况下,你应该使用动态sql来避免像这样的查询中的问题进行参数嗅探,动态sql的解决方案看起来像这样......

CREATE PROC spBalanceRange
    @VendorVar varchar(50) = NULL,
    @BalanceMax money = NULL,
    @BalanceMin money = NULL

AS
BEGIN
  SET NOCOUNT ON;
  DECLARE @Sql NVARCHAR(MAX);



SET @Sql  = N'SELECT VendorName, InvoiceNumber, InvoiceTotal - (PaymentTotal + CreditTotal) AS BalanceDue
        FROM Invoices 
        INNER JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
        WHERE '
        + CASE 
        WHEN         (@VendorVar IS NOT NULL 
                      AND @BalanceMin IS NOT NULL             
                      AND @BalanceMax IS NOT NULL)
         THEN N' VendorName = @VendorVar
            AND InvoiceTotal - (PaymentTotal + CreditTotal) >= @BalanceMin
            AND InvoiceTotal - (PaymentTotal + CreditTotal) <= @BalanceMax' 

        WHEN          ( @VendorVar IS NOT NULL
                        AND @BalanceMin IS NULL
                        AND (@BalanceMax IS NULL OR @BalanceMax = 0))
         THEN N' VendorName = @VendorVar
               AND InvoiceTotal - (PaymentTotal + CreditTotal) > 0'
        ELSE N' InvoiceTotal - (PaymentTotal + CreditTotal) > 0' END

Exec sp_executesql @Sql 
                  ,N'@VendorVar varchar(50),@BalanceMax money,@BalanceMin money'
                  ,@VendorVar 
                  ,@BalanceMax 
                  ,@BalanceMin 

END

答案 1 :(得分:1)

我认为这是因为你将它们与==的null进行比较。使用t-sql,您始终需要使用is运算符。

尝试

x is not null

x is null

代替。

答案 2 :(得分:1)

我不在Windows机器附近所以这可能需要调整:

CREATE PROC spBalanceRange
    @VendorVar varchar(50) = NULL,
    @BalanceMax money = NULL,
    @BalanceMin money = NULL

AS
SELECT VendorName, InvoiceNumber, InvoiceTotal - (PaymentTotal + CreditTotal) AS BalanceDue
FROM Invoices JOIN Vendors
ON Invoices.VendorID = Vendors.VendorID
WHERE
case when @VendorVar is null then true else vendorName=@vendorVar end
AND InvoiceTotal - (PaymentTotal + CreditTotal) >= coalesce(@BalanceMin,0)
AND case when @BalanceMax is null then true else InvoiceTotal - (PaymentTotal + CreditTotal) <= coalesce(@BalanceMax, 1e15) end;

答案 3 :(得分:1)

使用案例陈述的另一种方法可能会显示您想要的结果:

CREATE PROCEDURE spBalanceRange

@VendorVar varchar(50) = NULL,
@BalanceMax money = NULL,
@BalanceMin money = NULL

AS
BEGIN
SELECT 

b.VendorName, a.InvoiceNumber, 

case 
when InvoiceTotal - (PaymentTotal + CreditTotal) between @BalanceMax and     @BalanceMin
then InvoiceTotal - (PaymentTotal + CreditTotal) 
else 0
end BalanceDue
FROM Invoices a
JOIN Vendors b
ON a.VendorID = b.VendorID
WHERE
b.VendorName = @VendorVar 
and InvoiceTotal - (PaymentTotal + CreditTotal) between @BalanceMax and @BalanceMin
END

希望我理解你正在努力做到的事情。