In some scenarios, backuping the internal table data is necessary to maintain data integrity. To backup the table data, create new table with the same structure of existing source table.

The contents of internal table copied to another by using the APPEND or INSERT statement. The effortless way of copying the data among the above two methods is using MOVE statement.

Field to field copy -

Move statement used to copy the data from one table to another table while using the INSERT statement. The below syntax shows copying of one table data to another table at the field level is –

MOVE <table1_field> TO <table2_field>.

Using the above syntax, table1_field data copies to another table2_field.


Example -

Simple example for copying field to field from one internal table(it) to another internal table(it1).


Code -

*&---------------------------------------------------------------------*
*& Report  Z_INTERNAL_TABLE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_INTERNAL_TABLE.

* Specifying table name
TABLES ZTC_TPRODUCT.

* Declaring work area, table it of type ZTC_TPRODUCT
DATA: WA LIKE ZTC_TPRODUCT,
      it TYPE TABLE OF ZTC_TPRODUCT.

* Retrieving data from the table
SELECT *
  FROM ZTC_TPRODUCT ORDER BY PRODUCTID.
  
* Moving table field data to work area field
  MOVE ZTC_TPRODUCT-PRODUCTID TO WA-PRODUCTID.
  MOVE ZTC_TPRODUCT-PRODUCT   TO WA-PRODUCT.
  MOVE ZTC_TPRODUCT-PRODUCT_PRICE   TO WA-PRODUCT_PRICE.
  
* Inserting work area to table it.  
  INSERT WA INTO TABLE it.
ENDSELECT.

* Displaying header line for output.
WRITE: /'PRODUCTID  | PRODUCT                                  '
'|    PRODUCT PRICE         |'.
ULINE.

* Loop for displaying table it data.
LOOP AT it INTO wa.

* Displaying table data line output.
  IF sy-subrc = 0.
    WRITE: / WA-PRODUCTID, '|', WA-PRODUCT,  '|',WA-PRODUCT_PRICE,  '|'.
  ELSE.
    WRITE 'No Record Found'.
  ENDIF.
ENDLOOP.
ULINE.

Output -

Field to Field Copy Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

Work area(WA) defined like ZTC_TPRODUCT table structure. Internal table(it) defined as type of ZTC_TPRODUCT table. SELECT statement retrieves the table data row by row until end of the data in the table ZTC_TPRODUCT. After execution of SELECT statement, the data automatically placed in table fields which are referenced with table name with hyphen preceding with. In this case the table field for PRODUCTID is ZTC_TPRODUCT-PRODUCTID. Similarly, for the other fields.

MOVE statement used to move the data from table fields to work area. INSERT statements inserts the data from work area to internal table.


MOVE CORRESPONDING -

If the table has more than one field and both tables has the same set of the fields but the order of the fields is different, we may required to use MOVE statements as many times as the number of fields. The same task can be achieved by using a single MOVE statement with CORRESPONDING clause.

Syntax -

MOVE-CORRESPONDING <itable1> TO <itable2>. 

The above syntax describes that the table itable1 fields data moves to its corresponding fields in the table itable2.


Example -

Simple example for field to field corresponding copy from one internal table(it) to another internal table(it1).


Code -

*&---------------------------------------------------------------------*
*& Report  Z_INTERNAL_TABLE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_INTERNAL_TABLE.

* Specifying table name
TABLES ZTC_TPRODUCT.

* Declaring work area, table it, it1 of type ZTC_TPRODUCT
DATA: WA  LIKE ZTC_TPRODUCT,
      it  TYPE TABLE OF ZTC_TPRODUCT,
      it1 TYPE TABLE OF ZTC_TPRODUCT.

* Retrieving data from the table
SELECT *
  FROM ZTC_TPRODUCT ORDER BY PRODUCTID.
  
* Moving corresponding table fields data to work area fields
  MOVE-CORRESPONDING ZTC_TPRODUCT TO WA.
  
* Inserting work area to table it.  
  INSERT WA INTO TABLE it.
ENDSELECT.

* Displaying header line for output.
WRITE: /'PRODUCTID  | PRODUCT                                  '
'|    PRODUCT PRICE         |'.
ULINE.

* Loop for displaying table it data.
LOOP AT it INTO wa.

* Displaying table data line output.
  IF sy-subrc = 0.
    WRITE: / WA-PRODUCTID, '|', WA-PRODUCT,  '|',WA-PRODUCT_PRICE,  '|'.
  ELSE.
    WRITE 'No Record Found'.
  ENDIF.
ENDLOOP.
ULINE.

Output -

Move corresponding example output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

