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 last published in June 2003

Dig Deeper on SAP ABAP

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

OMG! this is exactly what I need... This is a fabulous website.. Thank you boys
Cancel

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close