我的表zadavatel有以下结构
当我对新数据运行查询时,我得到了zadavatel字段的一些新条目。这基本上获得了前表中不存在的新zadavatel条目
select distinct zadavatel from [dbo].[DATA_2016_CZ]
where zadavatel not in (select zadavatel from zadavatel)
我需要在此表中输入此条目,字段广告商应为此新条目准确复制zadavatel字段,而NOTE的值应为2.
如果查询的输出是这样的
然后我想添加到我的表
zadavatel advertiser note
Al Namura Al Namura 2
所有条目的等等
您能否就查询提出建议?
感谢
答案 0 :(得分:4)
您可以使用insert into..select
执行此操作。
insert into zadavatel (zadavatel, advertiser, note)
select distinct zadavatel , zadavatel, 2
from [dbo].[DATA_2016_CZ]
where zadavatel not in (select zadavatel from zadavatel)
或
insert into zadavatel (zadavatel, advertiser, note)
select distinct zadavatel , zadavatel, 2
from [dbo].[DATA_2016_CZ] d
where not exists (select 1 from zadavatel where zadavatel=d.zadavatel)