2016-03-15 69 views
0

enter image description here填充柱通过连接T1和T2

我需要通过在CountryItemcode接合表T1和T2以填充柱ISkeyProduct(参见表附件)。列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) 
+0

但与此代码我得到一系列的“1”和NULL。 – Ramaq

+0

您的样本中哪一个是T1,哪一个是T2? \t请阅读[**如何提问**](http://stackoverflow.com/help/how-to-ask) \t \t这里是[** START **]( http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/)了解如何提高您的问题质量并获得更好的答案。 –

+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应设置为的记录。

相关问题