Oracle “CONNECT BY”是層次查詢子句,一般用於樹狀或者層次結果集的查詢
Oracle 構造一個月份的天數 select to_date('200809','yyyymm')+(rownum-1) s_date from dual connect by rownum<=last_day(to_date('200809','yyyymm')) - to_date('200809','yyyymm') + 1
生成1-10的一個序列 SELECT ROWNUM FROM DUAL CONNECT BY ROWNUM <= 10
create table DEP ( DEPID number(10) not null, DEPNAME varchar2(256), UPPERDEPID number(10) ) --------------------------------------------------------------------- INSERT INTO DEP(DEPID, DEPNAME, UPPERDEPID) VALUES (0, '總經辦', null); INSERT INTO DEP(DEPID, DEPNAME, UPPERDEPID) VALUES (1, '開發部', 0); INSERT INTO DEP(DEPID, DEPNAME, UPPERDEPID) VALUES (2, '測試部', 0); INSERT INTO DEP(DEPID, DEPNAME, UPPERDEPID) VALUES (3, 'Sever開發部', 1); INSERT INTO DEP(DEPID, DEPNAME, UPPERDEPID) VALUES (4, 'Client開發部', 1); INSERT INTO DEP(DEPID, DEPNAME, UPPERDEPID) VALUES (5, 'TA測試部', 2); INSERT INTO DEP(DEPID, DEPNAME, UPPERDEPID) VALUES (6, '項目測試部', 2); --------------------------------------------------------------------- SELECT RPAD( ' ', 2*(LEVEL-1), '-' ) || DEPNAME "DEPNAME", CONNECT_BY_ROOT DEPNAME "ROOT", CONNECT_BY_ISLEAF "ISLEAF", LEVEL , SYS_CONNECT_BY_PATH(DEPNAME, '/') "PATH" FROM DEP START WITH UPPERDEPID IS NULL CONNECT BY PRIOR DEPID = UPPERDEPID;
DEMO下載