๐Ÿ’ป Programming/Oracle 11g

[SQL] CONNECT BY , START WITH

๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ ๋ฐ์ดํƒ€๋ฅผ ๋ฝ‘์•„๋ณด์ž!!!!

ํ•œ ํ…Œ์ด๋ธ” Table_1์ด ์žˆ๋‹ค.

์ด ํ…Œ์ด๋ธ”์€ objectIDํ•„๋“œ์™€ parentObjectID ํ•„๋“œ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค.

๋‘ ํ•„๋“œ์—๋Š” OBJECT_X(X๋Š” ์ •์ˆ˜ํ˜• ์ˆซ์ž)ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ๋‹ค. 

๋˜, OBJECT_1์ด ํ•˜์œ„ OBJECT_2๋ฅผ ๊ฐ€์ง€๊ณ  OBJECT_2๊ฐ€ ๋˜ ํ•˜์œ„ OBJECT_3์„ ๊ฐ€์ง„๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. (OBJECT_2๋Š” OBJECT_1์˜ ์ž์‹ OBJECT๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด๋œ๋‹ค.)

์—ฌ๋Ÿฌ ๋‹ค๋ฅธ OBJECT_X ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋“ค์ด ํ…Œ์ด๋ธ” ๋‚ด์— ๋งŽ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.

 

์ด๋•Œ CONNECT BY์™€ START WITH๋ฅผ ์ด์šฉํ•˜์—ฌ OBJECT_2์™€ ๊ทธ ์ƒ์œ„, ๋˜ ๊ทธ ์ƒ์œ„, ๋˜ ๊ทธ ์ƒ์œ„...์ญˆ์šฐ์šฑ ํ•ด์„œ OBJECT_2๋ฅผ ๊ฐ–๋Š” ์ตœ์ƒ์œ„ OBJECT๊นŒ์ง€ ๊ณ„์ธต๊ตฌ์กฐ๋กœ ๋ฝ‘์•„๋‚ด๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜๋ฉด ๋œ๋‹ค.

 

SELECT *

FROM Table_1

CONNECT BY PRIOR objectID = parentObjectID

START WITH objectID = 'OBJECT_2'

 

------- 

 ํŒŒ๋ž€์ƒ‰ ์กฐ๊ฑด๋ถ€์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๊ฒฐ๊ณผ๋„ ๋ฐ”๋€Œ๋ฏ€๋กœ ์ฃผ์˜ํ•˜์ž.!!!!