根据case ... when语句插入表中

时间:2015-10-06 04:45:15

标签: sql-server

这是我想要做的第一件事:

我有一个具有帐户ID的表(T1),并希望根据表T1中的帐户ID将标志插入另一个表(T2)。

第二

然后,我想更新标志[根据T1中存在的帐号ID]

我正在尝试这样:

Insert into T2 (...,flag)
select distinct ...,
case
When (insert into T2 from T1 where T1.accountID not in 
(select accountID   from t2)) then 'Y'
When (insert into T2 from T1 where T1.accountID in 
(select accountID from  t2)) then 'N'
from
T3 join T2

基本上,在CASE中,我试图获得标志(“Y”或“N”)并插入T2。注意:我需要计算标志,因为我在T1中没有标志列。

希望这是有道理的。

这是正确的做法吗?请帮忙。

1 个答案:

答案 0 :(得分:1)

我认为这就是你想要做的事情:

    Insert into T2 (FLAG)
    select distinct case
    When T1.accountID not in 
    (select accountID from t2)) then 'Y'
    ELSE 'N' END as someName
    from
    T3 join T2 on t2.somefield = t3.somefield

一旦我对您要做的事情有了更好的理解,我可以编辑答案以获得有关UPDATE语句的帮助。