ABAP Core Part 2: DATA, TYPES, CONSTANTS, Work Areas and Internal Tables
Declare data the way real ECC/S/4 programs do: TYPES, DATA and CONSTANTS, dictionary types, structures (work areas) and internal tables, filling them with APPEND, CLEAR/REFRESH/FREE, and naming conventions — with the modern inline and VALUE syntax as a side note.
Explanation
📌 KEY POINTS
✅ TYPES defines a type (no memory); DATA creates a variable; CONSTANTS creates a value that never changes.
✅ A structure (work area) holds one row; an internal table holds many rows of that structure.
✅ Rows are filled into the work area and added with APPEND; CLEAR empties a variable, REFRESH/CLEAR itab[] empties a table.
💡 Prefer types from the dictionary (S_CARR_ID, SFLIGHT) — they carry length, labels and conversions.
━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE
Part 1 ran a first program. Now you learn how to declare the data your program works with — the foundation of everything else.
━━━━━━━━━━━━━━━━━━━━
▶ 1. TYPES — THE BLUEPRINT
TYPES: BEGIN OF ty_flight,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
fldate TYPE s_date,
seatsmax TYPE s_seatsmax,
seatsocc TYPE s_seatsocc,
END OF ty_flight.
TYPES ty_flights TYPE STANDARD TABLE OF ty_flight.
A type allocates no memory; it describes a shape.
━━━━━━━━━━━━━━━━━━━━
▶ 2. DATA — VARIABLES, WORK AREAS, TABLES
DATA lv_count TYPE i. " elementary variable
DATA ls_flight TYPE ty_flight. " structure = work area
DATA lt_flights TYPE ty_flights. " internal table
Naming convention used in most projects: lv_ (local variable), ls_/wa_ (structure), lt_/it_ (table), gv_/gs_/gt_ (global), p_ (parameter), s_ (select-option).
━━━━━━━━━━━━━━━━━━━━
▶ 3. CONSTANTS — FIXED VALUES
CONSTANTS lc_max_rows TYPE i VALUE 100.
Use constants instead of "magic values" scattered in code.
━━━━━━━━━━━━━━━━━━━━
▶ 4. FILLING A TABLE
Fill the work area field by field, then APPEND it:
ls_flight-carrid = 'LH'. ls_flight-connid = '0400'. APPEND ls_flight TO lt_flights.
CLEAR ls_flight before filling the next row, so no old values remain.
━━━━━━━━━━━━━━━━━━━━
▶ 5. CLEAR, REFRESH, FREE
- CLEAR lv_x / CLEAR ls_flight — initial value.
- REFRESH lt_flights (or CLEAR lt_flights[]) — delete all rows, keep memory.
- FREE lt_flights — delete rows and release memory (large tables).
━━━━━━━━━━━━━━━━━━━━
▶ 6. REFERENCE TO DICTIONARY TYPES
TYPE sflight-price, TYPE s_carr_id, TYPE STANDARD TABLE OF sflight — reuse dictionary definitions so lengths and field labels stay consistent.
━━━━━━━━━━━━━━━━━━━━
▶ 7. MODERN SYNTAX (SIDE NOTE)
From 7.40: inline declarations DATA(lv_x) = … and the VALUE operator build data in one expression. You will see them in newer code; the classic form remains valid everywhere.
━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP
- Define ty_flight and ty_flights as in the code example.
- Fill three rows with work area + APPEND, clearing the work area each time.
- Loop over the table and compute free seats.
- Output with WRITE.
✅ CHECKPOINT: you can declare types, variables, constants, work areas and internal tables and fill them.
Code example
REPORT zabap_core_data. * ===== Step 1: TYPES - blueprints =====TYPES: BEGIN OF ty_flight, carrid TYPE s_carr_id, connid TYPE s_conn_id, fldate TYPE s_date, seatsmax TYPE s_seatsmax, seatsocc TYPE s_seatsocc, END OF ty_flight.TYPES ty_flights TYPE STANDARD TABLE OF ty_flight. * ===== Step 2: CONSTANTS - no magic values =====CONSTANTS lc_min_free_seats TYPE i VALUE 10. * ===== Step 3: DATA - work area, table, variable =====DATA: ls_flight TYPE ty_flight, lt_flights TYPE ty_flights, lv_free TYPE i. START-OF-SELECTION.* ===== Step 4: fill the table row by row ===== CLEAR ls_flight. ls_flight-carrid = 'LH'. ls_flight-connid = '0400'. ls_flight-fldate = '20261001'. ls_flight-seatsmax = 220. ls_flight-seatsocc = 215. APPEND ls_flight TO lt_flights. CLEAR ls_flight. ls_flight-carrid = 'LH'. ls_flight-connid = '0402'. ls_flight-fldate = '20261002'. ls_flight-seatsmax = 220. ls_flight-seatsocc = 120. APPEND ls_flight TO lt_flights. CLEAR ls_flight. ls_flight-carrid = 'AA'. ls_flight-connid = '0017'. ls_flight-fldate = '20261003'. ls_flight-seatsmax = 380. ls_flight-seatsocc = 300. APPEND ls_flight TO lt_flights. * ===== Step 5: process with a work area ===== LOOP AT lt_flights INTO ls_flight. lv_free = ls_flight-seatsmax - ls_flight-seatsocc. IF lv_free < lc_min_free_seats. WRITE: / ls_flight-carrid, ls_flight-connid, 'almost full, free:', lv_free. ELSE. WRITE: / ls_flight-carrid, ls_flight-connid, 'free seats:', lv_free. ENDIF. ENDLOOP. * ===== Step 6: empty the table ===== REFRESH lt_flights. * 💡 Modern syntax (7.40+):* DATA(lt_x) = VALUE ty_flights( ( carrid = 'LH' connid = '0400' ) ).* LOOP AT lt_x INTO DATA(ls_x). DATA(lv_f) = ls_x-seatsmax - ls_x-seatsocc. ENDLOOP.Real project scenario
A legacy report declared 60 global variables at the top, many with TYPE c LENGTH 10 for airline and document numbers, and used the literal 'O' for "open" in 14 places. A change of the status code required a risky search-and-replace, and a truncated customer number caused wrong postings.
The refactoring introduced dictionary-based types, a CONSTANTS structure for status values and inline declarations inside small methods. The next status change touched one line, and type mismatches were caught by the syntax check.
Common mistakes
- Declaring everything globally at the top.
- Using generic types (c LENGTH 10) instead of dictionary types.
- Magic literals instead of constants.
- Forgetting BASE and overwriting a table with VALUE.
- Assuming inline variables are block-scoped.
Best practices
- Use dictionary types for business fields.
- Declare close to first use (inline where it helps readability).
- Use CONSTANTS for fixed codes.
- Build data with VALUE instead of many APPEND statements.
- Follow your team's naming convention consistently.
Interview angle
Questions: "Difference between TYPES and DATA?", "What are inline declarations?", "How does the VALUE operator work?"
Strong answer: TYPES defines a type without memory, DATA creates a variable, CONSTANTS a fixed value; inline declarations (DATA(x) = …, INTO TABLE @DATA(…)) derive the type from the right side at first use; VALUE #( ) constructs structures and tables, with BASE to keep existing lines. Recommend dictionary types and constants over literals.
Practice this lesson
Quick check
1. What does DATA(lt_flights) = VALUE ty_flights( ( carrid = 'LH' ) ( carrid = 'AA' ) ). do?
2. Which statement declares a constant in ABAP?
3. What is the difference between TYPE and LIKE?
Interview questions
1. Explain the difference between TYPE and LIKE in ABAP.
Answer it in your own words first, then compare.
2. What is the difference between VALUE #( ), NEW #( ) and CORRESPONDING #( ) in modern ABAP? Give a practical example for each.
Answer it in your own words first, then compare.
Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.
More lessons in this topic
- ABAP Core Part 1: Your First ABAP Program and the Development Tools
- ABAP Core Part 10: Clean ABAP Basics for Maintainable Code
- ABAP Core Part 3: Data Types and Literals
- ABAP Core Part 4: Control Flow and Building Output Texts
- ABAP Core Part 5: Selection Screens — PARAMETERS, SELECT-OPTIONS and Validation
- ABAP Core Part 6: System Fields — sy-subrc, sy-tabix, sy-datum and More
- ABAP Core Part 7: Operators, Calculations and Type Conversion
- ABAP Core Part 8: Messages and User-Friendly Error Handling