SAP SELECT HIERARCHY NODE NAVIS
Get Example source ABAP code based on a different SAP table
• HIERARCHY_DESCENDANTS ABAP_OSQL_TABLE_FUNC
• HIERARCHY_ANCESTORS ABAP_OSQL_TABLE_FUNC
• HIERARCHY_SIBLINGS ABAP_OSQL_TABLE_FUNC
• SOURCE HIERARCHY_DESCENDANTS
• SOURCE HIERARCHY_ANCESTORS
• SOURCE HIERARCHY_SIBLINGS
• START WHERE HIERARCHY_DESCENDANTS
• START WHERE HIERARCHY_ANCESTORS
• START WHERE HIERARCHY_SIBLINGS
SELECT>, FROM hierarchy_node_navigator> ABAP_SYNTAX ... ${HIERARCHY_DESCENDANTS
$|HIERARCHY_ANCESTORS
$|HIERARCHY_SIBLINGS$}(
SOURCE hierarchy>
START WHERE sql_cond>
$[DISTANCE $[FROM n1$] $[TO n2$]$] ) ...>
ABAP_VARIANTS:
1 HIERARCHY_DESCENDANTS( ... )>
2 HIERARCHY_ANCESTORS( ... )>
3 HIERARCHY_SIBLINGS( ... )>
ABAP Addition
... DISTANCE $[FROM n1$] $[TO n2$]>
What does it do?
Specifies a hierarchy node navigator > as a data source data_source>> in an AB_SQL query>. It accesses and evaluates the SQL hierarchy > hierarchy>> specified after SOURCE>.
After START WHERE>, a logical expression sql_cond>> must be specified that contains conditions for hierarchy nodes > of the SQL hierarchy after SOURCE>. The columns of the hierarchy can also be used as operands here. Any hierarchy nodes that meet the conditions are the start nodes > of the hierarchy navigators. They evaluate the SQL hierarchy hierarchy>> specified after SOURCE >, starting from all start nodes, and insert the selected hierarchy nodes into their result set.
In addition to the hierarchy columns> of the SQL hierarchy specified after SOURCE>, the result set of each of these hierarchy navigators contains two further hierarchy columns:
START_RANK> with the type INT8>
START_ID> with the type SSTRING> and length 1333
In each row of the result set, these columns contain the values of the hierarchy columns> HIERARCHY_RANK> and NODE_ID> of the associated start nodes.
BEGIN_SECTION SAP_INTERNAL_HINT
Syntactically you can add AS tabalias> behind hierarchy, but it does not work.
END_SECTION SAP_INTERNAL_HINT
ABAP_VARIANT_1 HIERARCHY_DESCENDANTS( ... )>
What does it do?
The hierarchy navigator HIERARCHY_DESCENDANTS> selects all descendant nodes> of the start nodes> and the start nodes themselves, from the SQL hierarchy hierarchy>> specified after SOURCE>. In addition the hierarchy columns> of the SQL hierarchy specified after SOURCE>, plus START_RANK> and START_ID >, the result set contains a further hierarchy column, HIERARCHY_DISTANCE>, with the type INT4>, which contains the distance to the respective start node. This distance is the positive difference between the hierarchy levels HIERARCHY_LEVEL> of the individual child nodes> and the respective start nodes.
BEGIN_SECTION SAP_INTERNAL_HINT
Internally, the additional hierarchy column is defined in the CDS abstract entity DDDDL_HIERARCHY_DESCENDANTS>.
END_SECTION SAP_INTERNAL_HINT
ABAP_EXAMPLE_VX5
Call of the hierarchy navigator HIERARCHY_DESCENDANTS> in the class CL_DEMO_HIERARCHY_NAVIGATORS>>, where the hierarchy generator HIERARCHY> is specified as the source. When executed, CL_DEMO_HIERARCHY_NAVIGATORS>> demonstrates how this hierarchy navigator works. It also displays the value of the hierarchy column HIERARCHY_DISTANCE>.
ABEXA 01252
ABAP_EXAMPLE_END
ABAP_VARIANT_2 HIERARCHY_ANCESTORS( ... )>
What does it do?
The hierarchy navigator HIERARCHY_ANCESTORS> selects all ancestor nodes> of the start nodes>, as well as the start nodes themselves, from the SQL hierarchy specified after SOURCE>. In addition to the hierarchy columns> of the SQL hierarchy specified after SOURCE>, plus START_RANK> and START_ID>, the result set contains a further hierarchy column, HIERARCHY_DISTANCE>, with the type INT4>, which contains the distance to the respective start node. This distance is the negative difference between the hierarchy levels HIERARCHY_LEVEL> of the individual parent nodes> and the respective start nodes.
BEGIN_SECTION SAP_INTERNAL_HINT
Internally, the additional hierarchy column is defined in the CDS abstract entity DDDDL_HIERARCHY_ANCESTORS>.
END_SECTION SAP_INTERNAL_HINT
ABAP_EXAMPLE_VX5
Call of the hierarchy navigator HIERARCHY_ANCESTORS> in the class CL_DEMO_HIERARCHY_NAVIGATORS>>, where the CDS hierarchy DEMO_CDS_SIMPLE_TREE>> is specified as a source. When executed, this class demonstrates how this hierarchy navigator works. It also displays the value of the additional hierarchy column HIERARCHY_DISTANCE>.
ABEXA 01253
ABAP_EXAMPLE_END
ABAP_VARIANT_3 HIERARCHY_SIBLINGS( ... )>
What does it do?
The hierarchy navigator HIERARCHY_SIBLINGS> selects all sibling nodes> of the start nodes>, as well as the start nodes themselves, from the SQL hierarchy specified after SOURCE>. In addition to the hierarchy columns> of the SQL hierarchy specified after SOURCE>, plus START_RANK> and START_ID>, the result set contains a further hierarchy column, HIERARCHY_SIBLING_DISTANCE>, with the type INT4>, which contains the distance to the respective start node. This is the positive or negative difference between the numbers HIERARCHY_RANK > of the sibling nodes and the respective start nodes.
BEGIN_SECTION SAP_INTERNAL_HINT
Internally, the additional hierarchy column is defined in the CDS abstract entity DDDDL_HIERARCHY_SIBLINGS>.
END_SECTION SAP_INTERNAL_HINT
Latest notes:
If the values in the additional column HIERARCHY_SIBLING_DISTANCE> are accessed, the addition SIBLINGS ORDER BY> should be used in the SQL hierarchy specified after SOURCE>. Otherwise, the values remain undefined.
NON_V5_HINTS
ABAP_HINT_END
ABAP_EXAMPLE_VX5
Call of the hierarchy navigator HIERARCHY_SIBLINGS> in the class CL_DEMO_HIERARCHY_NAVIGATORS>>, where the CTE hierarchy> +hierarchy> is specified a source. When executed, this class demonstrates how this hierarchy navigator works. It also displays the value of the additional hierarchy column HIERARCHY_SIBLING_DISTANCE >.
ABEXA 01254
ABAP_EXAMPLE_END
• DISTANCE FROM TO HIERARCHY_DESCENDANTS
• DISTANCE FROM TO HIERARCHY_ANCESTORS
• DISTANCE FROM TO HIERARCHY_SIBLINGS
ABAP Addition
What does it do?
In the case of the hierarchy navigators HIERARCHY_DESCENDANTS> and HIERARCHY_ANCESTORS>, the addition DISTANCE> restricts the distance to the respective start node>. The addition DISTANCE> cannot be specified for the hierarchy navigator HIERARCHY_SIBLINGS>.
At least one of the additions FROM> or TO> must be specified in the order shown. n1> and n2> expect host variables>, host expressions>, or literals> with the type i>. Only the types b>, s>, or i> can be used. Only those hierarchy nodes are selected for which the value of the hierarchy column HIERARCHY_DISTANCE> is between the values of n1> and n2 >. If the value of n1> is greater than n2>, no hierarchy node is selected. If TO>, is not specified, there is no upper limit. If FROM>, is not specified, there is no lower limit.
Latest notes:
The function of the addition DISTANCE> can also be achieved using the comparison operator BETWEEN>> for the hierarchy column HIERARCHY_DISTANCE>. In this case, the selection is applied to the result set of the hierarchy navigator rather than being performed by the navigator itself.
To get hierarchy nodes in the result set of HIERARCHY_DESCENDANTS> , the interval boundaries must include positive values. To get hierarchy nodes in the result set of HIERARCHY_ANCESTORS>, the interval boundaries must include negative values.
To exclude the start nodes from the result set, the additions FROM > and TO> can be used as follows:
If FROM 1> is specified without the addition TO>, HIERARCHY_DESCENDANTS> selects only descendant nodes>
If TO -1> is specified without the addition FROM>, HIERARCHY_ANCESTORS> selects only the ancestor nodes>
NON_V5_HINTS
ABAP_HINT_END
ABAP_EXAMPLE_VX5
Call of the hierarchy navigator HIERARCHY_DESCENDANTS> in the class CL_DEMO_HIERARCHY_DISTANCE>> while specifying the addition DISTANCE>. When executed, this class demonstrates how this addition works. If, for example, the value 2> is specified for n1> and 3> is specified for n2>, only the grandchildren nodes and great-grandchildren nodes of the start node are selected. If -3> is specified for n1> and -2> is specified for n2>, the result set of HIERARCHY_DESCENDANTS > is empty. For these values, on the other hand, the result set of HIERARCHY_ANCESTORS> contains the grandparents and great-grandparents of the start node.
ABEXA 01255
ABAP_EXAMPLE_END