我不确定代码是否正确,我只是试图将数据插入到另一个表中,我不确定我是否可以在括号中使用两列where ([Event],[Event Start DateTime])
insert into CPEvents3 select * from CPEvents1 where ([Event],[Event Start DateTime]) not in
(select [Event], [Event Start DateTime] from CPEvents3)
谢谢,S
答案 0 :(得分:1)
这样我们就需要写
INSERT INTO CPEVENTS3
([Event],[Event Start DateTime])
select [Event],
[Event Start DateTime]
FROM CPEvents1 CP
where NOT EXISTS
(
SELECT [Event], [Event Start DateTime]
from CPEvents3
WHERE CP.[Event] = [Event])
OR
INSERT INTO CPEVENTS3
([Event],
[Event Start DateTime]
)
select [Event],[Event Start DateTime]FROM CPEvents1
EXCEPT
select [Event],[Event Start DateTime]FROM CPEvents3
答案 1 :(得分:0)
你也可以这样写
insert into CPEvents3
select * from CPEvents1 where
[Event] not in (select [Event] from CPEvents3)
and [Event Start DateTime] not in (select [Event Start DateTime] from CPEvents3)
答案 2 :(得分:0)
一种方法是使用outer join来查找出现在一个表中但不出现在另一个表中的记录。然后可以插入它们。
-- Add new records to CPEvents3.
INSERT INTO CPEvents3
(
[Event],
[Event Start DateTime]
)
SELECT
c1.[Event],
c1.[Event Start DateTime]
FROM
CPEvents1 AS c1
LEFT OUTER JOIN CPEvents3 AS c3 ON c3.[Event] = c1.[Event]
AND c3.[Event Start DateTime] = c1.[Event Start DateTime]
WHERE
c3.[Event] IS NULL
;
答案 3 :(得分:0)
尝试以下查询,它将正常工作
INSERT INTO CPEvents3
SELECT *
FROM CPEvents1 LEFT OUTER JOIN CPEvents3
ON CPEvents3.[Event]= CPEvents1.[Event]
AND CPEvents3.[Event Start DateTime] = CPEvents1.[Event Start DateTime]
WHERE CPEvents3.[Event] IS NULL
要选择存储在表中的数据在一个或多个列上使用比较而在另一个中找不到,您可以在这些列上使用左连接并设置第二个表中的一个连接列为空的条件。因此查询仅返回在第一个表中找到的数据而在另一个表中找不到
答案 4 :(得分:-1)
您无法一次比较两列的位置。您需要为每个列创建一个这样的条件:
insert into CPEvents3
select * from CPEvents1 c1 where
[Event] not in (select [Event] from CPEvents3) and
[Event Start DateTime] not in (select [Event Start DateTime] from CPEvents3)
最好在插入和选择列表中指定列。