Work area(WA) defined like ZTC_TPRODUCT table structure. Internal tables(it, it1) defined as type of ZTC_TPRODUCT table.

MOVE_CORRESPONDING statement used to move the data from corresponding table fields to work area fields. INSERT statements inserts the data from work area to internal table.


COPY Internal table to internal table without header line -

Till now, we have seen copying line by line from one table to another table. Now, we will see how the entire table data copied to another table in a single step. This can be acheived by using the below syntax.

Syntax -

MOVE <itable1> To <itable2>.

OR

<itable1> = <itable2>.

The above syntax used to copy the data from itable1 to itable2. This method works only when the tables has no header line and should have same structure.


Example -

Simple example for copying record by record from one internal table(it) to another internal table(it1).

ZTC_TPRODUCT table data -
Copy Internal Table Example

Code -

*&---------------------------------------------------------------------*
*& Report  Z_INTERNAL_TABLE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_INTERNAL_TABLE.

* Specifying table name
TABLES ZTC_TPRODUCT.

* Declaring work area, table it, it1 of type ZTC_TPRODUCT 
DATA: WA  LIKE ZTC_TPRODUCT,
      it  TYPE TABLE OF ZTC_TPRODUCT,
      it1 TYPE TABLE OF ZTC_TPRODUCT.

* Retrieving data from the table
SELECT *
  INTO WA
  FROM ZTC_TPRODUCT ORDER BY PRODUCTID.
  
* Inserting work area to table it.
  INSERT WA INTO TABLE it.

* inserting table it data to table it1.
  it1  = it.
ENDSELECT.

* Displaying header line for output.
WRITE: /'PRODUCTID  | PRODUCT                                  '
'|    PRODUCT PRICE         |'.
ULINE.

* Loop for displaying table it1 data.
LOOP AT it1 INTO wa.

* Displaying table data line output.
  IF sy-subrc = 0.
    WRITE: / WA-PRODUCTID, '|', WA-PRODUCT,  '|',WA-PRODUCT_PRICE,  '|'.
  ELSE.
    WRITE 'No Record Found'.
  ENDIF.
ENDLOOP.
ULINE.

Output -

Copy Internal Table Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

Work area(WA) defined like ZTC_TPRODUCT table structure. Internal table(it) defined as type of ZTC_TPRODUCT table.

it1 = it copies the entire record from internal table it to internal table it1. INSERT statements inserts the data from work area to internal table.


COPY Internal table to internal table with header line -

To copy internal table data with header line to another internal table, the MOVE statement required [] along with the table name. The syntax to copy table data along with header is –

itable1[] = itable2[].

The above syntax copies the itable1 data to itable2 along with the header information.


Example -

Simple example for copying entire internal table data in single shot from one internal table(it) to another internal table(it1).


Code -

*&---------------------------------------------------------------------*
*& Report  Z_INTERNAL_TABLE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_INTERNAL_TABLE.

* Specifying table name
TABLES ZTC_TPRODUCT.

* Declaring work area, table it, it1 of type ZTC_TPRODUCT
DATA: WA  LIKE ZTC_TPRODUCT,
      it  TYPE TABLE OF ZTC_TPRODUCT,
      it1 TYPE TABLE OF ZTC_TPRODUCT.

* Retrieving data from the table
SELECT *
  FROM ZTC_TPRODUCT ORDER BY PRODUCTID.
  
* Assigning table field to work area field.
  MOVE ZTC_TPRODUCT-PRODUCTID TO WA-PRODUCTID.
  MOVE ZTC_TPRODUCT-PRODUCT   TO WA-PRODUCT.
  MOVE ZTC_TPRODUCT-PRODUCT_PRICE   TO WA-PRODUCT_PRICE.

* Inserting work area data to table it.
  INSERT WA INTO TABLE it.
ENDSELECT.

* Copying internal table(it) data to another internal table (it1)
it1[] = it[].

* Displaying header line for output.
WRITE: /'PRODUCTID  | PRODUCT                                  '
'|    PRODUCT PRICE         |'.
ULINE.

* Loop for displaying table it1 data.
LOOP AT it1 INTO wa.

* Displaying table data line output.
  IF sy-subrc = 0.
    WRITE: / WA-PRODUCTID, '|', WA-PRODUCT,  '|',WA-PRODUCT_PRICE,  '|'.
  ELSE.
    WRITE 'No Record Found'.
  ENDIF.
ENDLOOP.
ULINE.

Output -

Table to table copy example output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

Work area(WA) defined like ZTC_TPRODUCT table structure. Internal table(it) defined as type of ZTC_TPRODUCT table.

it1[] = it[] copies entire it internal table to it1 internal table with header line. INSERT statements inserts the data from work area to internal table.