现在的位置: 首页 > 综合 > 正文

树形数据转换

2013年11月10日 ⁄ 综合 ⁄ 共 2815字 ⁄ 字号 评论关闭

原帖地址:
http://community.csdn.net/Expert/topic/3663/3663934.xml?temp=.9100458

--测试数据
create table PROJECT(id int,name nvarchar(20),parent_id int)
insert PROJECT select 1,'所有项目',null
union  all     select 2,'项目1',1
union  all     select 3,'项目2',1

create table task(id int,name nvarchar(20),outline varchar(10))
insert task select 1 ,'任务1'      ,'1'
union  all  select 2 ,'任务1.1'    ,'1.1'
union  all  select 3 ,'任务1.1.1'  ,'1.1.1'
union  all  select 4 ,'任务1.1.2'  ,'1.1.2'
union  all  select 5 ,'任务2'      ,'2'
union  all  select 6 ,'任务2.1'    ,'2.1'
union  all  select 7 ,'任务2.1.1'  ,'2.1.1'
union  all  select 8 ,'任务2.1.1.1','2.1.1.1'
union  all  select 9 ,'任务3'      ,'3'
union  all  select 10,'任务4'      ,'4'
union  all  select 11,'任务4.1'    ,'4.1'
go

/*--处理要求
 
 1.把一数据库的TASK表的数据导入到另一数据库的PROJECT表与TEMP1表.导进去的数据上下级关系不再是用大纲显示,而是通过parent_id和project_id表示上下级。
 2.表TEMP1的project_id与project表中的id关联,表project表中的parent_id是对该表本身id字段的关连。
 3.要把task表的最底层的任务(没有子任务的)导入到temp1表,他的上级任务通过project_id关连
 4.从TASK表导入数据的parent_id自己输入
--*/

/*--最后实现的结果:

--** PROJECT 表的内容
id          name                 parent_id  
----------- -------------------- -----------
1           所有项目               NULL
2           项目1                  1
3           项目2                  1
4           任务1                  2
5           任务1.1                4
6           任务2                  2
7           任务2.1                6
8           任务2.1.1              7
9           任务4                  2

(所影响的行数为 9 行)

--temp1 表的内容
id          name                 project_id 
----------- -------------------- -----------
1           任务1.1.1              5
2           任务1.1.2              5
3           任务2.1.1.1            8
4           任务3                  2
5           任务4.1                9

(所影响的行数为 5 行)
--*/

--处理的存储过程
create proc p_process
@parent_id int=2
as
set nocount on
declare @id int,@step int,@s nvarchar(1000)

--得到 PROJECT 表中的新编号(因为不知道PROJECT的id是否标识字段,所以用了一些判断)
select @step=ident_incr('PROJECT')
,@id=ident_current('PROJECT')+@step
if @id is null
select @id=isnull(max(id),0)+1 from PROJECT
select @s='alter table #t add id int identity('+rtrim(@id)+','+rtrim(isnull(@step,1))+')'

--生成处理临时表
select name,parent_id=@parent_id,outline into #t from task a
where exists(
select * from task where outline like a.outline+'.%')
order by outline

--生成id,并且生成 praent_id
exec(@s)
update a set parent_id=b.id
from #t a,#t b
where charindex('.',a.outline)>0
and a.outline like b.outline+'.%'
and charindex('.',a.outline,len(b.outline)+2)=0

--处理结果插入 PROJECT
if @step is not null
set identity_insert PROJECT on
insert PROJECT(id,name,parent_id) select id,name,parent_id from #t

--生成表temp1
if exists(select * from sysobjects where name='temp1' and objectproperty(id,'isusertable')=1)
drop table temp1
select id=identity(int),a.name,isnull(b.id,@parent_id) as project_id
into temp1 from task a left join
#t b on a.outline like b.outline+'.%'
and charindex('.',a.outline,len(b.outline)+2)=0
where not exists(
select * from #t where outline=a.outline)
go

--调用
exec p_process 2

--显示处理后的结果
select * from PROJECT
select * from temp1
go

--删除测试
drop table PROJECT,task,temp1
drop proc p_process

抱歉!评论已关闭.