sql Server 递归
Declare @Id Int
Set @Id = 1;
With RootNodeCTE(cId,cName,cParentId)
As
(
Select cId,cName,cParentId From dbo.tbCustomer Where cId In (@Id)
Union All
Select tbCustomer.cId,tbCustomer.cName,tbCustomer.cParentId From RootNodeCTE
Inner Join tbCustomer
On RootNodeCTE.cId = tbCustomer.cParentId
)
Select * From RootNodeCTE ORDER BY RootNodeCTE.cId ASC

浙公网安备 33010602011771号