![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
----来自网上
--
--第一步:创建表 IT
create table IT
(
id number primary key ,
cont varchar2 ( 255 ),
pid number ,
isleaf number ( 1 ), -- 0代表非叶子节点 1代表叶子节点。
grade number ( 2 )
);
-- --第二步:向数据中插入数据并提交事务
insert into IT values ( 1 , ' 大家好 ' , 0 , 0 , 0 );
insert into IT values ( 2 , ' 你也好 ' , 1 , 0 , 1 );
insert into IT values ( 3 , ' 愿大家学业有成 ' , 2 , 1 , 2 );
commit ; -- 提交事务
-- ---------------- select * from it
-- --第三步:创建存储过程p
create or replace procedure p (v_pid IT.pid % type, v_level binary_integer)
is
cursor c is select * from IT where pid = v_pid;
v_preStr varchar2 ( 1024 ) : = '' ;
begin
for i in 1 ..v_level
loop
v_preStr : = v_preStr || ' **$ ' ;
end loop;
for v_IT in c
loop
dbms_output.put_line(v_preStr || v_IT.cont);
if (v_IT.isleaf = 0 ) then
p(v_IT.id, v_level + 1 );
end if ;
end loop;
end ;
-- --第四步:启动serveroutput 利用exec命令实现树形展示 在pl/sql中的
-- --Command Window下运行
SQL > set serveroutput on
SQL > exec p( 0 , 0 );
-- --运行结果
/*
大家好
**$你也好
**$**$愿大家学业有成
*/
create table IT
(
id number primary key ,
cont varchar2 ( 255 ),
pid number ,
isleaf number ( 1 ), -- 0代表非叶子节点 1代表叶子节点。
grade number ( 2 )
);
-- --第二步:向数据中插入数据并提交事务
insert into IT values ( 1 , ' 大家好 ' , 0 , 0 , 0 );
insert into IT values ( 2 , ' 你也好 ' , 1 , 0 , 1 );
insert into IT values ( 3 , ' 愿大家学业有成 ' , 2 , 1 , 2 );
commit ; -- 提交事务
-- ---------------- select * from it
-- --第三步:创建存储过程p
create or replace procedure p (v_pid IT.pid % type, v_level binary_integer)
is
cursor c is select * from IT where pid = v_pid;
v_preStr varchar2 ( 1024 ) : = '' ;
begin
for i in 1 ..v_level
loop
v_preStr : = v_preStr || ' **$ ' ;
end loop;
for v_IT in c
loop
dbms_output.put_line(v_preStr || v_IT.cont);
if (v_IT.isleaf = 0 ) then
p(v_IT.id, v_level + 1 );
end if ;
end loop;
end ;
-- --第四步:启动serveroutput 利用exec命令实现树形展示 在pl/sql中的
-- --Command Window下运行
SQL > set serveroutput on
SQL > exec p( 0 , 0 );
-- --运行结果
/*
大家好
**$你也好
**$**$愿大家学业有成
*/