树状结构的存储与展示(利用递归):

 

创建论坛表存储文章:

create table article(
id number primary key,
cont varchar2(4000),
pid number, //父节点
isleaf number(1), //0代表非叶子节点,1代表叶子节点
alevel number(2) //所属等级
)

 

创建存储过程用树状结构存储文章:

create or replace procedure p(v_pid article.pid%type,v_level binary_integer) is
cursor c is select * from article where pid = v_pid;
v_preStr varchar2(1024) := '';
begin
for i in 1..v_level loop
v_preStr := v_preStr || '****';
end loop;

for v_article in c loop
dbms_output.put_line(v_preStr || v_article.cont);
if(v_article.isleaf = 0) then
p(v_article.id,v_level + 1);
end if;
end loop;
end;

 

输入文章:

insert into article values(1,'瞎说',0,0,0);
insert into article values(2,'大象被打趴下了',1,0,1);
insert into article values(3,'蚂蚁也不好过',2,1,2);
insert into article values(4,'瞎说',2,0,2);
insert into article values(5,'没有瞎说',4,1,3);
insert into article values(6,'大象进医院了',1,0,1);
insert into article values(7,'护士是蚂蚁',6,1,2);
insert into article values(8,'怎么可能',6,1,2);
insert into article values(9,'怎么没有可能',2,0,2);
insert into article values(10'可能性是很大的',9,1,3);

 

最后得出结果。

                                  _____mosquito.eq

posted on 2014-03-27 16:52  mosquito_real  阅读(442)  评论(0编辑  收藏  举报