日期:2014-05-16  浏览次数:20501 次

在oracle中,查找当前编码的一级部门
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')