日期:2014-05-17 浏览次数:20862 次
--创建机构表
create table department (
dept_id number(10), --机构编号
parent_id number(10), --上级机构编号
dept_name varchar2(100), --机构名称
class number(10) --机构层级
);
--1级机构(总公司)
insert into department (dept_id,parent_id,dept_name,class) values (1,0,'上海总公司',1);
insert into department (dept_id,parent_id,dept_name,class) values (2,0,'北京总公司',1);
--2级机构(分公司)
insert into department (dept_id,parent_id,dept_name,class) values (3,1,'上海浦东分公司',2);
insert into department (dept_id,parent_id,dept_name,class) values (4,1,'上海普陀分公司',2);
insert into department (dept_id,parent_id,dept_name,class) values (5,2,'北京中关村分公司',2);
insert into department (dept_id,parent_id,dept_name,class) values (6,2,'北京海淀区分公司',2);
--3级机构(部门)
insert into department (dept_id,parent_id,dept_name,class) values (7,3,'上海浦东分公司销售1部',3);
insert into department (dept_id,parent_id,dept_name,class) values (8,3,'上海浦东分公司销售2部',3);
insert into department (dept_id,parent_id,dept_name,class) values (9,4,'上海普陀分公司销售1部',3);
insert into department (dept_id,parent_id,dept_name,class) values (10,4,'上海普陀分公司销售2部',3);
insert into department (dept_id,parent_id,dept_name,class) values (11,5,'北京中关村分公司销售1部',3);
insert into department (dept_id,parent_id,dept_name,class) values (12,5,'北京中关村分公司销售2部',3);
insert into department (dept_id,parent_id,dept_name,class) values (13,6,'北京海淀区分公司销售1部',3);
insert into department (dept_id,parent_id,dept_name,class) values (14,6,'北京海淀区分公司销售2部',3);
commit;
--创建用户表
create table usrtbl (
user_id number(10),
dept_id number(10),
user_name varchar2(10)
);
insert into usrtbl (user_id,dept_id,user_name) values (1,3,'陈志强');
commit;
--查询机构表
select d.dept_id,lpad('-',level*2,'--')||d.dept_name as dept_name from department d
start with d.parent_id = 0
connect by prior d.dept_id = d.parent_id;
ChenZw> SELECT DISTINCT D.DEPT_ID,LPAD('-',LEVEL*2,'--')
------解决方案--------------------
D.DEPT_NAME AS DEPT_NAME FROM DEPARTMENT D,USRTBL A
2&nb