在where子句中使用ISNULL

时间:2015-10-15 19:09:51

标签: sql-server-2008

DECLARE @use10percent INT

SELECT 
    @use10percent = COUNT(*) 
FROM 
    [dbo].[CUSTOMER_ORDER] o
INNER JOIN 
    [dbo].[CUSTOMER] c ON c.ID = o.CUSTOMER_ID
INNER JOIN 
    [dbo].[CUST_ADDRESS] ca ON ca.CUSTOMER_ID = o.CUSTOMER_ID 
                            AND ca.ADDR_NO = o.[SHIP_TO_ADDR_NO]
WHERE 
    o.[ID] = @CUST_ORDER_ID
    AND (isnull(ca.STATE, c.STATE) IN ('AB','BC','MB','MT','NB','NS','ON','PQ','QC','SK','TX','NY'))

/**/
UPDATE SHIPPER_LINE
SET ACT_FREIGHT = CONVERT(DECIMAL(10, 2),
                     (CASE
                         WHEN CAST(ShipWeight AS Float) >= 400 
                           THEN CAST(Rate AS Float) * 1.06
                         WHEN CAST(ShipWeight AS Float) < 400 
                           THEN CASE
                                    WHEN @use10percent = 0 
                                      THEN CAST(Rate AS Float) * 1.20
                                    ELSE CAST(Rate AS Float) * 1.10 --New rate for Canada, TX, and NY
                                END
                         ELSE CAST(Rate AS Float)
                       END))

此触发器的目标是在重量低于400磅且目的地州为纽约州,德克萨斯州或整个加拿大时,将运费增加10%。如果客户使用掉落地址(ca.state)并且我获得10%的增长,它就可以工作。但如果客户是买方和目的地,则(ca.state)为空并且需要使用(c.state)。如果(ca.state)为null且(c.state)为NY,TX或Canada,则上面的代码会添加完整的20%。

如何修复where子句以正确过滤?

1 个答案:

答案 0 :(得分:0)

关于STATE检查的WHERE部分似乎很好: isnull(ca.STATE,c.STATE)IN('AB','BC','MB','MT','NB','NS','ON','PQ','QC','SK ”, 'TX', 'NY') 如果它增加20%意味着@ use10percent为零,你确定JOIN和数据进入表吗?您是否尝试删除COUNT并观察SELECT结果?