博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
树形数据的处理
阅读量:4191 次
发布时间:2019-05-26

本文共 4223 字,大约阅读时间需要 14 分钟。

/*--表结构描述及数据环境:

 表名tb,如果修改表名,则相应修改所有数据处理中涉及到的表名tb

 id为编号(标识字段+主键),pid为上级编号,name为名称,后面可以自行增加其他字段. 
 凡是未特殊标注的地方,对自行增加的字段不影响处理结果

--邹建2003.12(引用请保留此信息)--*/

--测试数据

create table tb(
id int identity(1,1) not null constraint PK_tb primary key clustered,
pid int,name varchar(20))
insert tb  select 0,'中国'
union all select 0,'美国'
union all select 0,'加拿大'
union all select 1,'北京'
union all select 1,'上海'
union all select 1,'江苏'
union all select 6,'苏州'
union all select 7,'常熟'
union all select 6,'南京'
union all select 6,'无锡'
union all select 2,'纽约'
union all select 2,'旧金山'
go

--1.自定义函数--获取编码累计
create function f_getmergid(@id int)
returns varchar(8000)
as
begin
 declare @re varchar(8000),@pid int

 --为了数字排序正常,需要统一编码宽度

 declare @idlen int,@idheader varchar(20)
 select @idlen=max(len(id))
  ,@idheader=space(@idlen)
 from tb

 --得到编码累计

 set @re=right(@idheader+cast(@id as varchar),@idlen)
 select @pid=pid from tb where
 while @@rowcount>0
  select @re=right(@idheader+cast(@pid as varchar),@idlen)+','+@re
   ,@pid=pid from tb where
 return(@re)
end
go

--2.自定义函数--检测某个编码出发,是否被循环引用
create function f_chkid(@id int)
returns bit --循环,返回1,否则返回0
as
begin
 declare @re bit,@pid int
 
 set @re=0

 --检测

 select @pid=pid from tb where
 while @@rowcount>0
 begin
  if @pid=@id
  begin
   set @re=1
   goto lbErr
  end
  select @pid=pid from tb where
 end

lbErr:

 return(@re)
end
go

/*--数据复制

 如果表中包含自定义字段,需要修改存储过程

 存在嵌套不超过32层的问题.
--*/

--3.复制指定结点下的子结点到另一个结点下

