If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Recursive query help required !!!!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-12-08, 23:33
jayshah7 jayshah7 is offline
Registered User
 
Join Date: Feb 2008
Posts: 35
Recursive query help required !!!!

Hi all,
I have a recursive query to be written in db2, fow which referring to IBM doc I wrote the following query. The output I need is to on entering the emp_code, all the employees wrking under it recursively. The problem is after running the query the server goes in hang state and the progress bar just goes on, Can anyone help out with the cause ??


WITH RPL (empcode, functional_reporting_to) AS
(

SELECT ROOT.functional_reporting_to, ROOT.empcode
FROM db2admin.employee_details ROOT
WHERE ROOT.functional_reporting_to = 'emp500'

UNION ALL

SELECT CHILD.functional_reporting_to, CHILD.empcode
FROM RPL PARENT, db2admin.employee_details CHILD
WHERE PARENT.empcode = CHILD.functional_reporting_to
and child.functional_reporting_to = 'emp500'


)

SELECT DISTINCT functional_reporting_to, empcode
FROM RPL
ORDER BY functional_reporting_to, empcode
Reply With Quote
  #2 (permalink)  
Old 11-13-08, 02:36
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
[...]
UNION ALL

SELECT CHILD.functional_reporting_to, CHILD.empcode
FROM RPL PARENT, db2admin.employee_details CHILD
WHERE PARENT.empcode = CHILD.functional_reporting_to
and child.functional_reporting_to = 'emp500'
[...]

This condition is erroneous.
employees, which are added in the recursion do not report to emp500. They report to employees which report to employees which report to employees which ... report to emp500.

Just omit this condition.


You might want to include an counter to limit the number of iterations ( to avoid infinite loops ):



WITH RPL (empcode, functional_reporting_to, level) AS
(
SELECT ROOT.functional_reporting_to, ROOT.empcode , 1 as level
FROM db2admin.employee_details ROOT
WHERE ROOT.functional_reporting_to = 'emp500'

UNION ALL

SELECT CHILD.functional_reporting_to, CHILD.empcode, parent.level + 1 as level
FROM RPL PARENT, db2admin.employee_details CHILD
WHERE PARENT.empcode = CHILD.functional_reporting_to
and parent.level < 10
)
SELECT DISTINCT functional_reporting_to, empcode, level
FROM RPL
ORDER BY functional_reporting_to, empcode ;
Reply With Quote
  #3 (permalink)  
Old 11-13-08, 04:58
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
and child.functional_reporting_to = 'emp500'
[...]

This condition is erroneous.
.....
.....

Just omit this condition.
Additional to that, I thought column names should be changed to the following.
WITH RPL (functional_reporting_to, empcode) AS
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On