SQL 递归使用
直接贴代码吧= =
WITH CTE AS ( -->Begin 一个定位点成员 SELECT COUNTRYORDERID,HSNAME, COUNTRYNAME,PARENTORDERID, IMPORTCURRENTSUMAMOUNT, IMPORTCONTRASTSUMAMOUNTPERCENTAGE, IMPORTCURRENTSUMAMOUNTPERCENTAGE, CAST(COUNTRYNAME AS NVARCHAR(max)) AS TE,0 AS Levle FROM #TempTableDetail WHERE PARENTORDERID IS NULL AND HSNAME IN (SELECT HSNAME FROM #TempTableData) -->End UNION ALL -->Begin一个递归成员 SELECT #TempTableDetail.COUNTRYORDERID, #TempTableDetail.HSNAME, #TempTableDetail.COUNTRYNAME,#TempTableDetail.PARENTORDERID, #TempTableDetail.IMPORTCURRENTSUMAMOUNT, #TempTableDetail.IMPORTCONTRASTSUMAMOUNTPERCENTAGE, #TempTableDetail.IMPORTCURRENTSUMAMOUNTPERCENTAGE, CAST(REPLICATE(' ',LEN(CTE.TE))+#TempTableDetail.COUNTRYNAME AS NVARCHAR(MAX)) AS TE,Levle+1 AS Levle FROM #TempTableDetail INNER JOIN CTE ON #TempTableDetail.PARENTORDERID=CTE.COUNTRYORDERID --group by #TempTableDetail.HSNAME WHERE #TempTableDetail.HSNAME IN (SELECT HSNAME FROM #TempTableData) -->End ) INSERT INTO #TempResult(COUNTRYORDERID,HSNAME,COUNTRYNAME,IMPORTCONTRASTSUMAMOUNTPERCENTAGE,IMPORTCURRENTSUMAMOUNTPERCENTAGE,IMPORTCURRENTSUMAMOUNT) SELECT DISTINCT COUNTRYORDERID, HSNAME,TE,IMPORTCONTRASTSUMAMOUNTPERCENTAGE, IMPORTCURRENTSUMAMOUNTPERCENTAGE,IMPORTCURRENTSUMAMOUNT FROM CTE ORDER BY HSNAME,COUNTRYORDERID;
效果:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。