欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sqlserver 树结构递归

程序员文章站 2024-02-22 21:56:24
...
--获取当前及以下部门
Create proc GetCurrentAndUnderOrg
@orgId int
as
begin
WITH cte
AS
(
SELECT * ,0 AS level FROM Static_Organ WHERE [email protected]
UNION ALL
SELECT g.*,level+1 FROM Static_Organ g INNER JOIN cte
ON g.ParentOrgan=cte.OrganID
)
SELECT * FROM cte
end