通过加入T1和T2填充列

时间:2016-03-15 14:31:01

标签: sql-server

enter image description here

我需要通过在ISkeyProductCountry上加入表格T1和T2来填充列Itemcode(请参阅表的附件)。列iskeyproduct中的最终值应为True或False,但不应为NULL。

我的代码是:

Update T1 
Set IsKeyProduct = Case 
                      When t2.ItemCode is not Null Then 1 Else 0 
                   End
From T1 
Left join T2 on t1.ShopCountry = t2.Country
             and t1.ItemCode = case when t2.ItemCode
Where 
    (t1.ItemCode is not Null and t2.ItemCode is not null )

1 个答案:

答案 0 :(得分:0)

你必须摆脱第二个谓词并修复JOIN ON条件:

UPDATE T1 
SET IsKeyProduct = CASE WHEN t2.ItemCode IS NOT NULL THEN 1 ELSE 0 END
FROM T1 
LEFT JOIN T2
    ON t1.ShopCountry = t2.Country AND t1.ItemCode = t2.ItemCode
WHERE t1.ItemCode IS NOT NULL

这个谓词:

t2.ItemCode is not null

过滤掉IsKeyProduct应设置为0的记录。