Database/MS-SQL | Posted by 아키텍처 2012. 4. 24. 18:10

재귀쿼리

 
 

TS_CD30 테이블
 CD_ITEM NM_ITEM CD_PITEM
 AA 티셔츠 *
 AA01 기본티셔츠 AA
 AA02 후드티셔츠 AA
 BB 블라우스 *
 BB01 롱블라우스 BB
 
WITH JUUUN(CD_ITEM,NM_ITEM,SEQ) AS
(
 SELECT CD_ITEM, NM_ITEM, 1 AS SEQ
 FROM TS_CD30
 WHERE CD_PITEM = '*'
 
 UNION ALL
 
 SELECT J.CD_ITEM, J.NM_ITEM, (SEQ + 1)
 FROM TS_CD30 AS J
   INNER JOIN JUUUN AS C
   ON C.CD_ITEM = J.CD_PITEM
)
SELECT * FROM JUUUN ORDER BY CD_ITEM

Posted by 김준홍 (http://www.Juuun.com)