Copy Internal Table to another Internal Table

By | Last Updated on November 14, 2013 | 102,459

Code Snippet of Various different options to copy entries from Internal table to another internal table

All of these approaches would copy the entries from ITAB to another ITAB when work as expected when:

  • Both Source and Target has Same structures
  • Target has fewer fields with same types. Rest of the fields would be discarded.

For this code snippet, we would be copying entries to reduced target internal table.

Using LOOP to copy itab to another itab

 
TYPES:
  BEGIN OF ty_vbak,
    vbeln TYPE vbak-vbeln,
    auart TYPE vbak-auart,
    vkorg TYPE vbak-vkorg,
  END   OF ty_vbak.
DATA: t_vbak  TYPE STANDARD TABLE OF ty_vbak.
DATA: ls_vbak LIKE LINE OF t_vbak.
DATA: t_vbeln TYPE STANDARD TABLE OF vbak-vbeln.
DATA: lv_lines TYPE i.
 
* Some test data
SELECT vbeln auart vkorg
  FROM vbak
  INTO TABLE t_vbak
  UP TO 500 ROWS.
 
CLEAR t_vbeln.
LOOP AT t_vbak INTO ls_vbak.
  APPEND ls_vbak-vbeln TO t_vbeln.
ENDLOOP.
 
* Display number of entries in target table
lv_lines = lines( t_vbeln ).
WRITE lv_lines.
 

Using MOVE to copy

 
CLEAR t_vbeln.
MOVE t_vbak TO t_vbeln.
 
* Display number of entries in target table
lv_lines = lines( t_vbeln ).
WRITE lv_lines.
 

Using APPEND LINES OF to copy

 
CLEAR t_vbeln.
APPEND LINES OF t_vbak TO t_vbeln.
 
* Display number of entries in target table
lv_lines = lines( t_vbeln ).
WRITE lv_lines.
 

Using direct copy

 
CLEAR t_vbeln.
t_vbeln = t_vbak.
 
* Display number of entries in target table
lv_lines = lines( t_vbeln ).
WRITE lv_lines.
 

Any other option, you might be using for copying?

More on Copying Internal tables on SAP Help

Do you have a Code Snippet which you want to share, Submit Code Snippet here

Share It!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

