select parent, child, level from (
select parent_table.table_name parent, child_table.table_name child
from user_tables parent_table,
user_constraints parent_constraint,
user_constraints child_constraint,
user_tables child_table
where parent_table.table_name = parent_constraint.table_name
and parent_constraint.constraint_type IN('P', 'U')
and child_constraint.r_constraint_name = parent_constraint.constraint_name
and child_constraint.constraint_type = 'R'
and child_table.table_name = child_constraint.table_name
and child_table.table_name != parent_table.table_name
)
start with parent = 'DEPT'
connect by prior child = parent
을 자체 참조 테이블을 삽입하지 않도록합니다. 스키마 간 종속성을 처리해야하는 경우 OWNER 및 R_OWNER 열에 대한 데이터 사전 테이블 및 조건의 DBA_ 버전을 사용하십시오. 더 자세히 살펴보면 자기 참조 제약 (즉, MGR 열이 EMPNO 열을 참조하는 EMP 테이블에 대한 제약)을 고려하지 않으므로 처리해야 할 경우 해당 사례를 처리하도록 코드를 수정해야합니다 자기 참조 제약이있다. 테스트 목적으로
, 나는 (손자 의존성 포함) 또한 DEPT 테이블을 참조하는 SCOTT 스키마에 대한 몇 가지 새로운 테이블
SQL> create table dept_child2 (
2 deptno number references dept(deptno)
3 );
Table created.
SQL> create table dept_child3 (
2 dept_child3_no number primary key,
3 deptno number references dept(deptno)
4 );
Table created.
SQL> create table dept_grandchild (
2 dept_child3_no number references dept_child3(dept_child3_no)
3 );
Table created.
를 추가하고 쿼리가 예상 출력을 반환 것을 확인
SQL> ed
Wrote file afiedt.buf
1 select parent, child, level from (
2 select parent_table.table_name parent, child_table.table_name child
3 from user_tables parent_table,
4 user_constraints parent_constraint,
5 user_constraints child_constraint,
6 user_tables child_table
7 where parent_table.table_name = parent_constraint.table_name
8 and parent_constraint.constraint_type IN('P', 'U')
9 and child_constraint.r_constraint_name = parent_constraint.constraint_name
10 and child_constraint.constraint_type = 'R'
11 and child_table.table_name = child_constraint.table_name
12 and child_table.table_name != parent_table.table_name
13 )
14 start with parent = 'DEPT'
15* connect by prior child = parent
SQL>/
PARENT CHILD LEVEL
------------------------------ ------------------------------ ----------
DEPT DEPT_CHILD3 1
DEPT_CHILD3 DEPT_GRANDCHILD 2
DEPT DEPT_CHILD2 1
DEPT EMP 1
그 쿼리를 실행할 때'ORA-01437 : CONNECT BY와 결합 할 수 없습니다 .' –
@ user1598390 - 정확한 테스트 케이스를 실행할 때 오류가 발생했다는 것을 말하고 있습니까? 아니면 뭔가 다른 것조차하고 있습니까? 코드를 보지 않으면, 우리가 당신을 도울 수있을 것 같지 않습니다. 아마도 사용하고있는 정확한 쿼리를 게시 할 수 있고 이상적으로 게시하는 일부 샘플 테이블로는 실패한 것으로 보이는 새로운 질문을 만들어야 할 것입니다. –
SQL을 복사하여 Oracle 클라이언트 (PL/SQL Developer)에 붙여 넣은 다음 F8 키를 눌러 실행하고 오류가 발생합니다. –