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

posted @ 2014-03-13 09:50  小猴纸  阅读(94)  评论(0)    收藏  举报