這是在2002年給一個制衣廠做系統的時候寫的,請大家多批評指正!
create table Dept(
DepartNO varchar2(10),
DepartName varchar2(20),
TopNo varchar2(10));
insert into Dept values('001','董事會','0');
insert into Dept values('002','總裁辦','001');
insert into Dept values('003','財務部','001');
insert into Dept values('004','市場部','002');
insert into Dept values('005','公關部','002');
insert into Dept values('006','銷售部','002');
insert into Dept values('007','分銷處','006');
insert into Dept values('008','業務拓展處','004');
insert into Dept values('009','銷售科','007');
CREATE TABLE TempDept
(
TempdeptNo char(4), /*部門編號*/
TempdeptName varchar2(20), /*部門名稱*/
UpperTempdeptNo char(4), /*上級部門編號*/
UpperTempdeptName varchar2(20), /*上級部門名稱*/
TempdeptLevel number(4) /*部門所處的級別*/
);
select departname
from dept
connect by prior departno=topno
start with topno='0';
select a.departname top ,b.departname next
from dept a,dept b
where a.departno=b.topno;
select lpad(departno,level*2+length(departno),'\')
from dept
start with topno='0'
connect by prior departno=topno;
select departno,departname,level ||'\'|| topno
from dept
start with topno='0'
connect by prior departno=topno;
select lpad(departno,level*2+length(departno),' ')
from dept
start with topno='0'
connect by prior departno=topno;
select lpad(departno,level*3,' ')
from dept
start with topno='0'
connect by prior departno=topno;
create or replace function GetLevel(TempNo varchar2)
return varchar2
IS
Result varchar2(20);
cursor CurLevel(curTempNo varchar2)
is
select departNo
from dept
connect by prior departNo = TopNo
start with TopNo = curTempNo;
begin
open CurLevel;
for v_sor in CurLevel(TempNo) loop
Result:=Result || '/' || v_sor.curTempNo;
end loop;
return(Result);
close CurLevel;
dbms_output.put_line('success');
end GetLevel;
REM************************************************************
REM********** 生成部門之間形成的樹結構 ******************
REM********** 功能:輸入部門後自動產生該部門 ******************
REM********** 及其子部門之間的樹關系() ******************
REM********** 輸入參數:部門編號ParaDeptId ******************
REM************************************************************
CREATE OR REPLACE procedure getAllSubDeptId(ParaDeptId varchar2)
IS
DeptNo1 varchar2(4); /*存放部門編號*/
UpperDeptNo1 varchar2(4); /*存放該部門的上級部門編號*/
UpperDeptNo2 varchar2(4); /*存放查詢同級上級部門編號*/
UpperDeptNo3 varchar2(4); /*存放查詢的上級部門編號*/
level number; /*部門所處級別*/
DSql varchar2(100); /*動態SQL字符串*/
CURSOR getDept IS
select DepartNO,TopNo
from dept
where DepartNo != ParaDeptId;
BEGIN
--DSql :='create table tempdept(TempdeptNo,TempdeptName,UpperTempdeptNo,
--UpperTempdeptName,TempdeptLevel)';
--EXECUTE IMMEDIATE DSql;
DSql :='TRUNCATE TABLE TempDept';
dbms_output.put_line(0);
EXECUTE IMMEDIATE DSql;
dbms_output.put_line(1);
insert into TempDept values(ParaDeptId,Null,Null,Null,0);
dbms_output.put_line(2);
open getDept;
fetch getDept into DeptNo1,UpperDeptNo1;
WHILE ( SQLCODE = 0 )
LOOP
level := 1 ;
UpperDeptNo3 := UpperDeptNo1;
WHILE ( UpperDeptNo1 is NOT NULL )
loop
if (UpperDeptNo1 = ParaDeptId) then
insert into tempdept values(DeptNo1,NULL,UpperDeptNo3,NULL,level);
exit;
else
UpperDeptNo2 := UpperDeptNo1;
select topNo into UpperDeptNo1
from dept
where Departno= UpperDeptNo2;
if UpperDeptNo2 != UpperDeptNo1
then
level := level+1;
end if;
end if;
END LOOP;
fetch getDept into DeptNo1,UpperDeptNo1;
END LOOP;
close getDept;
END;
/