SQL查询:如果2个表中不存在,则插入
如果此表中不存在行(对于特定值),我将尝试插入这些行 我插入的表格是insertTable:SQL查询:如果2个表中不存在,则插入,sql,tsql,Sql,Tsql,如果此表中不存在行(对于特定值),我将尝试插入这些行 我插入的表格是insertTable: date (date) created (datetime) category (varchar) companyId (int) price (decimal 6,3) 我从两个表之间的内部联接中选择行,执行以下操作: declare @currentDateTime datetime = getDate() declare @currentDate date = getDate() INSERT
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中插入该选择的结果。
我的问题是,我在insertTable中得到了重复项
如果insertTable有
2014-09-26 | 2014-09-26 02:25:00 | 30 Day | 32650 | 0.600
我的select将返回如下内容
2014-09-26 | 2014-09-26 02:36:00 | 30 Day | 32650 | 0.600
但是,我在insert表中已经有了公司ID和价格,我认为您不能以这种方式使用EXISTS函数。我认为您可能需要对insertTable表进行左连接,然后在WHERE子句中添加一个“iT.Price IS NULL”。(这假设您的插入表上的Price实际上从不为空。)我认为您必须在
不存在中修改子查询:@currentDateTime
更改为@currentDate
和rate.companyid
更改为company.companyid
(因为当insertinsertTable.date
获取@currentDate
的值时,insertTable.companyid
获取companyid
的值时):
您需要一个相关的子查询,而不是子查询中的联接
最好使用MERGE语句
编辑:
您可以像下面这样使用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)
您的问题陈述不可能准确
费率未定义
您缺少已创建的
您正在将日期与@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'
)
我有一种感觉,我在我的帖子中遗漏了一些东西……我编辑来解释我的问题,你知道吗?听起来像是一个很好的例子。来自MSDN:“执行[…]基于与源表联接的结果对目标表执行的操作。例如,您可以通过根据在另一个表中发现的差异插入、更新或删除一个表中的行来同步两个表。”@stakx感谢stakx。Rajesh提出了我不知道的解决方案。这在本页讨论。谢谢Hanks Rimas。是的rate.company我指的是company.companyid。键入它是个错误。但是currentdatetime到currentdate是个问题。谢谢!我不介意按created进行比较,因为我已经按日期进行了比较。created和date共享相同的日期,只是created指定了时间。不过,您将日期与@CurrentDateTime进行比较是正确的。我不确定如何实现该合并语句。我对MERGE一点也不熟悉。我也不认为您的答案中使用了它。@Greg,使用MERGE语句进行了更新。您可以根据条件进行更新或插入使用“合并”可以轻松完成。感谢您完成您的回答。这似乎适合我的情况。因为我已经用“不存在”编码了我的查询,所以我可能会坚持使用它。除非这会带来很大的不同?@Greg,当然,也许您可以尝试在将来的SQL中使用“合并”。
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)
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'
)