--測試資料
if object_id('[t]') is not null drop table [t]
create table [t] (id int,pid int,name varchar(10))
insert into [t]
select 1,0,'a' union all
select 2,1,'b' union all
select 3,2,'c' union all
select 4,0,'d' union all
select 5,4,'e' union all
select 6,2,'f' union all
select 7,3,'g' union all
select 8,7,'h'
go--獲取所有的要根結點
select t.* from t t where exists (select 1 from t s where s.pid = t.id)
go--獲取所有的葉子結點
select t.* from t t where not exists (select 1 from t s where s.pid = t.id)
go--獲取樹型記錄的全路徑
if object_id('[dbo].[getallpath]') is not null
drop function [dbo].[getallpath]
gocreate function getallpath()
returns @t table(id int,pid int,name varchar(50),path varchar(300))
asbegin
insert into @t select id,pid,name,null as path from t
update @t set path=name
declare @i int,@j int
set @i=0
set @j=1
select @i=max(pid) from @t
while @j<=@i
begin
update v set path=a.path+'.'+v.name
from @t v inner join @t a on v.pid=a.id
where v.pid=@j
set @j=@j+1
endreturn
endgo
select * from getallpath()
go--獲取樹型記錄的全路徑
if object_id('[dbo].[getpath]') is not null
drop function [dbo].[getpath]
gocreate function getpath(@id varchar(20))
returns varchar(300)
asbegin
declare @s varchar(300)
select @s = name,@id = pid from t where id = @id
while exists (select 1 from t where id = @id )
select @s = name+'.'+@s,@id = pid from t where id = @id
return @s
endgo
select t.*,dbo.getpath(cast(id as varchar(20))) path from t
go--查詢樹
if object_id('[dbo].[getchild]') is not null
drop function [dbo].[getchild]
gocreate function getchild(@id int)
returns @returnt table(pid int,id int,name varchar(50))
as begin
insert into @returnt select pid,id,name from t where id=@id
insert into @returnt select pid,id,name from t where pid=@id
while @@rowcount>0
insert into @returnt select a.pid,a.id,a.name
from t a inner join @returnt b on a.pid=b.id
where a.id not in(select id from @returnt)
return
end
goselect * from getchild(1)
go--查詢樹型記錄的全路徑
if object_id('[dbo].[getfindpath]') is not null
drop function [dbo].[getfindpath]
gocreate function getfindpath(@parentid varchar(20))
returns @returndepartment table(id int,pid int,name varchar(50),path varchar(300))
asbegin
declare @t table(id int,pid int,name varchar(50),path varchar(300))
insert into @t select id,pid,name,null as path from t
update @t set path=name
declare @i int,@j int
set @i=0
set @j=1
select @i=max(pid) from @t
while @j<=@i
begin
update v set path=a.path+'.'+v.name
from @t v inner join @t a on v.pid=a.id
where v.pid=@j
set @j=@j+1
endinsert into @returndepartment select * from @t where id=@parentid
insert into @returndepartment select * from @t where pid=@parentid
while @@rowcount>0
insert into @returndepartment select a.id,a.pid,a.name,a.path
from @t a inner join @returndepartment b on a.pid=b.id
where a.id not in(select id from @returndepartment)
return
endgo
select * from getfindpath(1)
go
樹型動態規劃練習總結
型別 一 多叉樹轉二叉樹進行資源分配 例如 例1.選課 每門課可能有一門先選課,即某些課必須在另外的某節課被選之後才能選,每門課能得的學分不同,求最大學分。例2.通向自由的鑰匙 可以從乙個房間通向另外多個房間,通過每個房間所需的花費不同,得到的價值也不同,用最小花費獲得最大價值。這種題目的特點是需要...
Java中泛型的應用總結
1 基本使用 public inte ce list 2 泛型與子類 child是parent的子類,list卻不是list的子類。因此 listlist new arraylist 是錯誤的。如果上面是正確的,那麼 listls new arraylist 1 listlo ls 2 lo.add...
利用SQL 2005 CTE處理樹型資料
在現實生活中,樹型資料屢見不鮮 組織機構,產品結構,人事關係等等.記得在以往的乙個專案中,涉及到機構,人員,在對其進行處理,特別是進行統計,聚集操作的時候,我一直沒找到乙個好的方法.臨時表,檢視,程式控制.能用的辦法都用上了,但在處理效率上一直不盡如人意.歸根結底,我的感覺就是在資料庫中,對資料的操...