2013-07-01 26 views
-4

如何僅顯示no#'s哪些不存在於給定列表no#s的表格中。編號是否存在於表格中

如果no#不存在,則將該no#插入表中。

+3

歡迎來到Stack Overflow。很難理解你在問什麼。請提供您嘗試過的代碼片段,您可能收到的錯誤消息,輸入與期望輸出以及您感覺更好的其他任何內容來解釋您的問題。 –

回答

1

here是一個演示。基本上創建一個您要檢查的數字列表的表格,然後使用帶有left joininsert-select查詢插入它們,如果它們不存在。

DECLARE @current TABLE (n INT) 

DECLARE @numbers TABLE (n INT) 

INSERT INTO @current VALUES 
(1),(2),(3),(4),(5),(6),(7),(8),(9) 

INSERT INTO @numbers VALUES 
(1),(10),(3),(11),(5),(12),(7),(13),(9) 

-- insert from source table 
-- where it does not exist 
-- in destination table 
INSERT INTO @current 
SELECT n.n 
FROM @numbers n 
LEFT JOIN @current c ON n.n = c.n 
WHERE c.n IS NULL 

SELECT n FROM @current 
相關問題