Sie sind auf Seite 1von 7

ABAP 740 Table Expressions to Read &

Modify ITAB line

Read and Modify the ITAB records without using the temporary work area or variables. It is possible with
using the ABAP Table Expressions. Lets see more about them.

Preface
Let me start with begging your apologies for not publishing any article in long time. I had to take a
pause . I would try to publish as many as I can now.

ABAP 740 has many new features. This new feature Table Expressions are also powerful and reduce the
code clutter.

There are two type of them:

For Reading the data These are known as Reader Positions. This would do the same job as
you do with the READ statement, but without using the local variables. You can access the row in
question directly by specifying the key.
For Writing the data This has same effect as the MODIFY statement on the table entry. With
using the Write Positions, you can read the data change the value directly.

How to use Table Expressions


To use the table expression,
You need use the square bracket [ ]. Within the bracket, you would need to specify the component
you want to use as the key.
Dont get confused with the table specification when you were using the HEADER LINE, e.g.
ITAB[]. If you are still using the HEADER LINE, you would need to stop doing that and upgrade to
more newer forms of Table definitions
When table entry doesnt exist, a catchable exception CX_SY_ITAB_LINE_NOT_FOUND is raised.
There are few exceptions when you dont need to catch it. But lets discuss that in the next article.
You can have multiple table expression chained together like [ ]-[ ]. You must enjoy this when using
the Deep structure reading. See example 5 here.

Read More at SAP Help

Lets see few examples.

Example 1 Reading table entry using the Key TABLE_LINE


In here, the table has only one component TABLE_LINE. Check out both Old and New way of reading the
data.

DATA: t_data TYPE STANDARD TABLE OF i.

DO 10 TIMES.
APPEND sy-INDEX TO t_data.
ENDDO.

*Old way
DATA: lv_data TYPE i.
DO 12 TIMES.
READ TABLE t_data INTO lv_data WITH KEY TABLE_LINE = sy-INDEX.
IF sy-subrc EQ 0.
WRITE: / lv_data.
ELSE.
WRITE: / 'Not Found', sy-INDEX.
ENDIF.
ENDDO.

* New Way using Table Expressions


DO 12 TIMES.
TRY .
WRITE: / t_data[ TABLE_LINE = sy-INDEX ].
CATCH cx_sy_itab_line_not_found.
WRITE: / 'Not found', sy-INDEX.
ENDTRY.
ENDDO.

Example 2 Modify the Table Entry using the Key


Same table as the previous example, but now modifying the entry.

* Modify

* Old Way
FIELD-SYMBOLS: <lfs_data> LIKE LINE OF t_data.
READ TABLE t_data ASSIGNING <lfs_data> WITH KEY TABLE_LINE = 10.
IF sy-subrc EQ 0.
<lfs_data> = 20.
ENDIF.

* New Way
t_data[ TABLE_LINE = 20 ] = 10.

In Debug, Before the Modify statement

After executing the statement

Example 3 Modify the Table Entry using the Key which


doesnt exist
Demo of the exception handling CX_SY_ITAB_LINE_NOT_FOUND

* Modify when entry doesn't exist

* OLD
READ TABLE t_data ASSIGNING <lfs_data> WITH KEY TABLE_LINE = 11.
IF sy-subrc EQ 0.
<lfs_data> = 20.
ENDIF.

* New Way
TRY .
t_data[ TABLE_LINE = 11 ] = 10.
CATCH cx_sy_itab_line_not_found.
ENDTRY.

Example 4 Reading table entry with components


As you see here, you can read the entry and access the component of the result workarea directly, no
need of temporary work area.

* Multiple Components
TYPES:
BEGIN OF ty_data,
kunnr TYPE kunnr,
name1 TYPE name1,
ort01 TYPE ort01,
land1 TYPE land1,
END OF ty_data.
TYPES: tt_data TYPE STANDARD TABLE OF ty_data
WITH DEFAULT KEY.
*
DATA(itab_multi_comp) =
VALUE tt_data( ( kunnr = '123' name1 = 'ABCD' ort01 = 'LV' land1 = 'NV' )
( kunnr = '456' name1 = 'XYZ' ort01 = 'LA' land1 = 'CA' )
).

* Old way to access


DATA ls_comp TYPE ty_data.
READ TABLE itab_multi_comp INTO ls_comp WITH KEY kunnr = '123'.
WRITE: / ls_comp-ort01.

* NEW way to access


WRITE: / itab_multi_comp[ kunnr = '123' ]-ort01.

Example 5 Reading and Modifying Table entry in Deep


Table
Demo of chaining table expressions when working with the Deep Table. Also notice, how the ASSIGN is
used to directly the needed component instead of the other helpers. This example also shows on how to
use VALUE operator.

* Itab Deep
TYPES:
BEGIN OF ty_alv_data,
kunnr TYPE kunnr,
name1 TYPE name1,
ort01 TYPE ort01,
land1 TYPE land1,
t_color TYPE lvc_t_scol,
END OF ty_alv_data.
TYPES: tt_alv_data TYPE STANDARD TABLE OF ty_alv_data
WITH DEFAULT KEY.

DATA(itab_alv) =
VALUE tt_alv_data(
"First Row
( kunnr = '123' name1 = 'ABCD'
ort01 = 'LV' land1 = 'NV'
" color table
t_color = VALUE #(
" Color table - First Row
( fname = 'KUNNR'
color-col = COL_NEGATIVE
color-int = 0
color-inv = 0
)
" Color Table - 2nd Row
( fname = 'ORT01'
color-col = COL_TOTAL
color-int = 1
color-inv = 1
)
)
)
"Second row
( kunnr = '456' name1 = 'XYZ'
ort01 = 'LA' land1 = 'CA'
)
).

* Old Way
FIELD-SYMBOLS: <fs_data> LIKE LINE OF itab_alv,
<fs_col> LIKE LINE OF <fs_data>-t_color.
READ TABLE itab_alv ASSIGNING <fs_data> WITH KEY kunnr = '123'.
IF sy-subrc EQ 0.
READ TABLE <fs_data>-t_color ASSIGNING <fs_col> WITH KEY fname = 'ORT01'.
IF sy-subrc EQ 0.
<fs_col>-color-col = COL_GROUP.
<fs_col>-color-int = 0.
<fs_col>-color-inv = 1.
ENDIF.
ENDIF.

* Table Expressions - New Way


TRY .
itab_alv[ kunnr = '123' ]-t_color[ fname = 'ORT01' ]-color-col = COL_BACKGROUND.
ASSIGN itab_alv[ kunnr = '123' ]-t_color[ fname = 'KUNNR' ]-COLOR
TO FIELD-SYMBOL(<col>).
<col>-col = COL_POSITIVE.
<col>-int = 1.
<col>-inv = 1.
CATCH cx_sy_itab_line_not_found.

ENDTRY.

In Debug, before the Direct modify (1st update in this example)


After the update

In Debug, ASSIGN statement creates the FS and assigns the values of the specific structure COLOR
from T_COLOR from ALV table direclty

Das könnte Ihnen auch gefallen