SQL Server????·??????????
???????????? ???????[ 2013/4/25 10:58:46 ] ????????
???????????????????????@Node ?? @RelatedNode????????
use TestDB
go
--Procedure:
if object_id('up_GetPath') Is not null
Drop proc up_GetPath
go
create proc up_GetPath
(
@Node nvarchar(50)??
@RelatedNode nvarchar(50)
)
As
set nocount on
declare
@level smallint =1?? --????????????
@MaxLevel smallint=100?? --??????????
@Node_WhileFlag bit=1?? --??@Node??????????????????????????????????
@RelatedNode_WhileFlag bit=1 --??@RelatedNode??????????????????????????????????
--?????????????Node???????????????
if Exists(select 1 from RelationGraph where (Node=@Node And RelatedNode=@RelatedNode)
or(Node=@RelatedNode And RelatedNode=@Node) ) or @Node=@RelatedNode
begin
select convert(nvarchar(2000)??@Node + ' --> '+ @RelatedNode) AsRelationGraphPath??convert(smallint??0) As StopCount
return
end
--
if object_id('tempdb..#1') Is not null Drop Table #1 --?????#1???洢??????@Node????????????????????????
if object_id('tempdb..#2') Is not null Drop Table #2 --?????#2???洢??????@RelatedNode????????????????????????
create table #1(
Node nvarchar(50)??--??????
RelatedNode nvarchar(50)?? --??????
Level smallint --???
)
create table #2(Node nvarchar(50)??RelatedNode nvarchar(50)??Level smallint)
insert into #1 ( Node?? RelatedNode?? Level )
select Node?? RelatedNode?? @level from RelationGraph a where a.Node Node union --??????@Node???????
select RelatedNode?? Node?? @level from RelationGraph a where a.RelatedNode = @Node --??????@Node????????в??
set @Node_WhileFlag=sign(@@rowcount)
insert into #2 ( Node?? RelatedNode?? Level )
select Node?? RelatedNode?? @level from RelationGraph a where a.Node =@RelatedNode union --??????@RelatedNode???????
select RelatedNode?? Node?? @level from RelationGraph a where a.RelatedNode = @RelatedNode--??????@RelatedNode????????в??
set @RelatedNode_WhileFlag=sign(@@rowcount)
--??????RelationGraph???????@Node ?? @RelatedNode ?????????????????While????
if not exists(select 1 from #1) or not exists(select 1 from #2)
begin
goto While_Out
end
while not exists(select 1 from #1 a inner join #2 b on b.RelatedNode=a.RelatedNode) --?ж????????е?
and (@Node_WhileFlag|@RelatedNode_WhileFlag)>0 --?ж??????????
And @level<@MaxLevel --???????
begin
if @Node_WhileFlag >0
begin
insert into #1 ( Node?? RelatedNode?? Level )
--????
select a.Node??a.RelatedNode??@level+1
From RelationGraph a
where exists(select 1 from #1 where RelatedNode=a.Node And Level=@level) And
Not exists(select 1 from #1 where Node=a.Node)
union
--????
select a.RelatedNode??a.Node??@level+1
From RelationGraph a
where exists(select 1 from #1 where RelatedNode=a.RelatedNode AndLevel=@level) And
Not exists(select 1 from #1 where Node=a.RelatedNode)
set @Node_WhileFlag=sign(@@rowcount)
end
if @RelatedNode_WhileFlag >0
begin
insert into #2 ( Node?? RelatedNode?? Level )
--????
select a.Node??a.RelatedNode??@level+1
From RelationGraph a
where exists(select 1 from #2 where RelatedNode=a.Node And Level=@level) And
Not exists(select 1 from #2 where Node=a.Node)
union
--????
select a.RelatedNode??a.Node??@level+1
From RelationGraph a
where exists(select 1 from #2 where RelatedNode=a.RelatedNode AndLevel=@level) And
Not exists(select 1 from #2 where Node=a.RelatedNode)
set @RelatedNode_WhileFlag=sign(@@rowcount)
end
select @level+=1
end
While_Out:
--??????????????·??
if object_id('tempdb..#Path1') Is not null Drop Table #Path1
if object_id('tempdb..#Path2') Is not null Drop Table #Path2
;with cte_path1 As
(
select a.Node??a.RelatedNode??Level??convert(nvarchar(2000)??a.Node+' -> '+a.RelatedNode) AsRelationGraphPath??Convert(smallint??1) As PathLevel
From #1 a where exists(select 1 from #2where RelatedNode=a.RelatedNode)
union all
select b.Node??a.RelatedNode??b.Level??convert(nvarchar(2000)??b.Node+' -> '+a.RelationGraphPath) As RelationGraphPath ??Convert(smallint??a.PathLevel+1)
As PathLevel
from cte_path1 a
inner join #1 b on b.RelatedNode=a.Node
and b.Level=a.Level-1
)
select * Into #Path1 from cte_path1
;with cte_path2 As
(
select a.Node??a.RelatedNode??Level??convert(nvarchar(2000)??a.Node) AsRelationGraphPath??Convert(smallint??1) As PathLevel
From #2 a where exists(select 1 from #1where RelatedNode=a.RelatedNode)
union all
select b.Node??a.RelatedNode??b.Level??convert(nvarchar(2000)??a.RelationGraphPath+' -> '+b.Node) As RelationGraphPath ??Convert(smallint??a.PathLevel+1)
from cte_path2 a
inner join #2 b on b.RelatedNode=a.Node
and b.Level=a.Level-1
)
select * Into #Path2 from cte_path2
;with cte_result As
(
select a.RelationGraphPath+' -> '+b.RelationGraphPath AsRelationGraphPath??a.PathLevel+b.PathLevel -1
As StopCount??rank() over(order bya.PathLevel+b.PathLevel) As Result_row
From #Path1 a
inner join #Path2 b on b.RelatedNode=a.RelatedNode
and b.Level=1
where a.Level=1
)
select distinct RelationGraphPath??StopCount From cte_result where Result_row=1
go
?????????
???????????????????????е????·????????????????????????????????????·??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????й??????????????????缸??????????????????????????????????????????????????б????????????????????鼮???????????????????????·?????ο?????????????????????????????????????
????С??
?????????????????????????·???У?????????·??????????????У?????????????????????????????????????????????????г?????????????????????Щ???????????Σ??????ο??Щ????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11