ITAB Structure in ABAP #
Structure #
Objectives #
- Understand what a
STRUCTUREis and what it’s used for - Know how to declare a
STRUCTUREwithTYPESandDATA - Know how to fill structure fields
- Access field values for reading or displaying
- Apply structures to organize heterogeneous data
Definition #
A
STRUCTUREis a grouping of multipleVARIABLESof different types under the same name.
It can be viewed as a table row where each field corresponds to a column.
This allows manipulating logically related data sets without having to manage each variable individually.
Think of a
STRUCTUREas a person’s identity card
- last name
- first name
- date of birth
- address
Each piece of information is a field, and together they form a unique
STRUCTURErepresenting the person.
The fields of a
STRUCTUREcan be of different types: characters, integers, dates, etc.
Declaration #
To create a STRUCTURE, we use TYPES:
TYPES: BEGIN OF ty_person,
nom TYPE char20,
prenom TYPE char20,
age TYPE i,
ville TYPE char20,
END OF ty_person.
Then we declare a structure variable with DATA:
DATA: ls_person TYPE ty_person.
Here,
ls_personis an empty card ready to be filled with a person’s information.
The
BEGIN OF ... END OFframes the field definitions. Each field has a name and aTYPE.
Populating fields #
Each field of the STRUCTURE is accessible with the - separator:
ls_person-nom = 'Dupont'.
ls_person-prenom = 'Jean'.
ls_person-age = 35.
ls_person-ville = 'Paris'.
To display the values:
WRITE: / ls_person-nom, ls_person-prenom, ls_person-age, ls_person-ville.
Result: Dupont Jean 35 Paris
We fill the identity card with the corresponding information.
Each field is independent but belongs to the sameSTRUCTURE.
Exercises #
1 – Create a structure for a book #
Define a
STRUCTURE ty_livrewith the fields
- titre (CHAR30)
- auteur (CHAR30)
- annee (I)
- genre (CHAR20)
Declare a
VARIABLE ls_livreand fill the fields with your information.
SOLUTION
TYPES: BEGIN OF ty_livre,
titre TYPE char30,
auteur TYPE char30,
annee TYPE i,
genre TYPE char20,
END OF ty_livre.
DATA: ls_livre TYPE ty_livre.
ls_livre-titre = '1984'.
ls_livre-auteur = 'George Orwell'.
ls_livre-annee = 1949.
ls_livre-genre = 'Dystopie'.
WRITE: / ls_livre-titre, ls_livre-auteur, ls_livre-annee, ls_livre-genre.
2 – Access a field #
Display only the author of the previously declared book.
SOLUTION
WRITE: / ls_livre-auteur.
Result: George Orwell
3 – Modify a field #
Change the publication year to 1950 and display the new value.
SOLUTION
ls_livre-annee = 1950.
WRITE: / ls_livre-annee.
Result: 1950
Summary #
- A
STRUCTUREgroups multiple heterogeneous fields under the same name.- Each field is accessible with
structure-field.- Allows organizing data in a logical and clear manner.
[!TIP] Identity card for a structure, address book for a table of structures.
[!TIP] To manipulate related data, avoid declaring individual variables for each piece of information.
Move corresponding (structures) #
Objectives #
- Copy only corresponding fields between two structures
- Understand how field name matching works
- Know how to ignore non-common fields
- Understand the difference between
MOVEandMOVE-CORRESPONDING - Use
MOVE-CORRESPONDINGwith dynamic structures
Definition #
MOVE-CORRESPONDING struc_source TO struc_target.
The
MOVE-CORRESPONDINGinstruction allows copying only fields with the same name between two structures.
- Fields must have the same name (case doesn’t matter).
- Fields not present in the destination are ignored.
- Fields not present in the source remain unchanged in the destination.
Imagine two administrative forms
- The first contains Name, First name, Country, Age
- The second contains Name, Country, Postal code
→MOVE-CORRESPONDINGwill only copy Name and Country.
Example 1 – Structures with common fields #
TYPES: BEGIN OF ty_person_src,
name TYPE char20,
country TYPE char3,
age TYPE i,
END OF ty_person_src.
TYPES: BEGIN OF ty_person_dest,
country TYPE char3,
name TYPE char20,
zipcode TYPE char5,
END OF ty_person_dest.
DATA: ls_person_src TYPE ty_person_src,
ls_person_dest TYPE ty_person_dest.
" --- SOURCE STRUCTURE INITIALIZATION ---
ls_person_src-name = 'Luis'.
ls_person_src-country = 'ES'.
ls_person_src-age = 32.
" --- DESTINATION STRUCTURE INITIALIZATION ---
ls_person_dest-zipcode = '75000'.
" --- COPY CORRESPONDING FIELDS ---
MOVE-CORRESPONDING ls_person_src TO ls_person_dest.
" --- DISPLAY RESULT ---
WRITE:/ 'Name :', ls_person_dest-name.
WRITE:/ 'Country :', ls_person_dest-country.
WRITE:/ 'ZipCode :', ls_person_dest-zipcode.
nameandcountryare copied because they exist in both structures.zipcoderemains unchanged, because it doesn’t exist in the source.ageis not transferred because it doesn’t exist in the destination.
MOVE-CORRESPONDINGdoesn’t throw an error even if the structures are not identical – it only copies what it can.
Example 2 – Difference between move and move-corresponding #
TYPES: BEGIN OF ty_employee_src,
id TYPE i,
name TYPE char20,
position TYPE char10,
END OF ty_employee_src.
TYPES: BEGIN OF ty_employee_dest,
name TYPE char20,
id TYPE i,
END OF ty_employee_dest.
DATA: ls_emp_src TYPE ty_employee_src,
ls_emp_dest TYPE ty_employee_dest.
ls_emp_src-id = 100.
ls_emp_src-name = 'Claire'.
ls_emp_src-position = 'DEV'.
" --- SIMPLE COPY ---
MOVE ls_emp_src TO ls_emp_dest.
WRITE:/ 'Simple MOVE -> Name:', ls_emp_dest-name,
'ID:', ls_emp_dest-id.
" --- RESET ---
CLEAR ls_emp_dest.
" --- COPY WITH MOVE-CORRESPONDING ---
MOVE-CORRESPONDING ls_emp_src TO ls_emp_dest.
WRITE:/ 'MOVE-CORRESPONDING -> Name:', ls_emp_dest-name,
'ID:', ls_emp_dest-id.
MOVEcopies values field by field in declaration order → error if structures are incompatible.MOVE-CORRESPONDINGcopies by name → safer and more flexible.
MOVEcan produce errors or inconsistencies if the order or type of fields differs.
Example 3 – Dynamic structures with field-symbols #
FIELD-SYMBOLS: <fs_src> TYPE any,
<fs_dest> TYPE any.
DATA: ls_src TYPE ty_person_src,
ls_dest TYPE ty_person_dest.
ASSIGN ls_src TO <fs_src>.
ASSIGN ls_dest TO <fs_dest>.
ls_src-name = 'Renata'.
ls_src-country = 'BR'.
ls_src-age = 29.
IF <fs_src> IS ASSIGNED AND <fs_dest> IS ASSIGNED.
MOVE-CORRESPONDING <fs_src> TO <fs_dest>.
ENDIF.
WRITE:/ 'Name:', ls_dest-name, 'Country:', ls_dest-country.
Useful in generic programs where structures are only known at runtime.
Useful when manipulating dynamically defined structures (for example via a data dictionary or user selection).
Best practices #
| Best practice | Explanation |
|---|---|
| Verify field names | Transfer depends solely on field name |
| Prefer MOVE-CORRESPONDING for similar structures | Helps avoid type or order errors |
| Initialize destination before transfer | Avoids residual values in non-copied fields |
| Don’t overuse simple MOVE | MOVE doesn’t handle structure differences |
| Use FIELD-SYMBOLS for generic programs | Makes code reusable and more flexible |
Exercises #
1 – Copy common fields between two structures #
Copy only the
nameandcountryfields fromls_person_srctols_person_dest.
SOLUTION
MOVE-CORRESPONDING ls_person_src TO ls_person_dest.
WRITE:/ 'Name:', ls_person_dest-name, 'Country:', ls_person_dest-country.
2 – Structures with non-existent field #
Add a
zipcodefield in the destination and observe that its value remains unchanged.
SOLUTION
TYPES: BEGIN OF ty_person_dest_zip,
name TYPE char20,
country TYPE char3,
zipcode TYPE char5,
END OF ty_person_dest_zip.
DATA: ls_person_dest_zip TYPE ty_person_dest_zip.
MOVE-CORRESPONDING ls_person_src TO ls_person_dest_zip.
WRITE:/ 'Name:', ls_person_dest_zip-name,
'Country:', ls_person_dest_zip-country,
'ZipCode:', ls_person_dest_zip-zipcode.
3 – Use dynamic field-symbols #
Copy the content of one structure to another via FIELD-SYMBOLS.
SOLUTION
FIELD-SYMBOLS: <fs_src> TYPE any,
<fs_dest> TYPE any.
ASSIGN ls_person_src TO <fs_src>.
ASSIGN ls_person_dest TO <fs_dest>.
IF <fs_src> IS ASSIGNED AND <fs_dest> IS ASSIGNED.
MOVE-CORRESPONDING <fs_src> TO <fs_dest>.
ENDIF.
Summary #
MOVE-CORRESPONDINGbetween structures allows transferring only fields with the same name.
- Ignores non-common fields
- Safe copy, without type errors
- Compatible with dynamic structures via FIELD-SYMBOLS
- More flexible alternative to
MOVE[!TIP] Copy common information between two different forms, without touching what doesn’t correspond.