Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

creating an internal table at runtime..

Former Member
0 Likes
1,842

hi,

i have a situation where in, during runtime only, i come to know what the internal table name is. so i need to create an internal table at runtime based on the name i have in a string. then i need to append the work area to that internal table.

i need to define a table like

data: xx type table of s_kunnr.

so i am making use to field symbols..

assume, the varible l_str contains the field name 's_kunnr'. then if i say,

FIELD-SYMBOLS <fstab> type table of l_str,

will this work, i suspect !!..please provide some insight..

thks

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
947

Hi,

Check this Sample code,

DATA:
  o_ref TYPE REF TO data.
FIELD-SYMBOLS:
  <lt_table> TYPE STANDARD TABLE,
  <fs>       TYPE ANY,
  <field>    TYPE ANY,
  <field1>   TYPE ANY.
PARAMETERS:
  p_tab       TYPE tabname,           " Table name (eg: MARA)
  p_field(20) TYPE c.                 " Field name (eg: MATNR)

START-OF-SELECTION.
  CREATE DATA o_ref TYPE TABLE OF (p_tab).

  ASSIGN p_field TO <field1>.
  ASSIGN o_ref->* TO <lt_table>.

  SELECT *
    INTO TABLE <lt_table>
    FROM (p_tab).

  LOOP AT <lt_table> ASSIGNING <fs>.
    ASSIGN COMPONENT <field1> OF STRUCTURE <fs>
                  TO <field>.
    IF sy-subrc = 0.
      WRITE:/ <field>.
    ENDIF.
  ENDLOOP.

*-----------------------------------------------*
*  give input as :
*  p_tab   : mara
*  p_field : matnr.
*-----------------------------------------------*

You can also use

cl_alv_table_create=>create_dynamic_table

to create table dynamically.

for more info check this link:

Regards

Adil

3 REPLIES 3
Read only

Former Member
0 Likes
947

One example is , This will create internal table with two components with KUNNR, and NAME

REPORT  zdynamic.
 
FIELD-SYMBOLS: <l_table> TYPE table,
<l_line> TYPE ANY,
<l_field> TYPE ANY.
 
DATA: is_lvc_cat TYPE lvc_s_fcat,
      it_lvc_cat TYPE lvc_t_fcat.
 
DATA: new_table TYPE REF TO data,
      new_line  TYPE REF TO data.
 
 
START-OF-SELECTION.
 
  is_lvc_cat-fieldname = 'KUNNR'.
  APPEND is_lvc_cat TO it_lvc_cat.
 
  is_lvc_cat-fieldname = 'NAME1'.
  APPEND is_lvc_cat TO it_lvc_cat.
 
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = it_lvc_cat
    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>.
 
 
  DO 2 TIMES.
    ASSIGN COMPONENT 'KUNNR' OF STRUCTURE <l_line> TO <l_field>.
    <l_field> = sy-index.
 
    ASSIGN COMPONENT 'NAME1' OF STRUCTURE <l_line> TO <l_field>.
    <l_field> = 'A'.
    INSERT <l_line> INTO TABLE <l_table>.
  ENDDO.
 
  LOOP AT <l_table> INTO <l_line>.
    WRITE:/ <l_line>.
  ENDLOOP.
 
  READ TABLE <l_table> INTO <l_line> INDEX 2.
  <l_line>+10(2) = 'B'.
  MODIFY <l_table> FROM <l_line> INDEX 2.
 
 
  LOOP AT <l_table> INTO <l_line>.
    WRITE:/ <l_line>.
  ENDLOOP.

Read only

Former Member
0 Likes
948

Hi,

Check this Sample code,

DATA:
  o_ref TYPE REF TO data.
FIELD-SYMBOLS:
  <lt_table> TYPE STANDARD TABLE,
  <fs>       TYPE ANY,
  <field>    TYPE ANY,
  <field1>   TYPE ANY.
PARAMETERS:
  p_tab       TYPE tabname,           " Table name (eg: MARA)
  p_field(20) TYPE c.                 " Field name (eg: MATNR)

START-OF-SELECTION.
  CREATE DATA o_ref TYPE TABLE OF (p_tab).

  ASSIGN p_field TO <field1>.
  ASSIGN o_ref->* TO <lt_table>.

  SELECT *
    INTO TABLE <lt_table>
    FROM (p_tab).

  LOOP AT <lt_table> ASSIGNING <fs>.
    ASSIGN COMPONENT <field1> OF STRUCTURE <fs>
                  TO <field>.
    IF sy-subrc = 0.
      WRITE:/ <field>.
    ENDIF.
  ENDLOOP.

*-----------------------------------------------*
*  give input as :
*  p_tab   : mara
*  p_field : matnr.
*-----------------------------------------------*

You can also use

cl_alv_table_create=>create_dynamic_table

to create table dynamically.

for more info check this link:

Regards

Adil

Read only

Former Member
0 Likes
947

It takes the table name and Dynamically Builds the Internal table.

First read the components and then Fill the fieldcata log, using the method create_dynamic_table of cl_alv_table_create we will build the dynamic data object , from there we will assign it to fieldsymbol. ans use it for report purpose./

REPORT  ytest_dynamic.
TYPE-POOLS : abap.
DATA : table_des TYPE REF TO cl_abap_structdescr.
DATA : ifields TYPE abap_compdescr_tab,
       wa_field LIKE LINE OF ifields.
DATA: it_fieldcat TYPE lvc_t_fcat,
      wa_fieldcat TYPE lvc_s_fcat.
data: i_tab type ref to data.
 
      field-symbols: <fs> type standard table.
 
PARAMETERS: p_table(30) type c default 'SFLIGHT'.
 
 
"Table definiton using the table name
table_des ?= cl_abap_typedescr=>describe_by_name( p_table ).
"Now Read all the fields to a table.
ifields = table_des->components.
 
 
LOOP AT ifields INTO wa_field.
clear wa_fieldcat.
  wa_fieldcat-fieldname = wa_field-name .
    wa_fieldcat-datatype = wa_field-type_kind.
    wa_fieldcat-inttype = wa_field-type_kind.
    wa_fieldcat-intlen = wa_field-length.
    wa_fieldcat-decimals = wa_field-decimals.
    APPEND  wa_fieldcat TO it_fieldcat.
ENDLOOP.
 
CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
    it_fieldcatalog           = it_fieldcat
*    i_length_in_byte          =
  IMPORTING
    ep_table                  = i_tab
*    e_style_fname             =
  EXCEPTIONS
    generate_subpool_dir_full = 1
    others                    = 2
        .
IF sy-subrc  0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
 
assign i_tab->* to <fs>.
 
*-fill the data
 
select  *
into table <fs>
from (p_table)
up to 20 rows.