Typical Oracle recursive algorithms are used. For example, under a multi-level department, a sub-department cannot be the parent department of the parent department.
For example, the father department has a son department and B son department. The father Department cannot be a parent department of the father department.
This is a classic demand for multi-level classification of many management systems.
Okay. The Department table structure-Department.
- NameType NullableDefaultComments
- ----------------------------------------------
- Id number (12)-- ID
- DEPT_NO VARCHAR2 (32) Y-- Department ID
- COMPANY_NO VARCHAR2 (4) Y-- Region
- NAMEVARCHAR2 (128) Y-- Name
- PARENT_NO VARCHAR2 (32) Y-- Parent department ID
Determine whether the sub-department can become the parent department of the father department and use recursion. SQL:
- SELECT COUNT(*)FROM(SELECTD. DEPT_NO, D. PARENT_NOFROMDepartment D
- CONNECT BY PRIORD. DEPT_NO = D. PARENT_NO
- STARTWITHD. DEPT_NO = # deptNo #) tWhereT. DEPT_NO = # parentNo #
Connect by prior... start with... is recursion. # DeptNo # And # parentNo # Represent the sub-department and the father department respectively.
If the return value is equal to 1, the sub-department cannot be the parent department of the father department; if the return value is not equal to 1, the opposite is true.