2015-10-06 48 views
1

這是我想要做什麼1:插入到表中根據情況......當聲明

我有一個有帳戶ID表(T1),並想插入一個標誌爲基於另一個表(T2)在表T1中是否存在賬戶ID。

第二:

而且,後來我也想更新標誌[基於存在於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

你想要做什麼與你的中間插入.... – logixologist

回答

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語句的幫助。