1.33 SAP ABAP - Copying Internal Tables

«« Previous
Next »»

When we read a record from an internal table with a header line, that record is moved from the table itself into the header line. It is then the header line that our program works with. The same applies while creating a new record. It is the header line with which you work with and from which the new record is sent to the table body itself.

To copy the records, we can use a SELECT statement to select all of the records from the table and then use MOVE statement that will move the records from the original table into the new internal table into the fields where the names correspond.

Following is the syntax for MOVE statement −

MOVE <table_field> TO <internal_tab_field>.

Example

REPORT  ZCUSLIST1.
TABLES: ZCUSTOMERS1.
DATA: BEGIN OF itab01 Occurs 0,
      name LIKE ZCUSTOMERS1-name,
      dob LIKE ZCUSTOMERS1-dob,
END OF itab01.

Select * FROM ZCUSTOMERS1.
MOVE ZCUSTOMERS1-name TO itab01-name.
MOVE ZCUSTOMERS1-dob TO itab01-dob.
ENDSELECT.

Write: / itab01-name, itab01-dob.

The above code produces the following output −

MARGARET   02.11.1994

The select loop fills each field one at a time, using the MOVE statement to move the data from one table’s field to the other. In the above example, MOVE statements were used to move the contents of the ZCUSTOMERS1 table to the corresponding fields in the internal table. You can accomplish this action with just one line of code. You can use the MOVECORRESPONDING statement.

Following is the syntax for MOVE-CORRESPONDING statement −

MOVE-CORRESPONDING <table_name> TO <internal_tab>.

It tells the system to move the data from the fields of ZCUSTOMERS1 to their corresponding fields in itab01.

Example

REPORT  ZCUSTOMERLIST.
TABLES: ZCUSTOMERS1.
DATA: Begin of itab01 occurs 0,
      customer LIKE ZCUSTOMERS1-customer,
      name LIKE ZCUSTOMERS1-name,
      title LIKE ZCUSTOMERS1-title,
      dob LIKE ZCUSTOMERS1-dob,
END OF itab01.

SELECT * from ZCUSTOMERS1.
MOVE-Corresponding ZCUSTOMERS1 TO itab01.
APPEND itab01.
ENDSELECT.
LOOP AT itab01.
Write: / itab01-name, itab01-dob.
ENDLOOP.

The above code produces the following output −

MARK           21.05.1981
JAMES          14.08.1977
AURIELE        19.06.1990
STEPHEN        22.07.1985
MARGARET       02.11.1994

This is made possible by the fact that both have matching field names. When making use of this statement, you need to make sure that both fields have matching data types and lengths. It has been done here with the LIKE statement previously.

«« Previous
Next »»

No comments:

Post a Comment