Tip

Dynamic internal tables

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

    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
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
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.