SQL SERVER下树形数据的查询

一、测试环境:

1.SQL SERVER

二、数据结构:

1.部门表sys_dept:

dept_id             部门编码

dept_name       部门名称

parent_id          上级部门编码

三、SQL:

with dept as
(
select
    d.*
from
    sys_dept d
union all
select
    dep.*
from
    dept,sys_dept dep
    where dept.parent_id=dep.dept_id
)
select distinct * from dept

(全文完)

猜你喜欢

转载自blog.csdn.net/humors221/article/details/114749081