SQL 找路线问题(查找 从A到 I怎么走路线)

查找 从A到 I怎么走路线

create
table #y([f1] varchar(1),[f2] varchar(1)) insert #y select 'A','B' union all select 'A','F' union all select 'A','G' union all select 'B','C' union all select 'B','G' union all select 'B','H' union all select 'B','A' union all select 'C','B' union all select 'C','H' union all select 'C','I' union all select 'C','D' union all select 'D','C' union all select 'D','I' union all select 'D','J' union all select 'E','J' union all select 'E','D' union all select 'F','A' union all select 'F','G' union all select 'G','A' union all select 'G','B' union all select 'G','H' union all select 'G','F' union all select 'H','G' union all select 'H','B' union all select 'H','C' union all select 'H','I' union all select 'I','H' union all select 'I','C' union all select 'I','D' union all select 'I','F' union all select 'J','I' union all select 'J','D' union all select 'J','E' GO WITH tb_a AS( SELECT #y.f1,#y.f2,[result]=CAST(#y.f1 +'->' + #y.f2 AS VARCHAR(1500)) FROM #y WHERE #y.f1='a' UNION ALL SELECT #y.f1,#y.f2,[result]=CAST([result]+ '->' + #y.f2 AS VARCHAR(1500)) FROM tb_a a INNER JOIN #y ON a.f2=#y.f1 WHERE charIndex(#y.f2,a.[result])<1 ) SELECT * FROM tb_a WHERE result LIKE '%i'
原文地址:https://www.cnblogs.com/you000/p/2804749.html