Sql递归关联情况,With作为开头条件。
with Test_Recursion(Id,ParentId)
AS
(
select Id,ParentId from [V_KPI_DetailsActivities] where ParentId = 4 --没有引用CTE自身必须放在第一个递归行之上
union all --没有引用CTE自身的语句和第一个递归行之间必须用UNION ALL
select a.Id,a.ParentId from [V_KPI_DetailsActivities] as a join Test_Recursion as b on a.parentid=b.id--递归行
)
select Id into #temp from Test_Recursion;
select row_number() over (order by id ) as rowid,* from [V_KPI_DetailsActivities] where ProjectId=13
and ID = 4 or ID in (select Id from #temp )
drop table #temp;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。