All skills
secondsky avatar

/sap-abap

@620a19a
by Eddiesecondsky/sap-skills456 stars
120

Comprehensive ABAP development skill for SAP systems. Use when writing ABAP code, working with internal tables, structures, ABAP SQL, object-oriented programming, RAP (RESTful Application Programming Model), CDS views, EML statements, ABAP Cloud development, string processing, dynamic programming, RTTI/RTTC, field symbols, data references, exception handling, or ABAP unit testing. Covers both classic ABAP and modern ABAP for Cloud Development patterns.

Use this Skill: https://skilld.dev/gh/secondsky/sap-skills/sap-abap

This session only. Nothing lands on disk.

referencessql-hierarchies.md

≈1.3k tokens on demand. Your agent reads this file only when SKILL.md points to it.

ABAP SQL Hierarchies - Complete Reference

Source: https://github.com/SAP-samples/abap-cheat-sheets/blob/main/10_ABAP_SQL_Hierarchies.md

Version Note: SQL hierarchies require CTEs (WITH) which are available from 7.51+. FINAL(...) in code examples requires 7.50+ — replace with DATA(...) on 7.40. Hierarchy navigators are available from 7.51+.


CTE Hierarchies

Common Table Expressions can declare SQL hierarchies using WITH HIERARCHY.

CDS Hierarchy as CTE Source

WITH
  +tree AS
    ( SELECT FROM demo_cds_simple_tree( p_id = @root_id )
             FIELDS * )
      WITH HIERARCHY demo_cds_simple_tree
  SELECT FROM +tree
         FIELDS id, parent, name, hierarchy_rank,
                hierarchy_tree_size, hierarchy_level
         INTO TABLE @FINAL(cte_cds_result).

HIERARCHY Generator as CTE Source

WITH
  +tree AS
    ( SELECT FROM HIERARCHY(
        SOURCE demo_cds_simple_tree_view
        CHILD TO PARENT ASSOCIATION _tree
        START WHERE id = @root_id
        SIBLINGS ORDER BY id
        MULTIPLE PARENTS NOT ALLOWED ) AS asql_hierarchy
        FIELDS id, parent, name )
      WITH HIERARCHY asql_hierarchy
   SELECT FROM +tree
         FIELDS id, parent, hierarchy_distance
         INTO TABLE @FINAL(cte_asql_result).

CTE with Internal Associations

WITH
  +cte_simple_tree_source AS
    ( SELECT FROM demo_simple_tree
             FIELDS id, parent_id AS parent, name )
       WITH ASSOCIATIONS (
         JOIN TO MANY +cte_simple_tree_source AS _tree
           ON +cte_simple_tree_source~parent = _tree~id ),
  +tree AS
    ( SELECT FROM HIERARCHY(
        SOURCE +cte_simple_tree_source
        CHILD TO PARENT ASSOCIATION _tree
        START WHERE id = @root_id
        SIBLINGS ORDER BY id
        MULTIPLE PARENTS NOT ALLOWED ) AS cte_hierarchy
        FIELDS id, parent, name )
        WITH HIERARCHY cte_hierarchy
  SELECT FROM +tree
         FIELDS id, parent, hierarchy_rank
         INTO TABLE @FINAL(cte_cte_result).

HIERARCHY Generator

Create SQL hierarchies directly in ABAP:

DATA root_id TYPE demo_cds_simple_tree_view-id.

SELECT FROM HIERARCHY( SOURCE demo_cds_simple_tree_view
                       CHILD TO PARENT ASSOCIATION _tree
                       START WHERE id = @root_id
                       SIBLINGS ORDER BY id
                       MULTIPLE PARENTS NOT ALLOWED )
       FIELDS id, parent, name, hierarchy_rank,
              hierarchy_tree_size, hierarchy_parent_rank,
              hierarchy_level, hierarchy_is_cycle,
              hierarchy_is_orphan, node_id, parent_id
       INTO TABLE @FINAL(asql_cds_result).

Key Parameters

Parameter Description
SOURCE Hierarchy source (CDS view or CTE)
CHILD TO PARENT ASSOCIATION Self-association for relationships
START WHERE Root node conditions
SIBLINGS ORDER BY Sort order for siblings
MULTIPLE PARENTS NOT ALLOWED Single/multiple parent control

Hierarchy Navigators

HIERARCHY_DESCENDANTS

SELECT FROM HIERARCHY_DESCENDANTS(
              SOURCE demo_cds_simple_tree( p_id = @root_id )
              START WHERE id = @sub_id )
  FIELDS id, parent_id, name, hierarchy_distance
  INTO TABLE @FINAL(descendants).

HIERARCHY_ANCESTORS

SELECT FROM HIERARCHY_ANCESTORS(
              SOURCE demo_cds_simple_tree( p_id = @root_id )
              START WHERE id = @max_id )
  FIELDS id, parent_id, name, hierarchy_distance
  INTO TABLE @FINAL(ancestors).

HIERARCHY_SIBLINGS

SELECT FROM HIERARCHY_SIBLINGS(
              SOURCE demo_cds_simple_tree( p_id = @root_id )
              START WHERE id = @sibl_id )
  FIELDS id, parent_id, name, hierarchy_sibling_distance
  INTO TABLE @FINAL(siblings).

HIERARCHY_DESCENDANTS_AGGREGATE

TYPES:
  BEGIN OF value,
    id     TYPE i,
    amount TYPE p LENGTH 16 DECIMALS 2,
  END OF value.

DATA value_tab TYPE SORTED TABLE OF value WITH UNIQUE KEY id.

SELECT FROM HIERARCHY_DESCENDANTS_AGGREGATE(
              SOURCE demo_cds_simple_tree( p_id = @sub_id ) AS h
              JOIN @value_tab AS v
                ON v~id = h~id
              MEASURES SUM( v~amount ) AS amount_sum
              WHERE hierarchy_rank > 1
              WITH SUBTOTAL
              WITH BALANCE )
  FIELDS id, amount_sum, hierarchy_rank, hierarchy_aggregate_type
  INTO TABLE @FINAL(descendants_aggregate).

Aggregate Parameters

Parameter Description
MEASURES Aggregate functions (SUM, COUNT, etc.)
WHERE Result set restrictions
WITH SUBTOTAL Sum of qualifying nodes
WITH BALANCE Sum of non-qualifying nodes

Implicit Hierarchy Columns

Column Description
hierarchy_rank Position in traversal order
hierarchy_tree_size Count of descendants + self
hierarchy_parent_rank Rank of parent node
hierarchy_level Depth from root
hierarchy_is_cycle Cycle detection flag
hierarchy_is_orphan Orphan node flag
hierarchy_distance Distance from start node

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    A comprehensive reference for SAP ABAP development, providing documentation and code examples for both classic ABAP and modern ABAP Cloud patterns. No security issues were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    3/32 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 620a19a. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 weeks ago.

Activeupdated 2 months ago
Other metadata
metadata
{
  "maintainer": "Eduard Jiglau",
  "maintainer_email": "hello@sap-ai-skills.com",
  "website": "https://sap-ai-skills.com",
  "version": "2.4.1",
  "last_verified": "2026-04-02",
  "abap_release": "7.40 SP08+ / 7.50+ / ABAP Cloud",
  "sources": [
    "https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm",
    "https://github.com/SAP-samples/abap-cheat-sheets"
  ]
}

README badge

README badge for secondsky/sap-skills/sap-abap