GPS平台、网站建设、软件开发、系统运维,找森大网络科技!
https://cnsendnet.taobao.com
来自森大科技官方博客
http://www.cnsendblog.com/index.php/?p=2087
/*
文件名:使用临时表循环递归形成树形结构
来源:POMDB.SP3_GetFundingPlanSubjectStat
说明:以下的用法比较巧妙,CTE_T是一个临时表,利用了循环,递归,首先查找根,
然后依次查找下一级,直到到最下级为止。
*/
USE POMDB
WITH CTE_T
AS
(
SELECT cast(OperAllowDB.DBO.F3_GetOutLine(Row_Number() OVER(ORDER BY rowno),2) AS VARCHAR(20)) AS OutLine,BAID,BAName,ParentBAID,BAWay
FROM dbo.TBudgetAccount
WHERE ParentBAID='00000'
UNION ALL
SELECT CONVERT(VARCHAR(20),B.OutLine+'.'+CAST(OperAllowDB.DBO.F3_GetOutLine(Row_Number() OVER(ORDER BY a.rowno),2)AS VARCHAR(20)))AS OutLine,a.BAID,a.BAName,a.ParentBAID,a.BAWay
FROM dbo.TBudgetAccount A INNER JOIN CTE_T as B ON A.ParentBAID=B.BAID
)
SELECT OutLine,BAID,BAName,ParentBAID,BAWay FROM CTE_T ORDER BY OutLine