2

Is it possible to create dynamic internal table with keys ? I am working with

    call method cl_alv_table_create=>create_dynamic_table
                     exporting 
                         it_fieldcatalog = lt_fldcat[]
                     importing 
                         ep_table        = lr_new_table

this gives the result without keys so I am not able to perform

    read table <ft_itab> from <fs_itab> ....

where "fs_itab" should be line of "ft_itab" with keys (specified in lt_fieldcat[]). Using method above is TABLE_LINE also a table key.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
bagere
  • 221
  • 5
  • 17
  • Hi, dusan. How are you populating field catalog table LT_FLDCAT? Also, what's the result list of fields of the table created? – Gone from this toxic community Jan 11 '18 at 11:56
  • Hi Ivan. I am trying to read a master data to my gt_outtab. So object ref lr_new_table will be combination of many master data tables. I will create a fieldcatalog that contain few keys and after read master data from lr_new_table into gt_outtab. It is possible by LOOP but if lr_new_table will contains also a keys the code could be "cleaner" or more readable – bagere Jan 11 '18 at 12:32
  • 5
    Are you talking about key fields of an internal table or field names? Because, you don't need key fields for read from an internal table. READ TABLE INTO @DATA(LS_ITAB) WITH KEY "A_FIELD_NAME" = "SOMETHING_YOU_WANT". Am I getting your question wrong? – Oguz Jan 11 '18 at 17:41
  • No. I need to read table without statement "with key" because this table will be different during report's run so I do not know number of keys. But check answer bellow from Sandra Rossi - that`s exactly what I need. – bagere Jan 15 '18 at 14:18

1 Answers1

2

To create a variable of any type dynamically at runtime, you may use the RTTC classes, followed by the statement CREATE DATA data_reference TYPE HANDLE rtti_instance.

For an internal table with its line being a structure (made of one or more fields), define first the structure with RTTC, then the internal table.

@Allen has shown a code sample in this other question: Dynamically defined variable in ABAP

To create a table type with a given primary key, use the parameters of the method CREATE of CL_ABAP_TABLEDESCR ; below is another writing of Allen's CREATE, but this one has a non-unique sorted primary key with components SIGN and LOW :

lo_table_descr = cl_abap_tabledescr=>create(
      p_line_type  = lo_struc_descr
      p_table_kind = cl_abap_tabledescr=>tablekind_sorted
      p_unique     = abap_false
      p_key        = VALUE #( ( 'SIGN' ) ( 'LOW' ) )
      p_key_kind   = cl_abap_tabledescr=>keydefkind_user
      ).

You may also create the type with secondary keys, but I guess you don't need it.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48