Internal Table Expressions in ABAP 7.4 Release

ABAP for HANA

2
70670

Hello everyone, in this blog you are going to learn about Internal Table Expressions in ABAP 7.4 Release. This is the second part of a series of blogs on What’s New in ABAP 7.4 Release.

Please read our first blog on Inline Declarations for DATA and FIELD-SYMBOL. Let’s get started.

Internal Table Expressions

A table expression consists of the internal table name, directly followed by square brackets [ ]

READ TABLE

Before ABAP 7.4 we use READ TABLE syntax to read the internal table, but with new release of ABAP 7.4 SAP has introduced table expressions to read the internal table with brand new syntax.

We can read the internal table data with 3 possible ways.You will learn how to use table expressions to read the internal table which is different from old syntax READ TABLE … .

1. READ INDEX – Read the internal table data using the table index

Old Syntax
READ TABLE IT_MARA INTO DATA(WA_MARA) INDEX 1.
New Syntax
DATA(WA_MARA) = IT_MARA[ 1 ].

2. READ USING a FREE KEY – Read the internal table using the free key.

Old Syntax
READ TABLE IT_BOOKINGS INTO WA_BOOKINGS WITH KEY CARRID = 'AA'
                                                 CONNID = '17'
                                                 CUSTTYPE = 'P'.
New Syntax
DATA(WA_BOOKINGS) = IT_BOOKINGS[ CARRID   = 'AA'
                                 CONNID   = '17'
                                 CUSTTYPE = 'P' ].

3. READ USING a TABLE KEY – Read the internal table by specifying  table keys

Old Syntax
READ TABLE IT_BOOKINGS INTO WA_BOOKINGS WITH TABLE KEY CARRID = 'AA'
                                                       CONNID = '17'.
 New Syntax
DATA(WA_BOOKINGS) = IT_BOOKINGS[ KEY keyid COMPONENTS CARRID = 'AA'
                                                      CONNID = '17' ].

APPEND

Before Netweaver 7.4 we use APPEND syntax to append rows to the internal table, but with new release of Netweaver 7.4 SAP has introduced table expressions to initialize the internal table with brand new syntax.

Old Syntax
TYPES: BEGIN OF ty_old,
         f1 TYPE c,
       END OF ty_old.

DATA: wa_old  TYPE ty_old,
      it_old  TYPE STANDARD TABLE OF ty_old WITH EMPTY KEY.

wa_old-f1 = 'A'.
APPEND wa_old TO it_old.

wa_old-f1 = 'B'.
APPEND wa_old TO it_old.

wa_old-f1 = 'C'.
APPEND wa_old TO it_old.
 New Syntax
TYPES: BEGIN OF ty_new,
         f1 TYPE c,
       END OF ty_new,
       tty_new TYPE TABLE OF ty_new WITH EMPTY KEY.

DATA(it_new) = VALUE tty_new( ( f1 = 'A') 
                              ( f1 = 'B') ).

MODIFY

We use MODIFY statement to modify the contents in internal table. In ABAP Netweaver 7.4 release SAP has introduced brand new syntax using table expressions. We also use FIELD-SYMBOL to modify the contents in internal table. We will look at both versions and you will also learn the new ABAP 7.4 new syntax

Old Syntax
* Modify the contents based on INDEX
READ TABLE IT_BOOKINGS ASSIGNING <FS_BOOKINGS> INDEX 1.
IF sy-subrc = 0.
  <FS_BOOKINGS>-CARRID = 'NP'.  
ENDIF.

* Modify the contents based on FREE-KEY
READ TABLE IT_BOOKINGS ASSIGNING <FS_BOOKINGS> WITH KEY CARRID = 'AA'
                                                        CONNID = '17'.
IF sy-subrc = 0.
  <FS_BOOKINGS>-CARRID = 'NP'.  
ENDIF.

* Modify the contents based on TABLE-KEY
READ TABLE IT_BOOKINGS ASSIGNING <FS_BOOKINGS> WITH TABLE KEY CARRID = 'AA'
                                                              CONNID = '17'.
IF sy-subrc = 0.
  <FS_BOOKINGS>-CARRID = 'NP'.  
ENDIF.
 New Syntax
*** Using Table Expressions **
* Modify the contents based on INDEX
TRY.
    it_bookings[ 1 ]-carrid = 'NP'.
  CATCH cx_sy_itab_line_not_found.
ENDTRY.


* Modify the contents based on FREE-KEY
TRY.
    it_bookings[ carrid = 'AA' connid = '17' ]-carrid = 'NP'.
  CATCH cx_sy_itab_line_not_found.
ENDTRY.


* Modify the contents based on TABLE-KEY
TRY.
    it_bookings[ KEY keyid COMPONENTS carrid = 'AA'
                                    connid = '17' ]-carrid = 'NP'.
  CATCH cx_sy_itab_line_not_found.
ENDTRY.

Functions of Internal Tables

#1. LINE_EXISTS( )

In ABAP 7.4, we have new syntax to check if the record exists in the internal table based on some conditions. This syntax is short form to READ TABLE with TRANSPORTING NO FIELDS followed by sy-subrc check. LINE_EXISTS function will return “true” if the row exists and “false” if the row does not exists.

Old Syntax
READ TABLE it_bookings TRANSPORTING NO FIELDS WITH KEY carrid = 'AA'
                                                       connid = '17'.
IF sy-subrc = 0.

  .....
ENDIF.
New Syntax
IF line_exists( it_bookings[ carrid = 'AA'
                             connid = '17'] ).
  .....
ENDIF.

#2. LINE_INDEX( )

In ABAP 7.4 release, we have new syntax LINE_INDEX() to identify the index of a row when a condition is met while reading the internal table. The new syntax is similar to READ TABLE with TRANSPORTING NO FIELDS followed by sy-subrc check. if sy-subrc = 0, then sy-tabix will give the index of the row.

Old Syntax
READ TABLE it_bookings TRANSPORTING NO FIELDS WITH KEY carrid = 'AA'
                                                       connid = '17'.
IF sy-subrc = 0.
  WRITE: sy-tabix. "index
ENDIF.
 New Syntax
DATA(indx) = line_index( it_bookings[ carrid = 'AA'
                                      connid = '17'] ).
WRITE: indx.

Congrats! You have successfully learned new features in ABAP 7.4 release. Next time when you write code try using the new syntax and let us know your experience. Please stay tuned for ABAP for HANA/ABAP 7.4 tutorials. Leave a comment in the below comment section and let us know your feedback.

Comments are closed.