Appearance
Lesson 3 — Types, Structures & Table Operations
Lesson 2 used tables of simple integers. Real application data normally has several related fields.
text
Product
id
name
category
priceABAP structures model one record. Internal tables of structures model collections of records.
Learning objectives
You should be able to:
- define local elementary types with
TYPES; - define structured types;
- build structures with
VALUE; - define table types with explicit categories/keys;
- explain standard, sorted, and hashed tables at an introductory level;
- create a complete table with
VALUE; - append/insert structured rows;
- read by index and by key;
- use
line_exists; - update a structured row;
- delete internal-table rows;
- use
LOOP AT ... WHERE; - explain that these operations modify memory, not database persistence.
1. Create the class
Create:
text
ZCL_ABAP_L03_###Implement IF_OO_ADT_CLASSRUN.
2. Local type aliases
abap
TYPES ty_product_code TYPE c LENGTH 6.
TYPES ty_category TYPE c LENGTH 10.A type describes values. A DATA declaration creates a data object.
abap
DATA product_code TYPE ty_product_code.3. Fixed-length character behavior
abap
TYPES ty_code TYPE c LENGTH 6.
DATA code TYPE ty_code VALUE 'ABCDEFGH'.A fixed-length character field only has space for its declared length. This is why choosing a type is part of the data model, not merely syntax.
4. Define a structure type
abap
TYPES:
BEGIN OF ty_product,
id TYPE ty_product_code,
name TYPE string,
category TYPE ty_category,
price TYPE decfloat34,
END OF ty_product.5. Create a structure
abap
DATA product TYPE ty_product.
product-id = 'P00001'.
product-name = 'Keyboard'.
product-category = 'IT'.
product-price = CONV decfloat34( '49.90' ).Component selector for structures:
text
-6. Use VALUE
Prefer concise construction when all values are known:
abap
DATA(product2) = VALUE ty_product(
id = 'P00002'
name = 'Mouse'
category = 'IT'
price = '19.95'
).7. Create a table type
For ordered list-style processing:
abap
TYPES tt_products TYPE STANDARD TABLE OF ty_product WITH EMPTY KEY.For unique key-based access:
abap
TYPES tt_products_by_id
TYPE HASHED TABLE OF ty_product
WITH UNIQUE KEY id.For always-sorted key-based access:
abap
TYPES tt_products_sorted
TYPE SORTED TABLE OF ty_product
WITH UNIQUE KEY id.8. Standard vs sorted vs hashed
| Category | Main characteristic |
|---|---|
| Standard | index-oriented; flexible general-purpose table |
| Sorted | maintained in key order; optimized key access |
| Hashed | unique hash key; optimized key access; no meaningful primary index |
The correct choice depends on expected access patterns.
Do not choose a hashed table merely because it sounds faster.
9. Create a whole table with VALUE
abap
DATA(products) = VALUE tt_products(
( id = 'P00001' name = 'Keyboard' category = 'IT' price = '49.90' )
( id = 'P00002' name = 'Mouse' category = 'IT' price = '19.95' )
( id = 'P00003' name = 'Desk' category = 'OFFICE' price = '250.00' )
).10. Loop structured rows
abap
LOOP AT products INTO DATA(product).
out->write( |{ product-id } - { product-name } - { product-price }| ).
ENDLOOP.11. Read by index
For a standard table:
abap
DATA(second_product) = products[ 2 ].Index access depends on table category. A hashed table is key-oriented and has no primary table index for index access.
12. Read by key
With a table expression:
abap
DATA(product) = products[ id = 'P00002' ].If the row may not exist:
abap
IF line_exists( products[ id = 'P00002' ] ).
out->write( 'Found' ).
ENDIF.For a standard table with an empty primary key, id = ... is a free-key search and is linear. Lesson design should distinguish convenient syntax from performance characteristics.
13. Use a keyed table for key-centric access
abap
DATA(products_by_id) = VALUE tt_products_by_id(
( id = 'P00001' name = 'Keyboard' category = 'IT' price = '49.90' )
( id = 'P00002' name = 'Mouse' category = 'IT' price = '19.95' )
).Then:
abap
DATA(mouse) = products_by_id[ id = 'P00002' ].The declared hash key supports optimized key access.
14. Insert a row
For a keyed table, use INSERT ... INTO TABLE:
abap
INSERT VALUE ty_product(
id = 'P00004'
name = 'Monitor'
category = 'IT'
price = '199.00'
) INTO TABLE products_by_id.For unique keys, duplicate-key insertion is not accepted.
15. Update a row safely
For a standard table, assigning through a field symbol avoids copying the line:
abap
LOOP AT products ASSIGNING FIELD-SYMBOL(<product>)
WHERE id = 'P00002'.
<product>-price = '17.95'.
ENDLOOP.<product> points to the current table line, so changing a component changes the line in the internal table.
16. Delete rows
abap
DELETE products WHERE category = 'OFFICE'.Or for a keyed table:
abap
DELETE TABLE products_by_id WITH TABLE KEY id = 'P00004'.17. These are not database CRUD operations
You have now performed operations analogous to:
text
create a row
read a row
update a row
delete a rowBut all of them happened in an internal table in ABAP memory.
text
ABAP internal table
temporary memory
disappears with execution/session lifecycleDatabase persistence is introduced later.
18. Filter loops
abap
LOOP AT products INTO DATA(product)
WHERE category = 'IT'.
out->write( product-name ).
ENDLOOP.19. Model an Employee directory
Create:
abap
TYPES:
BEGIN OF ty_employee,
id TYPE i,
first_name TYPE string,
last_name TYPE string,
department TYPE c LENGTH 3,
salary TYPE decfloat34,
END OF ty_employee.Choose a table category/key appropriate for unique employee ID lookup.
Use VALUE to create at least eight employees.
Perform:
text
read by ID
filter by department
update salary
remove one employee
print remaining employees20. Summary
You can now model meaningful in-memory business data:
text
TYPES
structures
VALUE
standard/sorted/hashed tables
explicit keys
key reads
updates
deletes
LOOP WHEREThe next lesson moves from passive structures to objects that own state and behavior.