2014-09-26 115 views
0

我想插入行,如果它們不存在(對於特定值)在此表中。SQL查詢:插入,如果不是已經存在從2表

我的表,其中我插入是insertTable:

date (date) 
created (datetime) 
category (varchar) 
companyId (int) 
price (decimal 6,3) 

我從內選擇我行的兩個表之間做加盟:

declare @currentDateTime datetime = getDate() 
declare @currentDate date = getDate() 
INSERT INTO insertTable (date, created, category, companyId, price) 
SELECT @currentDate, @currentDateTime, '30 Day', company.companyId, product.price 
FROM product 
INNER JOIN company 
ON product.companyid = company.companyid 
WHERE product.price >= 0.31 AND ... other conditions on company fields ... 
AND NOT EXISTS(
     SELECT * FROM insertTable WHERE insertTable.price = product.price 
        AND insertTable.date = @currentDateTime 
        AND insertTable.companyid = product.companyid 
        AND LTRIM(RTRIM(insertTable.category)) = '30 Day' 
    ) 

什麼我做錯了嗎?

謝謝

- 戈登Linoff後編輯評論。 我在insertTable中插入了select的結果。 我的問題是,我得到insertTable中的重複。

如果insertTable有

2014-09-26 | 2014-09-26 02:25:00 | 30 Day | 32650 | 0.600 

我的選擇將返回像

2014-09-26 | 2014-09-26 02:36:00 | 30 Day | 32650 | 0.600 

東西但是我已經有companyID和價格在插入表

+0

我有一種感覺,我失去了一些東西在我的崗位......我編輯解釋我的問題 – Greg 2014-09-26 18:29:43

+0

你知道[ **'MERGE' **](http://msdn.microsoft.com/en-us/library/bb510625.aspx)?聽起來像是其中之一的好例子。來自MSDN:_「根據與源表的連接結果對目標表執行操作。例如,您可以通過在一個表中插入,更新或刪除行來同步兩個表,並根據另一個表中的差異進行同步。「_ – stakx 2014-09-26 19:15:02

+0

@stakx謝謝stakx。Rajesh提出了我不知道的解決方案。謝謝 – Greg 2014-09-26 19:24:15

回答

1

您的問題聲明不能準確
費率未定義

你缺少創建
和您比較日期@currentDateTime

SELECT @currentDate, @currentDateTime, '30 Day' 
    , company.companyId, product.price 
    FROM product 
    JOIN company 
    ON product.companyid = company.companyid 
    and product.price >= 0.31 AND ... other conditions on company fields ... 
AND NOT EXISTS(
       SELECT * 
       FROM insertTable 
       WHERE insertTable.date = @currentDate 
       --AND insertTable.created = @currentDateTime 
        AND insertTable.price = product.price 
        AND insertTable.companyid = product.companyid 
        AND LTRIM(RTRIM(insertTable.category)) = '30 Day' 
       ) 
+0

我不介意通過創建進行比較,因爲我已經按日期進行了比較。創建日期和日期共享同一日期,但創建的時間不同。不過,您將日期與@currentDateTime進行比較是正確的 – Greg 2014-09-26 19:01:57

0

我不認爲你可以這樣使用EXISTS函數。我想你可能需要對insertTable表執行一個LEFT JOIN,然後在你的WHERE子句中添加一個「iT.Price IS NULL」。 (這是假設的價格是從來沒有真正對你insertTable NULL)。

1

我認爲你必須修改你的子查詢中NOT EXISTS@currentDateTime變化@currentDaterate.companyid變化company.companyid(因爲當插入insertTable.date得到的@currentDateinsertTable.companyid價值得到的價值company.companyid):

... 
AND NOT EXISTS(
     SELECT * FROM insertTable WHERE insertTable.price = product.price 
        AND insertTable.date = @currentDate 
        AND insertTable.companyid = company.companyid 
        AND LTRIM(RTRIM(insertTable.category)) = '30 Day' 
    ) 
+0

感謝Rimas。是的rate.company我的意思是company.companyid。這是一個輸入錯誤,但currentdatetime到currentdate是問題。謝謝! – Greg 2014-09-26 18:59:25

0

您需要關聯的子查詢,而不是子查詢中的連接。

它甚至更好地使用MERGE聲明http://msdn.microsoft.com/en-us/library/bb510625.aspx

declare @currentDateTime datetime = getDate() 
declare @currentDate date = getDate() 
INSERT INTO insertTable (date, created, category, companyId, price) 
SELECT @currentDate, @currentDateTime, '30 Day', company.companyId, product.price 
FROM product 
INNER JOIN company 
ON product.companyid = company.companyid 
WHERE product.price >= 0.31 AND ... other conditions on company fields ... 
AND NOT EXISTS(
     SELECT 1 FROM insertTable WHERE insertTable.price = product.price 
        AND insertTable.date = @currentDate 
        AND insertTable.companyid = company.companyid 
        AND LTRIM(RTRIM(insertTable.category)) = '30 Day' 
    ) 

編輯:

你可以做同樣的MERGE像下面

MERGE insertTable as target 
USING (SELECT @currentDate, 
      @currentDateTime, 
       '30 Day', 
       company.companyId, 
       product.price 
     FROM INNER JOIN company 
    ON product.companyid = company.companyid 
    WHERE product.price >= 0.31 AND ... other conditions on company fields ... 
) as source (currentDate, currentDateTime, category, companyid, price) 
ON target.price = source.price 
AND target.date = source.currentDate 
and target.companyid = source.companyid 
and LTRIM(RTRIM(target.category)) = '30 Day' 
WHEN NOT MATCHED THEN 
    INSERT (date, created, category, companyId, price) 
    VALUES (source.currentDate, source.currentDatetime, source.category, source.companyId, source.price) 
+0

我不知道如何實現該MERGE語句。我不熟悉MERGE,而且我也沒有看到它在你的答案中使用。 – Greg 2014-09-26 19:04:27

+0

@Greg,用MERGE語句更新。你可以根據條件輕鬆更新或插入合併。 – radar 2014-09-26 19:14:31

+0

感謝您填寫您的答案。這似乎適合我的情況。由於我已經編寫了我的查詢而不存在,我可能會堅持這一點。除非這樣做會造成很大的不同ehow? – Greg 2014-09-26 19:22:34

相關問題