SQL 关于tree存储及提取的话题

来源:互联网 发布:数据球神 编辑:程序博客网 时间:2024/05/12 17:07

 

-- 总结关于tree在mssql中的存储、查询相关代码了。
set nocount on
go
create table bom (
     code 
char(16not null primary key
    ,parentcode 
char(16)
    ,name 
varchar(32)
    ,description 
varchar(512)
)
go
-- 指定代码的名称
create function dbo.fn_nameofcode (@code char(16))
returns varchar(32)
as begin
    
return isnull((select name from bom where code=@code),'')
end
go
-- 指定代码的上级代码
create function dbo.fn_parentcode (@code char(16))
returns char(16)
as begin
    
return isnull((select parentcode from bom where code=@code),'')
end
go
-- 指定代码的顶级代码
create function dbo.fn_topcode (@code char(16))
returns char(16)
as begin
    
declare @pcode char(16)
    
set @pcode=dbo.fn_parentcode(@code)
    
while @pcode > ''
    
begin
        
set @code=@pcode
        
set @pcode=dbo.fn_parentcode(@code)
    
end
    
return @code
end
go
-- 指定代码的路径代码
create function dbo.fn_codepath (@code char(16))
returns varchar(512)
as begin
    
declare @pcode char(16), @codepath varchar(512)
    
set @codepath=ltrim(rtrim(@code))
    
set @pcode=dbo.fn_parentcode(@code)
    
while @pcode > ''
    
begin
        
set @code=@pcode
        
set @codepath=ltrim(rtrim(@code))+'/'+@codepath
        
set @pcode=dbo.fn_parentcode(@code)
    
end
    
return @codepath
end
go
-- 指定代码的路径名称
create function dbo.fn_codenamepath (@code char(16))
returns varchar(512)
as begin
    
declare @pcode char(16), @namepath varchar(512)
    
set @namepath=dbo.fn_nameofcode(ltrim(rtrim(@code)))
    
set @pcode=dbo.fn_parentcode(@code)
    
while @pcode > ''
    
begin
        
set @code=@pcode
        
set @namepath=dbo.fn_nameofcode(ltrim(rtrim(@code)))+'/'+@namepath
        
set @pcode=dbo.fn_parentcode(@code)
    
end
    
return @namepath
end
go
-- 指定代码的所有下级条目
create function dbo.fn_subtreeofcode (@code char(16))
returns @subtree table (
     code 
char(16)
    ,parentcode 
char(16)
    ,codepath 
varchar(512)
    ,name 
varchar(32)
    ,description 
varchar(512)
    )
as begin
    
insert into @subtree 
    
select code, parentcode, codepath=dbo.fn_codepath(code), name, description
    
from bom
    
where parentcode=@code 
    
while @@rowcount>0
    
begin 
        
insert into @subtree 
        
select code, parentcode, codepath=dbo.fn_codepath(code), name, description
        
from bom a
        
where exists (select 1 from @subtree b where a.parentcode=b.code)
        
and not exists (select 1 from @subtree b where b.code=a.code)
    
end  return
end
go
-- 指定代码的所有叶子
create function dbo.fn_leafeofcode (@code char(16))
returns @subtree table (
     code 
char(16)
    ,parentcode 
char(16)
    ,codepath 
varchar(512)
    ,name 
varchar(32)
    ,description 
varchar(512)
    )
as begin
    
insert into @subtree 
    
select code, parentcode, codepath=dbo.fn_codepath(code), name, description
    
from bom
    
where parentcode=@code 
    
while @@rowcount>0
    
begin 
        
insert into @subtree 
        
select code, parentcode, codepath=dbo.fn_codepath(code), name, description
        
from bom a
        
where exists (select 1 from @subtree b where a.parentcode=b.code)
        
and not exists (select 1 from @subtree b where b.code=a.code)
    
end
    
-- 删除所有存在下级条目的条目
    delete from @subtree a
    
where exists(select 1 from @subtree b
        
where b.parentcode=a.code)
    
return
end
go

/**//* 一个例子,源自《这样的数据结构怎样设计TABLE最优》
 * http://topic.csdn.net/u/20080419/13/3b181995-cbf1-4ab6-9101-90caf3953fd0.html
 
*/

-- 一级物料: 一级编号,一级物料名称,一级描述
insert into bom values ('A','','一种一级物料','物料A的描述')
insert into bom values ('B','','另一种一级物料','物料B的描述')

-- 二级物料:二级编号,二级物料名称,一级物料名称,二级描述
insert into bom values ('A01','A','二级物料','物料A01的描述')
insert into bom values ('A02','A','二级物料','物料A02的描述')
insert into bom values ('B01','B','二级物料','物料B01的描述')
insert into bom values ('B02','B','二级物料','物料B01的描述')

-- 三级物料:三级编号,三级料物名称,一级物料名称,二级物料名称,三级描述
insert into bom values ('B01-1','B01','三级物料','物料B01-1的描述')
insert into bom values ('B01-2','B01','三级物料','物料B01-2的描述')
insert into bom values ('B02-1','B02','三级物料','物料B02-1的描述')

-- 四级物料:四级编号,四级物料名称,一级物料名称,二级物料名称,三级物料名称,四级描述
insert into bom values ('B01-1.023','B01-1','四级物料','物料B01-1.023的描述')
insert into bom values ('B01-1.219','B01-1','四级物料','物料B01-1.219的描述')
insert into bom values ('B01-1.328','B01-1','四级物料','物料B01-1.328的描述')
insert into bom values ('B01-2.012','B01-2','四级物料','物料B01-2.012的描述')
go

select root = dbo.fn_topcode(code), path=dbo.fn_codepath(code), [|]='|'*
from bom

select *, namepath=dbo.fn_codenamepath(code)
from dbo.fn_subtreeofcode('B01')
order by codepath
go

drop function dbo.fn_nameofcode, dbo.fn_parentcode, dbo.fn_topcode, dbo.fn_codepath, dbo.fn_codenamepath
drop function dbo.fn_subtreeofcode
drop table bom
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手上弄了山药痒怎么办 老板就是个富二代怎么办 中考准考证密码忘了怎么办 准考证号和密码忘了怎么办 安运继续教育考试不及格怎么办 微信的视频打不开怎么办 公众号被取消了怎么办 合同上不写工资怎么办 已经上班了想考个大专文凭怎么办 微信小程序违规暂停服务怎么办 程序锁密码忘了怎么办 忘了应用锁密码怎么办 毕业后发现论文有错误怎么办 柯丽尔打胶片时胶片卡住了怎么办 闽教英语要收费怎么办 高一学生英语差怎么办 高一英语基础差怎么办 老师教育学生学生跑出教室怎么办 研究生课题难出论文怎么办 一审过了上诉期怎么办 民事判决赔偿不给钱怎么办 民事申诉期过了怎么办 过了两年申诉期怎么办 微快递下单不能定位怎么办 网上打字兼职被骗了怎么办 微信银行卡转错怎么办 在支付宝被诈骗怎么办 发现被骗了报警不理怎么办 投稿投错了网站怎么办 六个月宝宝吃辅食便秘怎么办 六个月宝宝加辅食后便秘怎么办 婴儿6个月便秘怎么办 7个月的孩子便秘怎么办 四个月宝宝喜欢吃手怎么办 博瑞智教育是上当了怎么办 我43岁记忆力差怎么办 艾灸灸出的湿疹怎么办 饭店合同到期房东不租怎么办 极端暴力控制不住自己怎么办 苹果已停止访问该网页怎么办 qq登陆后隐藏了怎么办