Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Query to Find Root ID in ABAP

Former Member
0 Kudos

Suppose we have a database table with values as following :

ItemID -


ParentID

A1 -


Null

A2 -


A1

A3 -


A1

A4 -


A3

A5 -


A3

Now I want to find the Root ParentID of A5 , that would be A1 in this case as the corresponding ParentID of A1 is NULL.

How should I get the desired result ?

Thank You

Saurabh

Edited by: Saurabh Kapoor on Sep 23, 2010 8:02 AM

3 REPLIES 3

Former Member
0 Kudos

Hi Saurabh,

Use this peace of code....


types : BEGIN OF x_data,
        child type char2,
        a      type i,
        end of x_data.

data : wa_data type x_data.

select single a~child count(*) as a into wa_data
  from zamit_test as a INNER JOIN zamit_test as b
       on a~parent ne b~child
  GROUP BY a~child.

IF sy-subrc = 0.
  BREAK-POINT.
ENDIF.

In the o/p workarea the value of child is the top most node.

Amitava

Edited by: Amitava De on Sep 23, 2010 1:32 PM

Former Member
0 Kudos

Hi

Get all the data from database to an internal table IT with clause ParentID NE null

Now read the internal table into a do - enddo loop

read table it into wa with key ItemID = A5

If it fids the entry continue reading this by passing the ParantID into the Item id

If it doesn't find any further entry, EXIT, The last entry is your desired record.

Former Member
0 Kudos

Thanks @ all.

Though I have found a recursive way to it.

Regards

Saurabh