Application Development 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: 

dynamic table manipulation

Former Member
0 Kudos

hi experts...

suppose i have i_tab with component werks, matnr, mtart.

in i_tab, i have retrieved 2 entries of matnr from one werks.

i have to transfer the entries from i_tab into dynamic table <dyn_tab>.

how will i transfer each entry?

i have created and assigned work area for the dynamic table output:

create data new_line like line of <dyn_table>.

assign new_line->* to <dyn_wa>.

please help me with this. thanks!

march

1 ACCEPTED SOLUTION

Sougata
Active Contributor
0 Kudos

Do it like this:

  METHOD fill_dynamic_tab.
    DATA: l_descr_ref TYPE REF TO cl_abap_structdescr,
          l_str       TYPE string,
          l_tabix     TYPE sytabix.

    FIELD-SYMBOLS: <li_table> TYPE ANY,
                   <comp_wa>  TYPE abap_compdescr,
                   <fzmach>   TYPE zscmachine.           "This is your i_tab work area

* Create dynamic work area and assign to FS
    CREATE DATA dy_line LIKE LINE OF <dyn_table>.
    ASSIGN dy_line->* TO: <dyn_wa>, <li_table>.

      l_descr_ref ?= cl_abap_typedescr=>describe_by_data( <dyn_wa> ).
      check ( l_descr_ref IS BOUND ).

* Main logic
    LOOP AT lt_zmach ASSIGNING <fzmach>.                 "This is your i_tab
      MOVE-CORRESPONDING <fzmach> TO: <li_table>, <dyn_wa>.
      READ TABLE l_descr_ref->components ASSIGNING <comp_wa>
                   WITH KEY name = 'WERKS'.
        l_tabix = sy-tabix.
        IF sy-subrc = 0.
          ASSIGN COMPONENT l_tabix OF STRUCTURE <li_table> TO <dyn_field>.
          <dyn_field> = <fzmach>-werks.
        ENDIF.
        READ TABLE l_descr_ref->components ASSIGNING <comp_wa>
                   WITH KEY name = 'MATNR'.
        l_tabix = sy-tabix.
        IF sy-subrc = 0.
          ASSIGN COMPONENT l_tabix OF STRUCTURE <li_table> TO <dyn_field>.
          <dyn_field> = <fzmach>-matnr.

* keep doing as above for all other fields you require 
        APPEND <li_table> INTO <dyn_table>.
      CLEAR: <li_table>, <dyn_wa>.
    ENDLOOP.

    SORT <dyn_table>.

  ENDMETHOD.                    "fill_dynamic_tab

I'm not in front of a system, so you might get syntax errors, correct them as you go.

<b>Don't forget to reward points if this has helped you!</b>

Cheers,

Sougata.

4 REPLIES 4

Sougata
Active Contributor
0 Kudos

Do it like this:

  METHOD fill_dynamic_tab.
    DATA: l_descr_ref TYPE REF TO cl_abap_structdescr,
          l_str       TYPE string,
          l_tabix     TYPE sytabix.

    FIELD-SYMBOLS: <li_table> TYPE ANY,
                   <comp_wa>  TYPE abap_compdescr,
                   <fzmach>   TYPE zscmachine.           "This is your i_tab work area

* Create dynamic work area and assign to FS
    CREATE DATA dy_line LIKE LINE OF <dyn_table>.
    ASSIGN dy_line->* TO: <dyn_wa>, <li_table>.

      l_descr_ref ?= cl_abap_typedescr=>describe_by_data( <dyn_wa> ).
      check ( l_descr_ref IS BOUND ).

* Main logic
    LOOP AT lt_zmach ASSIGNING <fzmach>.                 "This is your i_tab
      MOVE-CORRESPONDING <fzmach> TO: <li_table>, <dyn_wa>.
      READ TABLE l_descr_ref->components ASSIGNING <comp_wa>
                   WITH KEY name = 'WERKS'.
        l_tabix = sy-tabix.
        IF sy-subrc = 0.
          ASSIGN COMPONENT l_tabix OF STRUCTURE <li_table> TO <dyn_field>.
          <dyn_field> = <fzmach>-werks.
        ENDIF.
        READ TABLE l_descr_ref->components ASSIGNING <comp_wa>
                   WITH KEY name = 'MATNR'.
        l_tabix = sy-tabix.
        IF sy-subrc = 0.
          ASSIGN COMPONENT l_tabix OF STRUCTURE <li_table> TO <dyn_field>.
          <dyn_field> = <fzmach>-matnr.

* keep doing as above for all other fields you require 
        APPEND <li_table> INTO <dyn_table>.
      CLEAR: <li_table>, <dyn_wa>.
    ENDLOOP.

    SORT <dyn_table>.

  ENDMETHOD.                    "fill_dynamic_tab

I'm not in front of a system, so you might get syntax errors, correct them as you go.

<b>Don't forget to reward points if this has helped you!</b>

Cheers,

Sougata.

Former Member
0 Kudos

Hi Sougata,

Thank you for the code.

I have a question. The program does not recognize TYPE zscmachine.

What do you mean by this?

Thanks!

march

Sougata
Active Contributor
0 Kudos

No!! The ZSCMACHINE is the type I used in my program because my internal table is TYPE that table I created in the dictionary.

For you, it depends on the definition of your internal table. It should be the same TYPE as your internal table which you are looping in to fill your dynamic table.

Sougata.

<b>Don't forget to reward points on the left hand side of the page.</b>

Message was edited by:

Sougata Chatterjee

Former Member
0 Kudos

okay.. i'll try this on my code.

thank you so much!