在oracle中,查找当前编码的一级部门
程序员文章站
2022-07-10 16:21:00
...
create table A
(
DEPTID INTEGER not null, --部门ID
DEPT_CODE VARCHAR2(720) not null, --部门编码
PARENT_ID VARCHAR2(720) not Null --父节点('-1'为*节点,'-'1的下级节点为一级部门)
);
alter table A add constraint DEPT_CODE_PK primary key (DEPT_CODE);
Insert Into A (DEPTID,DEPT_CODE,PARENT_ID) Values('1004', '106707000000', '281' );
Insert Into A (DEPTID,DEPT_CODE,PARENT_ID) Values('281' , '106700000000', '43' );
Insert Into A (DEPTID,DEPT_CODE,PARENT_ID) Values('43' , '100000000000', '7289');
Insert Into A (DEPTID,DEPT_CODE,PARENT_ID) Values('7289', '000000000000', '-1' );
Commit;
--查找当前编码的一级部门
With all_id As (
Select t.parent_id , t.deptid From A t
Start With t.dept_code = '106707000000'
Connect By Prior t.parent_id = t.deptid )
Select deptid From all_id a Where a.parent_id =
(Select b.deptid From all_id b Where b.parent_id = '-1')
(
DEPTID INTEGER not null, --部门ID
DEPT_CODE VARCHAR2(720) not null, --部门编码
PARENT_ID VARCHAR2(720) not Null --父节点('-1'为*节点,'-'1的下级节点为一级部门)
);
alter table A add constraint DEPT_CODE_PK primary key (DEPT_CODE);
Insert Into A (DEPTID,DEPT_CODE,PARENT_ID) Values('1004', '106707000000', '281' );
Insert Into A (DEPTID,DEPT_CODE,PARENT_ID) Values('281' , '106700000000', '43' );
Insert Into A (DEPTID,DEPT_CODE,PARENT_ID) Values('43' , '100000000000', '7289');
Insert Into A (DEPTID,DEPT_CODE,PARENT_ID) Values('7289', '000000000000', '-1' );
Commit;
--查找当前编码的一级部门
With all_id As (
Select t.parent_id , t.deptid From A t
Start With t.dept_code = '106707000000'
Connect By Prior t.parent_id = t.deptid )
Select deptid From all_id a Where a.parent_id =
(Select b.deptid From all_id b Where b.parent_id = '-1')
上一篇: 低血糖饮食要均衡
下一篇: linux 下awk的应用
推荐阅读