求SQL高手!业务需求写的很明白,希望给一个SQL语句,关于数据插入的

wdzczy 2010-07-23 09:25:52
说明:表A与表B完全结构类型一样!字段一样!但是数据不一样,表A的多,表B的少!【再说一次是表A多,表B少】。
业务需求:我现在要表A向表B插入表B没有的那部分(即:多插入少),条件是:姓名如果重复就不添加!但是没添加成功的要显示出来(怎么显示?)便于我整理~。

表名:A,B
字段名:FNAME,FPerID,FAdds,FTel

表A数据:'张三','210321554982456','人民大街8821号','13844512828'
表A数据:'李四','211235549824563','人民大街82821号','13844549828'
表A数据:'于洋','215645316456145','人民大街21号','138445498984'

表A数据:'王刚','454038421654231','人民大街58号','138445498985'
表A数据:'小红','486132046165764','人民大街12321号','138445498982'
表A数据:'陈志刚','14523475123154','人民大街21456号','138445498988'

---------------------我是分割线(仔细看表A与表B数据不完全一样!)-----------------------------


表B数据:'张三','210321554982456','人民大街8821号','13844512828'
表B数据:'李四','211235549824563','人民大街82821号','13844549828'
表B数据:'于洋','215645316456145','人民大街21号','138445498984'

表B数据:'赵薇','210321554382456','前进大街8821号','138441344828'
表B数据:'林心如','21122549824563','北化大街82821号','13849814982


------------------------------------------

我现在想让表B的数据变为

新的表B数据:'张三','210321554982456','人民大街8821号','13844512828'
新的表B数据:'李四','211235549824563','人民大街82821号','13844549828'
新的表B数据:'于洋','215645316456145','人民大街21号','138445498984'

新的表B数据:'赵薇','210321554382456','前进大街8821号','138441344828'
新的表B数据:'林心如','21122549824563','北化大街82821号','13849814982
新的表B数据:'王刚','454038421654231','人民大街58号','138445498985'
新的表B数据:'小红','486132046165764','人民大街12321号','138445498982'
新的表B数据:'陈志刚','14523475123154','人民大街21456号','138445498988'

---------------------------------
不难看出
张三,李四,于洋 三个人没有插入成功,因为表B内已经有了,怎么把没插入成功的这三个人倒出来?

----------------------------------------

这仅仅是我举的一个例子,在学习中遇到的问题

最好根据我的数据与需求,写一个例句就好了

谢谢各位了,不知小弟写的可否清晰明白,如有疑问可以留言,在线等

...全文
101 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wdzczy 2010-08-02
  • 打赏
  • 举报
回复
插入
insert into B
select * from A
where not exists(select 1 from B as T where T.name=A.name)

顯示
select * from A
where exists(select 1 from B as T where T.name=A.name)

这个正确,呵呵~ 其他的也正确~

xman_78tom 2010-07-23
  • 打赏
  • 举报
回复

if OBJECT_ID('tempdb..#1') is not null
drop table #1;
go
create table #1 (id int, c char(1));
go
insert into #1 values(1,'a');
insert into #1 values(2,'a');
insert into #1 values(3,'a');
go

if OBJECT_ID('tempdb..#2') is not null
drop table #2;
go
create table #2 (id int, c char(1));
go
insert into #2 values(1,'a');
insert into #2 values(2,'a');
insert into #2 values(4,'a');
go

-- 方法 1
-- 查询 #1 和 #2 中重复的
select id,c from #1 where id in (select id from #2);
-- 插入不重复的
insert into #2 select id,c from #1 where id not in (select id from #2);


-- 方法 2(SQL Server 2008 适用)
-- 用 output 子句输出重复的($action=update)
merge into #2 t using #1 s
on t.id=s.id
when matched then
update set t.c=t.c
when not matched then
insert values(s.id,s.c)
output $action,deleted.*;

select * from #1;
select * from #2;
wdzczy 2010-07-23
  • 打赏
  • 举报
回复
请问应该是等号吗?=
不应该是非等吗?!=


[Quote=引用 6 楼 playwarcraft 的回复:]
插入
insert into B
select * from A
where not exists(select 1 from B as T where T.name=A.name)

顯示
select * from A
where exists(select 1 from B as T where T.name=A.name)
[/Quote]
playwarcraft 2010-07-23
  • 打赏
  • 举报
回复
插入
insert into B
select * from A
where not exists(select 1 from B as T where T.name=A.name)

顯示
select * from A
where exists(select 1 from B as T where T.name=A.name)

wdzczy 2010-07-23
  • 打赏
  • 举报
回复
插入没成功的怎么显示出来啊?
SQL2000
[Quote=引用 3 楼 thinclient 的回复:]
插入的命令:
insert into 表B select * from 表A join 表B ON 表B.姓名!=表A.姓名
[/Quote]
wdzczy 2010-07-23
  • 打赏
  • 举报
回复
你没有仔细看!

[Quote=引用 1 楼 xuam 的回复:]
insert into B select * from A where not exists (select * from B)
[/Quote]
thinclient 2010-07-23
  • 打赏
  • 举报
回复
插入的命令:
insert into 表B select * from 表A join 表B ON 表B.姓名!=表A.姓名
华夏小卒 2010-07-23
  • 打赏
  • 举报
回复
insert into @tb select *from @ta where FNAME not in(select FNAME from @tb)
xuam 2010-07-23
  • 打赏
  • 举报
回复
insert into B select * from A where not exists (select * from B)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