Free Newsletters:
DatabaseDaily  
Database Journal
Search Database Journal:
 
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» DBA Jobs
» Sitemap

News Via RSS Feed


follow us on Twitter





Brocade Doubles Down on 16 Gbps Fibre Channel

Microsoft Wants iOS Apps to Run on WP7

Avaya Debuts New Virtual Services Switch
Database Journal |DBA Support |SQLCourse |SQLCourse2







Technical Specialist – Pre-sales (MA)
Next Step Systems
US-MA-Littleton

Justtechjobs.com Post A Job | Post A Resume

Featured Database Articles

SQL Scripts & Samples

March 30, 2009

Hierarchial Loop II

By DatabaseJournal.com Staff



>>Script Language and Platform: Oracle
This script displays all of the employees in SCOTT.EMP table, with all of the bosses they report to, in a hierarchial loop, along with their level in the hierarchy.

Author: JP Vijaykumar


The business wants all of the employees in SCOTT.EMP table be displayed, with all of the bosses they report to, in a hierarchial loop, along with their level in the hierarchy. For this, I created a function to display the desired results.

create or replace function hierarchial_loop_jp(v_emp in number)
return varchar2 as
/*****************************************************
Author JP Vijaykumar, Oracle DBA, 
Date  Sep 30th 2007
This function takes one argument empno
*****************************************************/
v_out varchar2(3000);
begin 
for c1 in (select empno, level from scott.emp
    connect by prior mgr = empno start with empno = v_emp) loop
if (v_out IS NULL) then
v_out:=c1.level||':'||c1.empno;
else
v_out:=v_out||'::'||c1.level||':'||c1.empno;
end if;
end loop;
return v_out;
end;
select hierarchial_loop_jp(empno) from scott.emp;
HIERARCHIAL_LOOP3_JP(EMPNO)
-----------------------------------------------------------------------
1:7369::2:7902::3:7566::4:7839
1:7499::2:7698::3:7839
1:7521::2:7698::3:7839
1:7566::2:7839
1:7654::2:7698::3:7839
1:7698::2:7839
1:7782::2:7839
1:7788::2:7566::3:7839
1:7839
1:7844::2:7698::3:7839
1:7876::2:7788::3:7566::4:7839
1:7900::2:7698::3:7839
1:7902::2:7566::3:7839
1:7934::2:7782::3:7839

The business wants the level to be displayed in the reverse order, which means the level of KING should always be 1.

create or replace function hierarchial_loop_jp(v_emp in number)
return varchar2 as
/*****************************************************
Author JP Vijaykumar, Oracle DBA, 
Date  Sep 30th 2007
This function takes one argument empno
*****************************************************/
v_out varchar2(3000);
v_lvl number;
begin 
select max(level) into v_lvl from scott.emp
       connect by prior mgr = empno start with empno = v_emp;
for c1 in (select empno from scott.emp
           connect by prior mgr = empno start with empno = v_emp) loop
if (v_out IS NULL) then
v_out:=v_lvl||':'||c1.empno;
else
v_out:=v_out||'::'||v_lvl||':'||c1.empno;
end if;
v_lvl:=v_lvl - 1;
end loop;
return v_out;
end;

select hierarchial_loop_jp(empno) from scott.emp;
HIERARCHIAL_LOOP3_JP(EMPNO)
-----------------------------------------------------------------------
4:7369::3:7902::2:7566::1:7839
3:7499::2:7698::1:7839
3:7521::2:7698::1:7839
2:7566::1:7839
3:7654::2:7698::1:7839
2:7698::1:7839
2:7782::1:7839
3:7788::2:7566::1:7839
1:7839
3:7844::2:7698::1:7839
4:7876::3:7788::2:7566::1:7839
3:7900::2:7698::1:7839
3:7902::2:7566::1:7839
3:7934::2:7782::1:7839
14 rows selected.

Disclaimer: We hope that the information on these script pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, or fitness for a particular purpose... Disclaimer Continued


Back to Database Journal Home

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

SQL Scripts & Samples Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 



Latest Forum Threads
SQL Scripts & Samples Forum
Topic By Replies Updated
solving query svibuk 1 February 3rd, 09:08 AM
converting from a character string to uniqueidentifier saturnius 4 January 4th, 08:56 AM
SQL query relaxedgalaxy 5 December 7th, 08:11 AM
Projection of times on timeline ohv 2 December 6th, 12:47 AM