SQL中建2个表tb1,tb2。tb1和tb2的字段都为“编号,名称,规格”,tb2中的数据有与tb1重复的部分,怎么把重复的部分挑出,并且把不重复的接着tb1的编号的最后一个往下继续编号。就是查找“tb1.名称=tb2.名称 and tb1.规格=tb2.规格”的为重复的,怎么实现tb2中与tb1不重复的部分接着tb1的编号往下排?就是tb1中的编号最后一个是“009”的话,tb2的编号字段就接着“010”往下接。谢谢帮忙!
解决方案 »
- 求解SQL语句
- 关于SQl2008数据库在win7 上安装出现的问题
- int in string的问题
- 关于Sql server NextRecordset()出错的问题
- 求解:sqlserver2005集群下复制,发布执行时说系统用户名权限不够
- 新手请教.....
- 数据库导入导出的时候,其中的存储过程为什么不能够随着表,视图导入导出?
- 求教关于exists问题
- 救命~~,请问谁有在客户端成功注册sqldmo.dll的经历,请进(打包安装)!!!!!!
- mysql转存sql脚本的时候怎么取消外键
- ====>>>奇怪问题:关于SQL数据库连接不稳定!!!!!!!!!!!!
- 请问一些sql代码书写格式的问题
drop table tb1
if object_id('tb2') is not null
drop table tb2
go
create table tb1(编号 int identity,名称 varchar(10),规格 varchar(10))
insert into tb1
select '名称1','规格1' union all
select '名称2','规格1' union all
select '名称3','规格1' union all
select '名称4','规格1' union all
select '名称5','规格2' union all
select '名称6','规格2' union all
select '名称7','规格2' union all
select '名称8','规格2'
create table tb2(编号 int identity,名称 varchar(10),规格 varchar(10))
insert into tb2
select '名称10','规格1' union all
select '名称20','规格1' union all
select '名称3','规格1' union all
select '名称4','规格1' union all
select '名称50','规格2' union all
select '名称60','规格2' union all
select '名称7','规格2' union all
select '名称8','规格2'
--select * from tb1
--select * from tb2select a.编号,a.名称,a.规格 into # from tb1 a inner join tb2 b on a.名称=b.名称 and a.规格=b.规格insert into tb1
select a.名称,a.规格 from tb2 a
where a.编号 not in(select 编号 from #)drop table #
select * from tb1
接分
create table tb1(编号 varchar(10),名称 varchar(10),规格 varchar(10))
insert into tb1
select '001','名称1','规格1' union all
select '002','名称2','规格1' union all
select '003','名称3','规格1' union all
select '004','名称4','规格1' union all
select '005','名称5','规格2' union all
select '006','名称6','规格2' union all
select '007','名称7','规格2' union all
select '008','名称8','规格2'
create table tb2(编号 varchar(10),名称 varchar(10),规格 varchar(10))
insert into tb2
select '001','名称1','规格1' union all
select '002','名称2','规格1' union all
select '003','名称3','规格1' union all
select '004','名称4','规格1' union all
select '005','名称9','规格1' union all
select '006','名称10','规格2' union all
select '007','名称11','规格2' union all
select '008','名称12','规格2'
select id=identity(int,1,1),* into # from tb1
insert into # select * from tb2 a
where not exists(select 1 from tb1 where a.名称=名称 and a.规格=规格)
insert into tb1 select right('000'+ltrim(id),3),名称,规格 from # a
where not exists(select 1 from tb1 where a.名称=名称 and a.规格=规格)
drop table #
select * from tb1
tb1编号 名称 规格
----- ----- -----
001 名称1 规格1
002 名称2 规格2
003 名称3 规格3
004 名称4 规格4
005 名称5 规格5
006 名称6 规格6
007 名称7 规格7
008 名称8 规格8
009 名称9 规格9
tb2编号 名称 规格
----- ----- -----
001 名称1 规格1
003 名称3 规格3
004 名称4 规格4
066 名66 规66
008 名称8 规格8
077 名77 规77
sql查询:
select
right('00000'+
convert(varchar(5),
(
select count(*) + 1+ convert(integer,(select max(tb1.编号) from tb1 )) from tb2
where tb2.名称 + tb2.规格 not in (select 名称 + 规格 from tb1)
and tb2.编号 <a.编号
))
,5) as '编号',
名称,
规格
from tb2 a where a.名称 + a.规格 not in (select 名称 + 规格 from tb1)
结果:
编号 名称 规格
---------- ----- -----
00010 名66 规66
00011 名77 规77
select @maxnum = max(编号) + 1 from tb1select identity(numeric,@maxnum,1) as 编号,名称,规格 from tb2 where tb2.名称 + tb2.规格 not in (select tb2.名称 + tb2.规格 from tb1)
select @maxnum = max(编号) + 1 from tb1 select identity(numeric,@maxnum,1) as 编号,名称,规格 from tb2 where tb2.名称 + tb2.规格 not in (select tb1.名称 + tb1.规格 from tb1)
select @maxnum = max(编号) + 1 from tb1 select identity(numeric,@maxnum,1) as 编号,名称,规格 into tb3 from tb2 where tb2.名称 + tb2.规格 not in (select tb1.名称 + tb1.规格 from tb1)
(Number Nvarchar(5),--编号
Name Nvarchar(20),--名称
Kind Nvarchar(10)--类型
)
Create Table tb2
(Number Nvarchar(5),
Name Nvarchar(20),
Kind Nvarchar(10)
)
Insert tb1 values('001','aa','C001')
Insert tb1 values('002','bb','C002')
Insert tb1 values('003','cc','C003')
Insert tb2 values('001','aa','C001')
Insert tb2 values('002','bb','C002')
Insert tb2 values('003','cc','C003')
Insert tb2 values('004','dd','C004')
Insert tb2 values('005','ee','C005')
Insert tb2 values('006','ff','C006')
Insert tb2 values('007','gg','C007')
Insert tb2 values('008','hh','C008')insert tb1
select * from tb2
where not exists (select* from tb1
where Name=tb2.Name and Kind=tb1.Kind)select * from tb1
--Drop Table tb1
insert into tb1(名称,规格)
Select tb2.名称,tb2.规格 From tb2 Where tb2.名称 not in (Select tb1.名称 From tb1)
INTERSECT
SELECT 编号,名称,规格 from tb2 WITH (NOLOCK)
) a
UNION ALL
SELECT 编号,名称,规格 FROM (SELECT 编号,名称,规格 from tb1 WITH (NOLOCK)
EXCEPT
SELECT 编号,名称,规格 from tb2 WITH (NOLOCK)
) b