SQL查询:如果两个表中尚未存在则插入

时间:2014-09-26 18:21:53

标签: sql tsql

我正在尝试插入行,如果它们在此表中不存在(对于特定值)。

我插入的表格是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'
    )

我做错了什么?

由于

- 在Gordon 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和price

4 个答案:

答案 0 :(得分:1)

我认为您必须在NOT EXISTS中修改子查询:@currentDateTime更改为@currentDaterate.companyid更改为company.companyid(因为插入{{1}时} }获取insertTable.date的值,@currentDate获取值insertTable.companyid):

company.companyid

答案 1 :(得分: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'
               )

答案 2 :(得分:0)

我认为你不能用这种方式使用EXISTS功能。我想你可能需要对insertTable表进行LEFT JOIN,然后添加一个" iT.Price IS NULL"你的WHERE子句。 (这假设您的insertTable上的Price实际上从不为NULL。)

答案 3 :(得分: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)