with recursice dep_list(dept_cd, dept_nm, dept_depth, path, cycle) as
(
select
bas.dept_cd,
bas.dept_nm,
bas.dept_depth,
array[bas.dept_cd::text], // 지나온 path 를 다시 탐색하지 않기 위해 저장
false // 성능상 추가. 없어도 쿼리 실행에는 상관無
from dept_bas bas
where bas.up_dept_cd ='ROOT' // 시작 조건
and bas.use_yn ='Y'
UNION ALL
select
dep1.dept_cd,
dep1.dept_nm,
dep1.dept_depth,
array_append(dep2.path, dep1.dept_cd::text),
dep1.dept_cd = any(dep2.path)
from dept_bas dep1, dep_list dep2
where dep1 = up_dept_cd = dep2.dept_cd // 상위 메뉴 = 하위 메뉴
and dep1.use_yn = 'Y'
and not cycle
)
select *
from dep_list
where 1=1
order by path => 실ㅈㅔ view 를 위한 데이터 select