if exists (select name from sysobjects where name = 'old1' and xtype ='u')
drop table old1
goif exists (select name from sysobjects where name = 'ntable1' and xtype ='u')
drop table ntable1
gocreate table old1
(id1 varchar(10),name1 varchar(20),
id2 varchar(10),name2 varchar(20),
id3 varchar(10),name3 varchar(20),
id4 varchar(10),name4 varchar(20),
id5 varchar(10),name5 varchar(20),
id6 varchar(10),name6 varchar(20))
gocreate table ntable1(nid varchar(100),nname varchar(100))
goinsert into old1 select '07','产成品','01','成品锅','11','复底锅','101','双耳','01','切边','5*12.5','5 x 12.5锅'
insert into old1 select null,null ,'02','进口成品锅','12','单底锅','102','单柄','02','宽边',null,null
insert into old1 select null,null ,'03','国内成品锅','13','复合锅','103','线耳','03','反边',null,nulldeclare @t table
(id1 varchar(10),name1 varchar(20),
id2 varchar(10),name2 varchar(20),
id3 varchar(10),name3 varchar(20),
id4 varchar(10),name4 varchar(20),
id5 varchar(10),name5 varchar(20),
id6 varchar(10),name6 varchar(20))insert into @t select * from old1update a
set
id1 =isnull(a.id1 ,(select max(id1) from @t)),
name1=isnull(a.name1,(select max(name1) from @t)),
id2 =isnull(a.id2 ,(select max(id2) from @t)),
name2=isnull(a.name2,(select max(name2) from @t)),
id3 =isnull(a.id3 ,(select max(id3) from @t)),
name3=isnull(a.name3,(select max(name3) from @t)),
id4 =isnull(a.id4 ,(select max(id4) from @t)),
name4=isnull(a.name4,(select max(name4) from @t)),
id5 =isnull(a.id5 ,(select max(id5) from @t)),
name5=isnull(a.name5,(select max(name5) from @t)),
id6 =isnull(a.id6 ,(select max(id6) from @t)),
name6=isnull(a.name6,(select max(name6) from @t))
from
@t ainsert into ntable1
select
a.*
from
(select distinct id1,name1 from @t
union all
select distinct id1+'.'+id2,name2 from @t
union all
select distinct id1+'.'+id2+'.'+id3,name3 from @t
union all
select distinct id1+'.'+id2+'.'+id3+'.'+id4,name4 from @t
union all
select distinct id1+'.'+id2+'.'+id3+'.'+id4+'.'+id5,name5 from @t
union all
select distinct id1+'.'+id2+'.'+id3+'.'+id4+'.'+id5+'.'+id6,name6 from @t) a
select * from ntable1
go/*
nid nname
---------------------- ----------
07 产成品
07.01 成品锅
07.02 进口成品锅
07.03 国内成品锅
07.01.11 复底锅
07.02.12 单底锅
07.03.13 复合锅
07.01.11.101 双耳
07.02.12.102 单柄
07.03.13.103 线耳
07.01.11.101.01 切边
07.02.12.102.02 宽边
07.03.13.103.03 反边
07.01.11.101.01.5*12.5 5 x 12.5锅
07.02.12.102.02.5*12.5 5 x 12.5锅
07.03.13.103.03.5*12.5 5 x 12.5锅
*/drop table old1,ntable1
go
drop table old1
goif exists (select name from sysobjects where name = 'ntable1' and xtype ='u')
drop table ntable1
gocreate table old1
(id1 varchar(10),name1 varchar(20),
id2 varchar(10),name2 varchar(20),
id3 varchar(10),name3 varchar(20),
id4 varchar(10),name4 varchar(20),
id5 varchar(10),name5 varchar(20),
id6 varchar(10),name6 varchar(20))
gocreate table ntable1(nid varchar(100),nname varchar(100))
goinsert into old1 select '07','产成品','01','成品锅','11','复底锅','101','双耳','01','切边','5*12.5','5 x 12.5锅'
insert into old1 select null,null ,'02','进口成品锅','12','单底锅','102','单柄','02','宽边',null,null
insert into old1 select null,null ,'03','国内成品锅','13','复合锅','103','线耳','03','反边',null,nulldeclare @t table
(id1 varchar(10),name1 varchar(20),
id2 varchar(10),name2 varchar(20),
id3 varchar(10),name3 varchar(20),
id4 varchar(10),name4 varchar(20),
id5 varchar(10),name5 varchar(20),
id6 varchar(10),name6 varchar(20))insert into @t select * from old1update a
set
id1 =isnull(a.id1 ,(select max(id1) from @t)),
name1=isnull(a.name1,(select max(name1) from @t)),
id2 =isnull(a.id2 ,(select max(id2) from @t)),
name2=isnull(a.name2,(select max(name2) from @t)),
id3 =isnull(a.id3 ,(select max(id3) from @t)),
name3=isnull(a.name3,(select max(name3) from @t)),
id4 =isnull(a.id4 ,(select max(id4) from @t)),
name4=isnull(a.name4,(select max(name4) from @t)),
id5 =isnull(a.id5 ,(select max(id5) from @t)),
name5=isnull(a.name5,(select max(name5) from @t)),
id6 =isnull(a.id6 ,(select max(id6) from @t)),
name6=isnull(a.name6,(select max(name6) from @t))
from
@t ainsert into ntable1
select
a.*
from
(select distinct id1,name1 from @t
union all
select distinct id1+'.'+id2,name2 from @t
union all
select distinct id1+'.'+id2+'.'+id3,name3 from @t
union all
select distinct id1+'.'+id2+'.'+id3+'.'+id4,name4 from @t
union all
select distinct id1+'.'+id2+'.'+id3+'.'+id4+'.'+id5,name5 from @t
union all
select distinct id1+'.'+id2+'.'+id3+'.'+id4+'.'+id5+'.'+id6,name6 from @t) a
select * from ntable1
go/*
nid nname
---------------------- ----------
07 产成品
07.01 成品锅
07.02 进口成品锅
07.03 国内成品锅
07.01.11 复底锅
07.02.12 单底锅
07.03.13 复合锅
07.01.11.101 双耳
07.02.12.102 单柄
07.03.13.103 线耳
07.01.11.101.01 切边
07.02.12.102.02 宽边
07.03.13.103.03 反边
07.01.11.101.01.5*12.5 5 x 12.5锅
07.02.12.102.02.5*12.5 5 x 12.5锅
07.03.13.103.03.5*12.5 5 x 12.5锅
*/drop table old1,ntable1
go
create table old1
(id1 varchar(10),name1 varchar(20),
id2 varchar(10),name2 varchar(20),
id3 varchar(10),name3 varchar(20),
id4 varchar(10),name4 varchar(20),
id5 varchar(10),name5 varchar(20),
id6 varchar(10),name6 varchar(20))
gocreate table ntable1(nid varchar(100),nname varchar(100))
goinsert into old1 select '07','产成品','01','成品锅','11','复底锅','101','双耳','01','切边','5*12.5','5 x 12.5锅'
insert into old1 select null,null ,'02','进口成品锅','12','单底锅','102','单柄','02','宽边',null,null
insert into old1 select null,null ,'03','国内成品锅','13','复合锅','103','线耳','03','反边',null,null
go
--借助中间表处理数据
select * into #t from old1update a
set
id1 =isnull(a.id1 ,(select max(id1) from #t)),
name1=isnull(a.name1,(select max(name1) from #t)),
id2 =isnull(a.id2 ,(select max(id2) from #t)),
name2=isnull(a.name2,(select max(name2) from #t)),
id3 =isnull(a.id3 ,(select max(id3) from #t)),
name3=isnull(a.name3,(select max(name3) from #t)),
id4 =isnull(a.id4 ,(select max(id4) from #t)),
name4=isnull(a.name4,(select max(name4) from #t)),
id5 =isnull(a.id5 ,(select max(id5) from #t)),
name5=isnull(a.name5,(select max(name5) from #t)),
id6 =isnull(a.id6 ,(select max(id6) from #t)),
name6=isnull(a.name6,(select max(name6) from #t))
from
#t a--insert数据
insert into ntable1
select
a.*
from
(select distinct id1,name1 from #t
union all
select distinct id1+'.'+id2,name2 from #t
union all
select distinct id1+'.'+id2+'.'+id3,name3 from #t
union all
select distinct id1+'.'+id2+'.'+id3+'.'+id4,name4 from #t
union all
select distinct id1+'.'+id2+'.'+id3+'.'+id4+'.'+id5,name5 from #t
union all
select distinct id1+'.'+id2+'.'+id3+'.'+id4+'.'+id5+'.'+id6,name6 from #t) a
--查看insert操作结果
select * from ntable1
go/*
nid nname
---------------------- ----------
07 产成品
07.01 成品锅
07.02 进口成品锅
07.03 国内成品锅
07.01.11 复底锅
07.02.12 单底锅
07.03.13 复合锅
07.01.11.101 双耳
07.02.12.102 单柄
07.03.13.103 线耳
07.01.11.101.01 切边
07.02.12.102.02 宽边
07.03.13.103.03 反边
07.01.11.101.01.5*12.5 5 x 12.5锅
07.02.12.102.02.5*12.5 5 x 12.5锅
07.03.13.103.03.5*12.5 5 x 12.5锅
*/
--删除测试数据
drop table old1,ntable1,#t
go