2017-09-05 47 views
0

我想從表中選擇數據不存在於另一個表中,但它不工作,我認爲在語法上有一個錯誤WHERE NOT EXISTS請指教解決這個問題問題我想選擇數據不存在於另一個表中,但不工作

SELECT CHARACTER6 sales_no 
     , CHARACTER4 CUST_NO 
     , OCCURRENCE 
INTO QA_RES# 
FROM OPENQUERY(ORALINK_QA, 'select * from QA.QA_RESULTS 
           where PLAN_ID = ''32196''') 
WHERE NOT EXISTS (SELECT * 
        FROM CHECK_OCCURRENCE); 
+0

使用的列名,而不是'選擇*' – dbajtr

+0

請參閱[如何發佈在公共論壇上TSQL的問題(http://spaghettidba.com/2015/04/24/how-to-post -at-sql-question-on-a-public-forum /)和[如何提問](https://stackoverflow.com/help/how-to-ask)。 – SqlZim

+0

你的意思是_but但不是working_?你得到一個錯誤消息?請清楚。 – Sami

回答

0

如果你想插入新行唯一的,你可以考慮使用except像這樣:

select 
    CHARACTER6 as sales_no 
    , CHARACTER4 as CUST_NO 
    , OCCURRENCE 
INTO [QA_RES#] 
from openquery (ORALINK_QA, ' 
    select CHARACTER6, CHARACTER4, OCCURRENCE 
    from QA.QA_RESULTS 
    where PLAN_ID = ''32196''') 
except 
select CHARACTER6, CHARACTER4, OCCURRENCE 
from CHECK_OCCURRENCE 
+0

感謝您的快速支持 –

0

您需要定義(WHERE子句中的條件關係)的關係裏面的存在操作員查詢

SELECT t.CHARACTER6 AS sales_no 
     , t.CHARACTER4 AS CUST_NO 
     , t.OCCURRENCE 
     INTO QA_RES# 
FROM OPENQUERY(ORALINK_QA, 'select * from QA.QA_RESULTS 
          where PLAN_ID = ''32196''' 
       ) t 
WHERE NOT EXISTS ( SELECT * 
        FROM CHECK_OCCURRENCE c 
        WHERE t.SomeColumn = c.SomeColumn --<-- Condition to check 
        ); 
+0

感謝您的快速支持 –

相關問題