Search code examples
structureabapinternal-tables

Get value field dynamically from another table ABAP


help me for report Abap. I have 2 table.

  1. Itab head
Z1 Z2 Z3
A B C
  1. Itab obbligatory field
Fieldname
Z2

i should create a file with only fields of itab2(itab obbligatory field) but the value of itab head.

The file will be:

tab file

Z2
B

Itab2 say me which field of itab1 are obbligatory for create itab file.


Solution

  • This gives you an idea of what to do using ASSIGN for dynamic field selection.

      LOOP AT itab_a INTO DATA(wa_itab_a).     " your data tab
        LOOP AT itab_b INTO DATA(wa_itab_b).   " your obligatory field list
          TRANSLATE wa_itab_b-fieldname TO UPPER CASE. " important if they are not already in uppercase
          ASSIGN COMPONENT wa_itab_b-fieldname OF STRUCTURE wa_itab_a TO FIELD-SYMBOL(<fs_field>).
          " ... in <FS_FIELD> you will have the values of the obligatory fields
          " so you can concatenate, par example, to file line
          CONCATENATE wa_file-line <fs_field> INTO wa_file-line SEPARATED BY c_separator.
        ENDLOOP.
        " append file line here, something like this:
        APPEND wa_file TO itab_file.
        CLEAR wa_file.
      ENDLOOP.