create proc p_copy
@s_id int, --复制该项下的所有子项
@d_id int, --复制到此项下
@new_id int --新增加项的开始编号
as
declare @nid int,@oid int,@name varchar(20)
select id,name into #temp from tb where and id<@new_id
while exists(select 1 from #temp)
begin
 select @oid=id,@name=name from #temp
 insert into tb values(@d_id,@name)
 set @nid=@@identity
 exec p_copy @oid,@nid,@new_id
 delete from #temp where
end
go

--4.批量复制的存储过程--复制指定结点及其下面的所有子结点,并生成新结点

create proc p_copystr
@s_id varchar(8000) --要复制项的列表,用逗号分隔
as
declare @nid int,@oid int,@name varchar(20)
set @s_id=','+@s_id+','
select id,name into #temp from tb
where charindex(','+cast(id as varchar)+',', @s_id)>0
while exists(select 1 from #temp)
begin
 select @oid=id,@name=name from #temp
 insert into tb values(@oid,@name)
 set @nid=@@identity
 exec p_copy @oid,@nid,@nid
 delete from #temp where
end
go

--5.得到指定id的子id列表

create function f_getchildid(@id int)
returns @re table(id int)
as
begin
 insert into @re select id from tb where
 while @@rowcount>0
  insert into @re select a.id
   from tb a inner join @re b on a.pid=b.id
   where a.id not in(select id from @re)
 return
end
go

--6.得到指定id的父id列表
create function f_getparentid(@id int)
returns @re table(id int)
as
begin
 declare @pid int
 select @pid=pid from tb where
 while @pid<>0
 begin
  insert into @re values(@pid)
  select @pid=pid from tb where
 end
 return
end
go

--7.删除指定结点

create proc p_delete

@id int,    --要删除的id
@deletechild bit=0  --是否删除子 1.删除子,0.如果@id有子,则删除失败.
as
 if @deletechild=1
  delete from tb where dbo.f_getmergid(id) like dbo.f_getmergid(@id)+'%'
 else
  if exists(select 1 from tb where )
   goto lbErr
  else
   delete from tb where  
return

lbErr:

 RAISERROR ('该结点下有子结点,不能删除', 16, 1)
go

--8.得到编码累计及编码级别表,这个是针对全表的,主要是应该于全表处理:

create function f_getbmmerg()

returns @re table(id int,idmerg varchar(8000),level int)

as

begin

         declare @idlen int,@idheader varchar(20), @level int
         select @idlen=max(len(id)),@idheader=space(@idlen) from tb
         set @level=1
         insert into @re select id,right(@idheader+cast(id as varchar),@idlen),@level
                   from tb where pid=0
         while @@rowcount>0
         begin
                   set @level=@level+1
                   insert into @re select b.id,a.idmerg+','+right(@idheader+cast(b.id as varchar),@idlen),@level
                            from @re a inner join tb b on a.id=b.pid
                            where
         end
return
end
go

--应用:

/*--数据显示排序--*/

--分级显示--横向,先一级,后二级...
select * from tb order by pid

--分级显示--纵向

select * from tb order by dbo.f_getmergid(id)
go

/*--数据统计--*/

--分级统计,每个地区下的明细地区数
select *,
 明细地区数=(select count(*) from tb where dbo.f_getmergid(id) like dbo.f_getmergid(a.id)+',%')
from tb a order by dbo.f_getmergid(id)

go

/*--数据新增,修改
 
 数据新增,修改(包括修改所属的类别)没有什么技巧
 ,只需要检查所属的上级是否存在就行了.这个可以简单的用下面的语句来解决:
 if exists(select 1 from tb where
) print '存在' else print '不存在'
--*/

--删除'美国'的数据
--exec p_delete 2  --不包含子,因为有美国下有子,所以删除会出错
exec p_delete 2,1 --包含子,将删除美国及所有数据
go

原文参见我在CSDN上发表的贴子

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=20073

你可能感兴趣的文章
微信朋友圈五月十大谣言:60岁以上老人打962899可享免费服务
查看>>
乐视网:受让方致新云网与融创、盈瑞汇鑫无关联关系
查看>>
微信号都可以改了,那淘宝号呢?官方硬核回应遭网友吐槽:你没有心!
查看>>
“你出命,我出钱!”靠玩命,他又做了一次首富
查看>>
拼多多宣布周涛出任“明星推荐官” 618直播带货1999元iPhone 11
查看>>
三星Galaxy Note 20系列或将于8月5日发布
查看>>
新款iPad Pro曝光:搭载A14x仿生芯片 支持5G
查看>>
身份证丢失后被入职16家公司,网友:现在身份证都能自己打工了?
查看>>
一加Z将采用联发科处理器?官方回应:假的
查看>>
雷军微博念了几句诗 评论区疯狂猜谜
查看>>
快手为什么需要周杰伦?
查看>>
跑山么、后浪们?2.0T+237匹大马力后驱CT4山路试驾体验
查看>>
全球首款搭载华为5G技术量产车型即将发布
查看>>
京东在港上市,尘埃落定!
查看>>
微信喊你来找工作:上千家企业将提供超10万个就业岗位
查看>>
5年内,创始人父子相继因意外去世,这家市值百亿公司怎么了?
查看>>
定了!网易CEO丁磊6月11日快手直播带货
查看>>
小米全系865旗舰大降价,买就完事了!
查看>>
联姻寺库,一次半斤八两的合作,趣店的奢侈品生意仍看不见未来
查看>>
从马云柳传志的摆摊往事,看地摊经济值不值得All in
查看>>