ms-sql 재귀쿼리를 이용해서 메뉴 소트하기
메뉴 테이블 생성 시 MENU_NO, MENU_NM, MENU-PRNT, MENU_SEQ, MENU_URL 컬럼을 생성
MENU-PRNT는 페이지내 상단 메뉴 번호
MENU_SEQ 는 MENU-PRNT 하위 메뉴 번호
WITH MENU_TREE AS(
SELECT
MENU_NO
, MENU_NM
, MENU_PRNT
, MENU_SEQ
, CONVERT(VARCHAR(255), MENU_SEQ) SORT
, CONVERT(VARCHAR(255), MENU_NM) DEPTH_FULLNAME
FROM FEWM_MENU_MNG
WHERE MENU_PRNT = 0
UNION ALL
SELECT
B.MENU_NO
, B.MENU_NM
, B.MENU_PRNT
, B.MENU_SEQ
, CONVERT(VARCHAR(255), CONVERT(NVARCHAR, C.SORT) + '>' + CONVERT(VARCHAR(255), B.MENU_SEQ)) SORT
, CONVERT(VARCHAR(255), CONVERT(NVARCHAR, C.DEPTH_FULLNAME) + '>' + CONVERT(VARCHAR(255), B.MENU_NM)) DEPTH_FULLNAME
FROM FEWM_MENU_MNG B, MENU_TREE C
WHERE C.MENU_NO = B.MENU_PRNT
)
SELECT
MENU_NO
, MENU_NM
, MENU_PRNT
, MENU_SEQ
, DEPTH_FULLNAME
FROM MENU_TREE ORDER BY SORT