Dynamic internal tables

Here is how to create a dynamic internal table in a program.

Here is how to create a dynamic internal table in a program. A dynamic internal table is the internal table which is not declared in the program as static.

Usage:
* Used when the number of columns / fields are not known at the design time / compile time.
* Performance will not as good as in the Static internal table creation.
The code was written in R/3 Release 4.6C.
TYPE-POOLS: slis.

DATA: it_fcat TYPE slis_t_fieldcat_alv,
      is_fcat LIKE LINE OF it_fcat.
DATA: it_fieldcat TYPE lvc_t_fcat,
      is_fieldcat LIKE LINE OF it_fieldcat.
DATA: new_table TYPE REF TO data.
DATA: new_line  TYPE REF TO data.
FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,
               <l_line>  TYPE ANY,
               <l_field> TYPE ANY.

is_fieldcat-fieldname = 'FIELD1'.
is_fieldcat-ref_field = 'MATNR'.
is_fieldcat-ref_table = 'MARA'.
APPEND is_fieldcat TO it_fieldcat.

is_fieldcat-fieldname = 'FIELD2'.
is_fieldcat-ref_field = 'SPRPS'.
is_fieldcat-ref_table = 'PA0001'.
APPEND is_fieldcat TO it_fieldcat.

is_fieldcat-fieldname = 'FIELD3'.
is_fieldcat-ref_field = 'BEGDA'.
is_fieldcat-ref_table = 'PA0002'.
APPEND is_fieldcat TO it_fieldcat.

* Create a new Table
CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
    it_fieldcatalog = it_fieldcat
  IMPORTING
    ep_table        = new_table.

* Create a new Line with the same structure of the table.
ASSIGN new_table->* TO <l_table>.
CREATE DATA new_line LIKE LINE OF <l_table>.
ASSIGN new_line->* TO <l_line>.

* Test it...
DO 30 TIMES.
  ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <l_line> TO <l_field>.
  <l_field> = '12345'.

  ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <l_line> TO <l_field>.
  <l_field> = 'X'.

  ASSIGN COMPONENT 'FIELD3' OF STRUCTURE <l_line> TO <l_field>.
  <l_field> = '20030101'.

  INSERT <l_line> INTO TABLE <l_table>.
ENDDO.

LOOP AT <l_table> ASSIGNING <l_line>.
*  ASSIGN COMPONENT 'field1' OF STRUCTURE <l_line> TO <l_field>.
  WRITE  / <l_line>.
ENDLOOP.
This was first published in June 2003

Dig deeper on SAP ABAP

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

1 comment

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close