15 Comments

  • tempx

    Time ago I created an FM Z_MOVE_CORRESPONDING_TABLE. I use it all the time to move e.g. DB table content to display structure based internal tables or vica versa. Simple fast and very useful IMHO.

     
    FUNCTION z_move_corresponding_table.
    *"----------------------------------------------------------------------
    *"*"Local Interface:
    *"  IMPORTING
    *"     REFERENCE(IT_TABLE) TYPE  TABLE
    *"  EXPORTING
    *"     REFERENCE(ET_TABLE) TYPE  TABLE
    *"----------------------------------------------------------------------
     
      DATA: er_line TYPE REF TO data.
     
      FIELD-SYMBOLS:  TYPE ANY,
                      TYPE ANY.
     
      CLEAR et_table.
     
      CREATE DATA er_line LIKE LINE OF et_table.
      ASSIGN er_line->* TO .
     
      LOOP AT it_table ASSIGNING .
        MOVE-CORRESPONDING  TO .
        APPEND  TO et_table.
      ENDLOOP.
     
    ENDFUNCTION.
     
  • tempx

    SOmehow the blog engine is eating the field symbols:

    Replace ^ with corresponding field symbol brackets

     
    FUNCTION z_move_corresponding_table.
    *"----------------------------------------------------------------------
    *"*"Local Interface:
    *"  IMPORTING
    *"     REFERENCE(IT_TABLE) TYPE  TABLE
    *"  EXPORTING
    *"     REFERENCE(ET_TABLE) TYPE  TABLE
    *"----------------------------------------------------------------------
     
      DATA: er_line TYPE REF TO data.
     
      FIELD-SYMBOLS: ^is_table^ TYPE ANY,
                     ^es_table^ TYPE ANY.
     
      CLEAR et_table.
     
      CREATE DATA er_line LIKE LINE OF et_table.
      ASSIGN er_line-^* TO ^es_table^.
     
      LOOP AT it_table ASSIGNING ^is_table^.
        MOVE-CORRESPONDING ^is_table^ TO ^es_table^.
        APPEND ^es_table^ TO et_table.
      ENDLOOP.
     
    ENDFUNCTION.
     
  • Pankaj Kumar

    It’s very useful for beginners…

  • stevn oldner

    Those 3 are all I used depending on the circumstance. I’ve seen some of SAP’s dynamic copy for tables, but never had the need. Seems like a bit of extra overhead to make it dynamic.

  • Fawcs

    Isn’t = just the short form of MOVE?

  • Hello tempx,

    Apologies that the FS doesn’t work in the comment. I guess I have to work hard to fix that.

    I too believe – like Steve – that it would be unnecessary to create the dynamic type and use move-corresponding to copy them. As long as the target structure contains only required fields in the same order, above listed approaches would work.

    Thanks for sharing πŸ™‚

  • Hello Fawcs,

    Equal to ( = ) is not same as MOVE but MOVE is same as Equal to. Equal to is more powerful. Using Equal to, you can create a chain of assignments.

    Like:

     
    itab4 = itab3 = itab2 = itab1.
     

    This chain would start assigning the values from right to left. To achieve the same using MOVE, you need to have individual MOVE statements.

    Thanks.

  • tempx

    Hi,

    As I can see you did not really get the point of my FM. It is not for copying tables but doing the same what move correponding does, just for tables. Imagine the following situtation: you have too internal tables:

    1. VBELN – POSNR – MATNR – WERKS
    2. VBELN – POSNR – MATNR – MATNR_DESCR – WERKS – WERKS_DESCR

    Itab 1 is used for DB update Itab 2 is displayed for the user in an ALV.

    I use my FM first after I selected the data from VBAP (imagine a more complex case without joins I just try to be simple) -> move the table content to the display table, loop over it and fill the description field.

    I use the FM second time after the user changed something in the UI, then I move back the table content with the FM to the DB like itab and perform the update based on that.

    Instead of:

    LOOP AT itab1 into ls_itab1.
    MOVE CORRESPONDING ls_itab1 TO ls_itab2.
    APPEND ls_itab2 to lt_itab2.
    ENDLOOP.

    I just call the FM.

    I am curious how can you do this without dynamic programming?

    I found myself use it all the time mainly in WD ABAP, but also using for editable ALVs.

    Tempx

  • Hello Tempx,

    I recognized the purpose your FM the first time itself – it doesn’t simply copy the table but does better job of moving data at row level when fields of both of the structures don’t have same field sequence.

    If I have similar situation, I would define my table 2 differently. My types would be like this:
    Table 1 – VBELN, POSNR, MATNR, WERKS
    Table 2 – VBELN, POSNR, MATNR, WERKS, MATNR_DESC, WERKS_DESC

    I wold be able to than use any of the copy options listed here to move data from Table 2 to Table 1.

    Thanks Much.

  • Ram

    DIRECT COPY,

    I think , it would be better if we use body of internal table.

    ITAB_2[ ] = ITAB_1[ ].

    Any comments?

  • tempx

    ITAB_2[ ] = ITAB_1[ ]. it is just necessary for header tables, what is obsolete and should not be used anymore if not inevitable (some RFC cases with TABLES params)

  • ozon

    Great Website, i gain much from it for my work!

    I recently wanted to encapsulate a selection Screen within a function module.

    Tables: mara.
    SELECT-OPTIONS: so_matnr FOR mara-matnr.

    So in order to return a Select-Options to the caller-programm you need a ranges-table-type
    for any specific Select-Options (e.g. one for matnr, one for vbeln etc.)
    So you need to create quite a lot of ney dictionary table types πŸ™

    There is indeed a universal ranges table type in the dictionay:
    DATA: t_range_matnr TYPE efg_tab_ranges.

    But if you try to just copy the itab, it won’t work:
    t_range_matnr = so_matnr[].

    The reason is, the copy-process just takes the charsequence from
    so_matnr and “oberlays” the t_range_matnr with it (char by char).
    The so_matnr-low gets ist length from the table statement implicitly (length 18 like matnr).
    The t_range_matnr has a universal length of 45.

    So in order to solve the Problem i used some dynamical programming:

    CALL METHOD me->create_range(
    EXPORTING
    it_so = so_matnr[]
    CHANGING
    ct_range = t_range_matnr).

    METHOD create_range.
    *importing it_so type STANDARD TABLE
    *CHANGING ct_range type STANDARD TABLE.

    FIELD-SYMBOLS: TYPE data,
    TYPE any.

    DATA: ls_range TYPE efg_ranges.

    LOOP AT it_so ASSIGNING .
    ASSIGN COMPONENT ‘SIGN’ OF STRUCTURE TO .
    ls_range-sign = .
    ASSIGN COMPONENT ‘OPTION’ OF STRUCTURE TO .
    ls_range-option = .
    ASSIGN COMPONENT ‘LOW’ OF STRUCTURE TO .
    ls_range-low = .
    ASSIGN COMPONENT ‘HIGH’ OF STRUCTURE TO .
    ls_range-high = .
    APPEND ls_range TO ct_range.
    ENDLOOP.

    ENDMETHOD. “create_range

    With this coding in a class or Function-Module you can use the efg_tab_ranges table-type
    for all Select-Options.

  • ozon

    There was some Problem with html and and the field-symbol-tags
    METHOD create_range.
    *importing it_so type STANDARD TABLE
    *CHANGING ct_range type STANDARD TABLE.

    FIELD-SYMBOLS: struc TYPE data,
    fs TYPE any.

    DATA: ls_range TYPE efg_ranges.

    LOOP AT it_so ASSIGNING struc.
    ASSIGN COMPONENT ‘SIGN’ OF STRUCTURE struc TO fs.
    ls_range-sign = fs.
    ASSIGN COMPONENT ‘OPTION’ OF STRUCTURE struc TO fs.
    ls_range-option = fs.
    ASSIGN COMPONENT ‘LOW’ OF STRUCTURE struc TO fs.
    ls_range-low = fs.
    ASSIGN COMPONENT ‘HIGH’ OF STRUCTURE struc TO fs.
    ls_range-high = fs.
    APPEND ls_range TO ct_range.
    ENDLOOP.

    ENDMETHOD. “create_range

  • Kris

    Ozon, is there any specific reason for not using ‘type range of mara-matnr’ for declaring t_range_matnr?

  • ozon

    You can’t pass a (local defined) Range of mara-matnr to an external FM or Method, because the ‘LOW’
    field of the Range has always the length of the actual technical field (e.G. matnr=18).

    The efg_tab_ranges table-type in the dictionary has a “universal” length of 45.
    So you can use some dymalical Programming and use always this table-type
    to pass select options around to an external FM or Method.

    You can of course create a range-table-type for any field in in the dictionary but this
    means to create a lot of range table-types.

Comments on this Post are now closed. If you have something important to share, you can always contact me.