SAP ITAB CLASS ATTRIBUTES AS KEY

Get Example source ABAP code based on a different SAP table
  


ARTICLE

Attributes of Objects as Internal Table Keys
If the line type of an internal table includes an object reference variable from ABAP Objects in the form of the component comp, the attributes attr of the object to which the reference in a line points can be used as key values for reading, sorting, and changing table lines. In principle this is possible using statements, which address individual components of the table, for example:

LOOP AT itab ... WHERE comp->attr ...
READ TABLE itab ... WITH KEY comp->attr = ...
SORT itab BY comp->attr ...
DELETE itab WHERE comp->attr ...
MODIFY itab ... WHERE comp->attr ...
READ|DELETE ... COMPARING comp->attr ...
If a table contains lines without structure with the type of a reference variable, the attributes of the object to which a line points can be addressed using TABLE_LINE->attr.

Notes
You can use the attribute access, provided that all references of the relevant component indicate one object, that is are not initial. Otherwise a runtime error will occur if the system attempts to read the attribute value.
When using the TRANSPORTING addition to the statements READ, LOOP, and so on, accessing attributes is not permitted.


Example CLASS c1 DEFINITION.
PUBLIC SECTION.
DATA a1 TYPE i.
ENDCLASS.

DATA: BEGIN OF itab_line,
col1 TYPE i,
col2 TYPE REF TO c1,
END OF itab_line.

DATA itab LIKE HASHED TABLE OF itab_line WITH UNIQUE KEY col1.

DO 5 TIMES.
itab_line-col1 = sy-index.
CREATE OBJECT itab_line-col2.
itab_line-col2->a1 = sy-index ** 2.
INSERT itab_line INTO TABLE itab.
ENDDO.

* SORT

SORT itab BY col2->a1 DESCENDING.

* DELETE

DELETE itab WHERE col2->a1 = 9.

* READ

READ TABLE itab INTO itab_line WITH KEY col2->a1 = 4.
WRITE: / itab_line-col1, itab_line-col2->a1.
SKIP.

* MODIFY

itab_line-col2->a1 = 100.
MODIFY itab FROM itab_line TRANSPORTING col2
WHERE col2->a1 = 4.

* LOOP

LOOP AT itab INTO itab_line WHERE col2->a1 > 1.
WRITE: / itab_line-col1, itab_line-col2->a1.
ENDLOOP.