Implement stored procedures in the Oracle Tree Structure

Source: Internet
Author: User

This article mainly describes how to implement the stored procedure in the Oracle tree structure, including the actual operation steps for creating the IT table, and how to insert data to the relevant data, submit related transactions, and create a stored procedure p.

Step 1: Create a table IT

 
 
  1. create table IT  
  2. (  
  3. id number primary key ,  
  4. cont varchar2(255),  
  5. pid number,  
  6. isleaf number(1),   

0 indicates that non-leaf node 1 indicates a leaf node.

 
 
  1. grade number(2)  
  2. );  

Step 2: insert data into the data and submit the transaction

Insert into IT values (1, 'Hello all ', 0, 0, 0 );

Insert into IT values (2, 'you', 1, 0, 1 );

Insert into IT values (3. 'May you succeed in your studies ', 2, 1, 2 );

Commit; -- submit a transaction

 
 
  1. select * from it 

Step 3: Create a stored procedure p

 
 
  1. create or replace procedure p (v_pid IT.pid%type, v_level binary_integer)  
  2. is  
  3. cursor c is select * from IT where pid = v_pid;  
  4. v_preStr varchar2(1024) := '';  
  5. begin  
  6. for i in 1..v_level  
  7. loop  
  8. v_preStr := v_preStr || '**$';  
  9. end loop;  
  10. for v_IT in c  
  11. loop  
  12. dbms_output.put_line(v_preStr || v_IT.cont);  
  13. if(v_IT.isleaf = 0) then  
  14. p(v_IT.id, v_level + 1);  
  15. end if;  
  16. end loop;  
  17. end;  

Step 4: Start serveroutput and use the exec command to display the tree in pl/SQL

Run in Command Window

 
 
  1. SQL> set serveroutput on  
  2. SQL> exec p(0,0);  

The preceding content describes the stored procedure of the Oracle tree structure. We hope it will help you in this regard.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.