--测试数据ta为源数据, tb为目标数据
create table ta(id int, name varchar(10),
value int, groupid int)
create table tb(id int, name varchar(10),
value int, groupid int)
insert ta select 4,'A',4, 2 union all select 6,'D',6,2
union all select 7,'E',7, 2
insert tb select 1,'A',1, 1 union all select 2,'B',2,1
union all select 3,'C',3, 1 union all select 4,'A',4,2
union all select 5,'B',5, 2
--主要部分
--注意源数据中,也要记得排除重复记录
insert tb
select *
from ta as t1
where not exists(select 1 from tb
where tb.name=t1.name and tb.groupid=t1.groupid)
and exists(select top 1 id from ta
where name=t1.name and groupid=t1.groupid
and id=t1.id)select * from tb
--清除
drop table ta
drop table tb
create table ta(id int, name varchar(10),
value int, groupid int)
create table tb(id int, name varchar(10),
value int, groupid int)
insert ta select 4,'A',4, 2 union all select 6,'D',6,2
union all select 7,'E',7, 2
insert tb select 1,'A',1, 1 union all select 2,'B',2,1
union all select 3,'C',3, 1 union all select 4,'A',4,2
union all select 5,'B',5, 2
--主要部分
--注意源数据中,也要记得排除重复记录
insert tb
select *
from ta as t1
where not exists(select 1 from tb
where tb.name=t1.name and tb.groupid=t1.groupid)
and exists(select top 1 id from ta
where name=t1.name and groupid=t1.groupid
and id=t1.id)select * from tb
--清除
drop table ta
drop table tb
解决方案 »
- 我想通过编程知道一个ACCESS表中是否有数据,如果没有就insert into 插入数据,如果有就update 修改,如何做到?
- 触发器疑问?如果存在记录,更新相应栏位时出错,提示记录被抢先修改,这是为何?
- 高分求教sql语句
- 高手请指教:循环递增n条数据
- 急救,我的sql server2000 出问题了,打开企业管理器的时候提示MMC无法打开选择文件。
- 求助~~关于sql 语句...
- 请教4个表的select 非我能力所为阿
- 进来,看一看,就散分。
- 关于课程表的问题,一点思路都没有...
- 从一个数据库 import数据 过来,为什么ID列的自动递增属性失效了?
- 一個疑難問題!
- 如何使用execute中声明的变量
select * from ta
where id in(select min(ta.id)
from ta left join tb
on ta.name=tb.name and ta.groupid=tb.groupid
where tb.id is null
group by ta.name, ta.groupid)
select * from ta a
where not exists (select 1 from tb where id=a.id and GroupId=a.GroupId )
where id not in
(
select min(id) from tableName group by datavar
)