树状结构
存储过程中通过递归构建,类似BBS回帖显示,代码共三段:
建表,插入数据,创建存储过程显示;
1.
create table article
(
id number primary key,
cont varchar2(4000),
pid number,
idleaf number(1), --0为非叶子节点,1为叶子节点
alevel number(2)
);
2.
insert into article values(1,‘蚂蚁大战大象‘,0,0,0);
insert into article values(2,‘大象被打趴下了‘,1,0,1);
insert into article values(3,‘蚂蚁也不好过‘,1,0,1);
insert into article values(4,‘瞎说‘,2,1,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);
3.
create or replace procedure tree(v_pid article.pid%type,v_alevel article.alevel%type)
is
cursor c is select * from article where pid=v_pid;
v_str varchar2(2048) :=‘ ‘;
begin
for i in 0..v_alevel loop
v_str :=v_str||‘ ‘;
end loop;
for v_article in c loop dbms_output.put_line(v_str||v_article.cont);
if (v_article.isleaf =0) then
tree(v_article.id,v_article.alevel);
end if;
end loop;
end;
结果前面的N*4个空格不显示:
设置空格输出后,问题解决:
其它问题:第一层的输出前不应该带空格,设置初始字符串为空,i从1开始。
设置 oracle sqlplus DBMS_OUTPUT.put_line
下面内容摘录自
http://blog.csdn.net/wbj1234566/article/details/2557515
1 设置DBMS_OUTPUT.put_line输出显示 SET SERVEROUTPUT ON
2 关闭DBMS_OUTPUT.put_line输出显示 SET SERVEROUTPUT OFF
3 设置DBMS_OUTPUT缓冲区大小 SET SERVEROUTPUT ON SIZE number(1000)
4 设置只显示DBMS_OUTPUT默认的长度 SET SERVEROUTPUT ON FORMAT TRUNCATED
5 设置空格的输出 SET SERVEROUTPUT ON FORMAT WRAPPED
6 取消空格的输出 SET SERVEROUTPUT ON FORMAT WORD_WRAPPED