【发布时间】:2012-02-07 07:28:59
【问题描述】:
我有一个临时表,其中有以下数据,我想用他的父级 categoryID 过滤子级的行,直到它到达那些层次结构中父级的顶部。
;with cte (rowid,ParentCategoryID,CategoryID,Status,Level,CategoryName,ISProduct) as
(
Select rowid,ParentCategoryID,CategoryID,Status,Level,CategoryName,ISProduct from #newtemp where ParentCategoryId!=0
union all
select cte.rowid,cte.ParentCategoryID,cte.CategoryID,cte.Status,cte.Level,cte.CategoryName,cte.ISProduct
from #newtemp inner join cte ON cte.CategoryId=#newtemp.ParentCategoryId
)
select * from cte
【问题讨论】:
-
你有什么问题?结果是什么?
-
我希望 'ghfd' 的父级 'f' 应该在结果表中。这不会出现
标签: sql-server-2005 parent-child common-table-expression