简体   繁体   English

将行从表A复制到不存在键的表B

[英]Copy Row From Table A to Table B Where Key Doesn't Exist

I have this query: 我有这个查询:

INSERT INTO Master (Case)
SELECT Case
FROM Extract
WHERE (NOT Exists (SELECT 1
                  FROM Extract
                  WHERE Master.Case = Extract.Case))
GROUP BY Case;

I have two tables, Master and Extract, both have Case as their unique key. 我有两个表Master和Extract,它们都以Case为唯一键。

I want to copy all rows from Extract to Master that don't appear in Master, so any row with a Case number that isn't in Master. 我想将所有未提取的行从“提取”复制到“主”中,因此任何具有“案例号”的行都不在“主”中。

Hope that makes sense. 希望有道理。

When I run my query it says Enter Parameter Value for Master.Case 当我运行查询时,它说输入Master.Case的参数值

What am I doing wrong? 我究竟做错了什么?

Try to use column name in bracket [] ie [Case]. 尝试在方括号[]中使用列名,即[Case]。 because its reserved word in sql for CASE WHEN. 因为它在CASE WHEN中在sql中是保留字。

            insert into .Master([Case]) 
            select distinct [Case] from Extract where [Case] NOT IN(select [Case] from Master);

            OR

            insert into Master([Case]) 
            select distinct [Case] from Extract t1 where NOT EXISTS(select [Case] from Master t2 where t1.[Case]=t2.[Case]);
INSERT INTO [Master] ([Case])
SELECT [Case]
FROM [Extract]
WHERE NOT Exists (SELECT 1 FROM [Extract] WHERE [dbo].[Master].[Case] = [dbo]. 
[Extract].[Case])
GROUP BY [Case]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果 Postgresql 的父表中不存在外键,则忽略复制(读取)该行 - Ignore copy(reading) the row if the foreign key doesn't exist in the parent table in Postgresql 在存在A和B但不存在C的表中选择一行 - Select a row in a table where A and B exist but not C 如果当前表中不存在,则从其他表中选择行 - Select Row from Other Table if Doesn't Exist in Current Table 从表a插入表b,其中b中的记录不存在 - Insert from table a to table b where records in a don't exist in b 仅当目标表中不存在该行时,如何将一个表中的行复制到另一个表中 - How to copy row in one table to another only when that row doesn't exist in target table 从表A中找到缺失记录表B中表C中不存在它们 - Find Missing Records From Table A For Table B Where They Don't Exist in Table C 从表A中获取表B中没有匹配行的所有行 - Get all rows from table A where no matching row exist in table B 将值从table_a复制/更新到table_b的另一行,其中table_a.id = table_b.id AND table_a.name = table_b.name - Copy/Update values from table_a to another row of table_b WHERE table_a.id = table_b.id AND table_a.name = table_b.name 如果其他表中的值不存在,如何插入一行? - How to insert a row if the value from other table doesn't exist? 如果某行不存在,则从另一张表中获取该行 - if a row doesn't exist fetch it from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM