Oracle connect by prior 處理樹狀表

--基礎篇 --1.建表 create table department(departmentid integer,departmentname varchar2(30),upperdepartmentid integer,manager varchar2(30)); --2.建序列 create sequence department_seq start with 1 nocycle; --3.建觸發器 create trigger tr_department_id before insert on department for each row begin select department_seq.nextval into :new.departmentid from dual; end; --4.建存儲過程 create or replace procedure pr_add_department(deptName in varchar2,upperDeptName in varchar2) as pid integer; begin if nvl(upperDeptName,'0')='0' then pid:=0; else select departmentid into pid from department where departmentname=upperDeptName; end if; insert into department(departmentname,upperdepartmentid) values(deptName,pid); commit; end; --5.查詢 select * from department; --6.更新 update department set departmentname='會計電算化',upperdepartmentid=9 where departmentid=10; --7.添加樹狀表樹根 begin pr_add_department('江西財經學院',''); end; --8.添加分支 begin --pr_add_department('信息系','江西財經學院'); pr_add_department('會計專業','江西財經學院'); end; --9.查詢樹狀表 level 分層級 select departmentid,departmentname,upperdepartmentid,level from department connect by prior departmentid=upperdepartmentid start with upperdepartmentid=0
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.