我有这样一个需求!
a 表 主键 a列 (int)(自动增长加1) b列 c列
b 表 主键 a列 (int)(自动增长加1) b列 c列 d列(比a表多出一列)这两张表中都有数据 (a表中可能有和b表的重复的数据 【包括主键也一样】 也可能没有重复数据【但是主键重复】)
把a表的数据导入到 b表中 (重复的数据替换【主键a列 和b列 c列数据一样】,追加数据【主键a列重复但后边 b列 c列不一样】,追加数据【主键a列 b列 c列都不一样】 )
导入次数为一次性导入,b表的d列为‘’
数据很多
dts 中不能实现 需要用sql语句来执行谢谢!!
a 表 主键 a列 (int)(自动增长加1) b列 c列
b 表 主键 a列 (int)(自动增长加1) b列 c列 d列(比a表多出一列)这两张表中都有数据 (a表中可能有和b表的重复的数据 【包括主键也一样】 也可能没有重复数据【但是主键重复】)
把a表的数据导入到 b表中 (重复的数据替换【主键a列 和b列 c列数据一样】,追加数据【主键a列重复但后边 b列 c列不一样】,追加数据【主键a列 b列 c列都不一样】 )
导入次数为一次性导入,b表的d列为‘’
数据很多
dts 中不能实现 需要用sql语句来执行谢谢!!
--相同的更新
update b
set b=b.b,c=b.c
from b inner join a on b.a=a.a
--不存在的插入
insert into b(a,b,c)
select a,b,c
from a
where not exists(select 1 from b where b.a=a.a)
as
set nocount on
--相同的更新
update b
set b=b.b,c=b.c
from b inner join a on b.a=a.a
--不存在的插入
insert into b(a,b,c)
select a,b,c
from a
where not exists(select 1 from b where b.a=a.a) go
insert into b from select a,b,c from a
更新b表的的列即可
insert b
select *,d=null --赋个null
from a where ID not in(select ID from b)update b
set b=a.b,c=a.c
from a
where
a.ID=b.ID
导入次数为一次性导入,b表的d列为‘’
-->>重复的数据替换【主键a列 和b列 c列数据一样】,这个就不用了吧,替换后还是一样的数据,就不用操作了追加数据【主键a列重复但后边 b列 c列不一样】,追加数据【主键a列 b列 c列都不一样】
->是不是可以这样理解,只要b,c不相同都要插入?
-->> insert into b(a,b,c)
select t.a,t.b,t.c
from a left join b on a.a=b.a
where b.a is null ->>插入b中没有的id
and (a.b<>b.b or a.c<>b.c)
a 表 主键 a列 (int)(自动增长加1) b列 c列
b 表 主键 a列 (int)(自动增长加1) b列 c列 d列(比a表多出一列)
把a表的数据导入到 b表中 (重复的数据替换【主键a列 和b列 c列数据一样】,追加数据【主键a列重复但后边 b列 c列不一样】,追加数据【主键a列 b列 c列都不一样】 )
导入次数为一次性导入,b表的d列为‘’
数据很多
判断a表 b表 的数据不同是根据是 b列 c列 的不同
如果a,b表中 b列 c列 相同就认为是一样的数据 执行覆盖
如果a,b表中 b列 c列不同 导入的时候为 追加 a中也有数据 b 中也有数据(d列始终是‘’)
a-->b 时候 b 表的主键可以重新设置
数据量很大 !~~~
create table A(name nchar(10),age int)
insert A
select 'zhangsan',18 union all
select 'lisi',19 union all
select 'wangwu',20 union all
select 'lvliu',22if object_id('B') is not null drop table B
create table B(name nchar(10),age int,sex nchar(10))
insert B
select 'wangwu',21,'male' union all
select 'lvliu',22,'female' union all
select 'caoqi',23,'female'insert B
select A.*,sex=null
from A
where not exists(select name,age from B where A.name=B.name and A.age=B.age)select * from B order by name
怎么做!~~
set age=A.age
from A join B
on A.name=B.name select * from B order by name