terça-feira, 26 de maio de 2015

Copy Internal Table to another Internal Table

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.

Nenhum comentário:

Postar um comentário