这三个表中的字段值都各自都有重复的。
例如:
select * from a
查询结果如下:
id
id0001
id0002
id0003
id0004
......
select * from b
查询结果如下:
id
id0001
id0002
id0003
id0004
......
select * from c
查询结果如下:
id
id0001
id0002
id0003
id0004
......
我现在想将a,b,c三个表的id值合并到d表中,因为各自都有重复的值!想通过存储过程达到合并的目的当a表的id值插入到d表中后,再导入b表的第一个id值时就自动取a
表导入到d表中的最后一个id值+1直至最后导入完毕,导c表的第一个id值时就自动取b表导入到d表中的最后一个id值+1直至最后导入完毕.使d表的id值是连贯的序号值id0001至id***.
CREATE TABLE a (id char(11) null default '')
CREATE procedure sbp_hb
as
insert into d select * from a
insert into d select * from b
insert into d select * from c
go
我想要的结果如下:
select * from d
查询结果如下:
id
id0001
id0002
id0003
id0004
id0005
id0006
id0007
id0008
id0009
id0010
id0011
id0012
希望高手们来解决这个问题!感激之至!
7 个解决方案
#1
看你的需求就是为了保证d表的id列值不重复。
为什么不重新编号。重新编号?
为什么不重新编号。重新编号?
#2
可以生成临时表:
create table #t(id int identity),从1递增,比你的三个表的记录要略大,
a,b,c 三个表的ID依次从此表中取,用过的ID不再使用,就可以实现你想要的结果
create table #t(id int identity),从1递增,比你的三个表的记录要略大,
a,b,c 三个表的ID依次从此表中取,用过的ID不再使用,就可以实现你想要的结果
#3
create procedure prc_uniteABC
as
begin
declare @id int
insert into d select id from a order by id--如果去掉重复的加个 distinct
select @id=max(right(id,len(id)-2)) from d
insert into d select left(id,2)+right('0000'+ltrim(@id+right(id,len(id)-2)),4) from b order by id
select @id=max(right(id,len(id)-2)) from d
insert into d select left(id,2)+right('0000'+ltrim(@id+right(id,len(id)-2)),4) from c order by id
end
go
#4
create table d (id varchar(6))
go
select id into a from ( select 'id0001' as id union all
select 'id0002' union all select 'id0003' union all select 'id0004' ) t
select id into b from ( select 'id0001' as id union all
select 'id0002' union all select 'id0003' union all select 'id0004' ) t
select id into c from ( select 'id0001' as id union all
select 'id0002' union all select 'id0003' union all select 'id0004' ) t
select * from a
go
/*
id0001
id0002
id0003
id0004
*/
select * from b
go
/*
id0001
id0002
id0003
id0004
*/
select * from c
go
/*
id0001
id0002
id0003
id0004
*/
create procedure prc_uniteABC
as
begin
declare @id int
insert into d select id from a order by id--如果去掉重复的加个 distinct
select @id=max(right(id,len(id)-2)) from d
insert into d select left(id,2)+right('0000'+ltrim(@id+right(id,len(id)-2)),4) from b order by id
select @id=max(right(id,len(id)-2)) from d
insert into d select left(id,2)+right('0000'+ltrim(@id+right(id,len(id)-2)),4) from c order by id
end
go
exec prc_uniteABC
select * from d
go
/*
id0001
id0002
id0003
id0004
id0005
id0006
id0007
id0008
id0009
id0010
id0011
id0012
*/
#5
--> 测试数据: [a]
if object_id('[a]') is not null drop table [a]
create table [a] (id varchar(6))
insert into [a]
select 'id0001' union all
select 'id0002' union all
select 'id0003' union all
select 'id0004'
--> 测试数据: [b]
if object_id('[b]') is not null drop table [b]
create table [b] (id varchar(6))
insert into [b]
select 'id0001' union all
select 'id0002' union all
select 'id0003' union all
select 'id0004'
--> 测试数据: [c]
if object_id('[c]') is not null drop table [c]
create table [c] (id varchar(6))
insert into [c]
select 'id0001' union all
select 'id0002' union all
select 'id0003' union all
select 'id0004'
go
--> 测试数据: [d]
if object_id('[d]') is not null drop table [d]
create table d(id varchar(6))
go
--创建函数
create function conv(@id varchar(6))
returns varchar(6)
as
begin
return (select 'id'+right('0000'+ltrim(isnull(max(right(id,4)),0)+1),4) from d)
end
go
--通过函数转换后插入a,b,c三表数据
insert into d select dbo.conv(id) from [a]
insert into d select dbo.conv(id) from [b]
insert into d select dbo.conv(id) from [c]
go
--查询结果
select * from d
#6
谢谢各位!感激之至!
#7
hanjs在吗?
我用select @id=max(right(id,len(id)-2)) from d 时sql报错:
服务器: 消息 536,级别 16,状态 1,行 1
向 substring 函数传递了无效的 length 参数。
我用
select * from d where PATINDEX('%[^0-9]%',substring(id,4,6))<>0
没有发现非数字型的字符啊!
真的是急死个人咧!
我用select @id=max(right(id,len(id)-2)) from d 时sql报错:
服务器: 消息 536,级别 16,状态 1,行 1
向 substring 函数传递了无效的 length 参数。
我用
select * from d where PATINDEX('%[^0-9]%',substring(id,4,6))<>0
没有发现非数字型的字符啊!
真的是急死个人咧!
#1
看你的需求就是为了保证d表的id列值不重复。
为什么不重新编号。重新编号?
为什么不重新编号。重新编号?
#2
可以生成临时表:
create table #t(id int identity),从1递增,比你的三个表的记录要略大,
a,b,c 三个表的ID依次从此表中取,用过的ID不再使用,就可以实现你想要的结果
create table #t(id int identity),从1递增,比你的三个表的记录要略大,
a,b,c 三个表的ID依次从此表中取,用过的ID不再使用,就可以实现你想要的结果
#3
create procedure prc_uniteABC
as
begin
declare @id int
insert into d select id from a order by id--如果去掉重复的加个 distinct
select @id=max(right(id,len(id)-2)) from d
insert into d select left(id,2)+right('0000'+ltrim(@id+right(id,len(id)-2)),4) from b order by id
select @id=max(right(id,len(id)-2)) from d
insert into d select left(id,2)+right('0000'+ltrim(@id+right(id,len(id)-2)),4) from c order by id
end
go
#4
create table d (id varchar(6))
go
select id into a from ( select 'id0001' as id union all
select 'id0002' union all select 'id0003' union all select 'id0004' ) t
select id into b from ( select 'id0001' as id union all
select 'id0002' union all select 'id0003' union all select 'id0004' ) t
select id into c from ( select 'id0001' as id union all
select 'id0002' union all select 'id0003' union all select 'id0004' ) t
select * from a
go
/*
id0001
id0002
id0003
id0004
*/
select * from b
go
/*
id0001
id0002
id0003
id0004
*/
select * from c
go
/*
id0001
id0002
id0003
id0004
*/
create procedure prc_uniteABC
as
begin
declare @id int
insert into d select id from a order by id--如果去掉重复的加个 distinct
select @id=max(right(id,len(id)-2)) from d
insert into d select left(id,2)+right('0000'+ltrim(@id+right(id,len(id)-2)),4) from b order by id
select @id=max(right(id,len(id)-2)) from d
insert into d select left(id,2)+right('0000'+ltrim(@id+right(id,len(id)-2)),4) from c order by id
end
go
exec prc_uniteABC
select * from d
go
/*
id0001
id0002
id0003
id0004
id0005
id0006
id0007
id0008
id0009
id0010
id0011
id0012
*/
#5
--> 测试数据: [a]
if object_id('[a]') is not null drop table [a]
create table [a] (id varchar(6))
insert into [a]
select 'id0001' union all
select 'id0002' union all
select 'id0003' union all
select 'id0004'
--> 测试数据: [b]
if object_id('[b]') is not null drop table [b]
create table [b] (id varchar(6))
insert into [b]
select 'id0001' union all
select 'id0002' union all
select 'id0003' union all
select 'id0004'
--> 测试数据: [c]
if object_id('[c]') is not null drop table [c]
create table [c] (id varchar(6))
insert into [c]
select 'id0001' union all
select 'id0002' union all
select 'id0003' union all
select 'id0004'
go
--> 测试数据: [d]
if object_id('[d]') is not null drop table [d]
create table d(id varchar(6))
go
--创建函数
create function conv(@id varchar(6))
returns varchar(6)
as
begin
return (select 'id'+right('0000'+ltrim(isnull(max(right(id,4)),0)+1),4) from d)
end
go
--通过函数转换后插入a,b,c三表数据
insert into d select dbo.conv(id) from [a]
insert into d select dbo.conv(id) from [b]
insert into d select dbo.conv(id) from [c]
go
--查询结果
select * from d
#6
谢谢各位!感激之至!
#7
hanjs在吗?
我用select @id=max(right(id,len(id)-2)) from d 时sql报错:
服务器: 消息 536,级别 16,状态 1,行 1
向 substring 函数传递了无效的 length 参数。
我用
select * from d where PATINDEX('%[^0-9]%',substring(id,4,6))<>0
没有发现非数字型的字符啊!
真的是急死个人咧!
我用select @id=max(right(id,len(id)-2)) from d 时sql报错:
服务器: 消息 536,级别 16,状态 1,行 1
向 substring 函数传递了无效的 length 参数。
我用
select * from d where PATINDEX('%[^0-9]%',substring(id,4,6))<>0
没有发现非数字型的字符啊!
真的是急死个人咧!