Data Types and Variables in SAP ABAP - Comprehensive Notes

Data types and variables in SAP ABAP – Comprehensive notes

  • Purpose of the lesson

    • Understand data types and variables in SAP ABAP-like syntax.
    • Explore characters, integers, numeric characters, and packed decimals (P) used in programs and database tables.
    • Learn about system fields that are populated at runtime.
  • Program structure and variable scope

    • Variables are categorized as local or global in a program.
    • Global variables: accessible across the entire program and all subroutines.
    • Local variables: defined in a specific area (form/routine) and not accessible outside that area; they are cleared when returning from the area.
    • Global variables are often declared in a top include so they are visible to the whole program and all subroutines.
    • Subroutines (started with PERFORM) can access global variables (e.g., IT report) but local variables are only valid within their subroutine.
  • Example program reference

    • FI regrouping report, transaction ZFI006, used to illustrate global vs. local variables.
    • IT report is defined as a global variable in the top include, and is created/initialized at startup.
    • In the subroutines (e.g., check input, get data, set display grid), IT report is updated in get data and reused in display grid with the same data.
    • IT report is a structure/table that is created at the beginning and updated as data is retrieved.
    • If a variable is defined in the main program area (global) it can be accessed from all subroutines; if defined in a subroutine, it is local to that subroutine.
  • Declaring variables and data types

    • Keywords and concepts used:
    • data: the keyword to declare variables.
    • TYPE and TYPES: define data types. TYPE creates a single type alias; TYPES allows defining multiple types (a mini “type library”).
    • TYPE BEGIN OF … END OF: define a structured type (like a record/struct).
    • TYPE TABLE OF …: define an internal table type (array of structures or elements).
    • OCCURS n: marks an internal table with multiple lines (historical syntax; modern ABAP uses internal tables with standard table types).
    • BEGIN OF … END OF: blocks to define a structure inside a data declaration.
    • LIKE: references existing data objects (e.g., LIKE LINE OF lt_mykn i1) to ensure matching structure.
    • LIKE LINE OF: matches the line type of another internal table.
    • BEGIN OF and END OF blocks define a structure with named components.
  • Basic data types and how they are used

    • Character data: type C; fixed-length character field. You can also declare a character string with a specific length, e.g., extClength2ext{C length } 2.
    • Numeric characters: type N; contains only numeric characters (not a full numeric type). Example: a length of 3 means three numeric digits.
    • Packed decimal numbers: type P; used for decimal numbers with a defined length and number of decimals. Example: declaring a decimal with 4 digits total and 2 decimals: P(4,2)P(4,2).
    • Strings: type STRING; variable-length string without a fixed maximum unless constrained by a separate length.
    • Predefined types and dictionary types:
    • Data dictionary types like KNA1 (customer master data) or Kunener (data element for customer number) can be referenced directly or via type aliases.
    • Example: declare a type Kunener that references the data element Kunener; or declare a type like KNA1 (table) or a field like KUNNR.
    • Structures and tables:
    • A single structure (record) is defined via TYPE BEGIN OF … END OF or via a named structure in the TYPE definitions.
    • A table is defined as TYPE TABLE OF or TYPE STANDARD TABLE OF WITH DEFAULT KEY (or similar) depending on ABAP version.
    • A table with multiple lines uses OCCURS (historical) or a standard internal table definition.
  • Examples and terminology from the session

    • Example data types and variables used in the lecture:
    • A type referencing KNA1 or Kunener for customer data (e.g., KUNNR as the customer number).
    • A type Kunener used to hold a single customer number (scalar).
    • A type KNI1 used as a table element type (structure) or as a table type depending on the declaration.
    • A table type defined as TYPE TABLE OF KNI1; this creates an internal table of KNI1 structures.
    • Example of a single-line structure vs. a multi-line table:
    • STRUCTURE LS_Status: a structure with several fields (one row).
    • LT_MyKNI1: an internal table holding multiple KNI1-like structures.
    • Example of data declarations and initializations discussed:
    • lvkunnr type KUNNR, lvname type C length 40, etc. (illustrative names).
    • A decimal example: a value 1.25 with two decimals; note the decimal separator issue (comma vs dot) depending on locale. 1.251.25 or 1,251,25 depending on configuration; if the separator is wrong, a runtime error occurs and a correction to the correct separator is needed.
    • Initializing and populating data:
    • Create a line/row in a line variable ls_mykn i1; assign fields using MOVE or MOVE CORRESPONDING.
    • Append a line to an internal table: APPENDls<em>mykni1TOlt</em>mykni1APPEND ls<em>mykn i1 TO lt</em>mykn i1 (or similar naming in the actual code).
    • Move corresponding: moves data by matching field names between two structures; it does not compare data types beyond name matching. Useful when field names match but you do not want to map by position.
    • Append vs MOVE CORRESPONDING: append adds a new row to an internal table; move corresponding maps fields from one structure to another; both require compatible field names; append requires matching structure types for the fields.
    • Using LIKE for compatibility:
    • You can declare a work variable like line of lt_mykn i1 to reuse the same field structure as the table element.
  • Data declarations and usage patterns discussed

    • Data declaration example patterns (ABAP-like syntax):
    • Global: ITREPORT TYPE TABLE OF ITREPORT_LINE.
    • Local/FORM area: declarations inside a PERFORM-ed subroutine are local.
    • Type and data separation:
    • Types (via TYPES or TYPE BEGIN OF) define data types and structures without holding values.
    • Data statements actually hold values in variables or internal tables.
    • Defining a structure and then a table of that structure:
    • BEGIN OF mystruct, field1 TYPE KUNNR, field2 TYPE NAME, END OF mystruct.
    • TYPES: BEGIN OF tystruct, field1 TYPE KUNNR, field2 TYPE NAME, END OF tystruct.
    • LTMYTABLE TYPE TABLE OF ty_struct.
    • Defining a table by referencing dictionary elements:
    • TYPE TABLE OF KNA1 with a field KUNNR referencing a data element from SC11.
  • Practical considerations when defining and using types

    • When you define a type KNI1 that references a dictionary table type or a single data element, you must be aware whether it represents a simple value or a composite structure.
    • If you declare a type as a table of KNI1, you must ensure the KNI1 type is itself compatible with a table element.
    • If you declare a structure in a TYPE BEGIN OF block, you can then reference that structure in a TYPE TABLE OF to create an internal table.
    • When using the LIKE operator, you reference an already-defined line type to ensure compatibility (e.g., LSMYKNI1 LIKE LINE OF LTMYKNI1).
  • Floating, fixed, and formatting details

    • Decimal formatting depends on system settings: the thousands separator and decimal separator may be either comma or dot, controlled by the system; mismatches cause syntax or runtime errors.
    • Example: setting a decimal value in a field of type P with 4 total length and 2 decimals: P(4,2)P(4,2), then assign a value like 1.251.25 or 1,251,25 depending on the configuration.
  • Global vs local memory lifecycle and best practices

    • Global declarations persist across the program and subroutines; local declarations are temporary and cleaned up when leaving the subroutine.
    • For variables used across many subroutines, prefer global declarations (e.g., in a top include).
    • For temporary or intermediate data used only in one subroutine, prefer local declarations to keep memory clean and reduce side effects.
    • A practical rule: use a single global variable if multiple areas of the program need to reference the same data; otherwise, limit scope to the subroutine to avoid clutter and confusion.
  • System (SY) fields for runtime information

    • SY is the system structure that the SAP system updates at runtime.
    • Key fields discussed:
    • SY-TABIX: current line index (row number) inside internal tables.
    • SY-DA TE and SY-TIME: current date and time; DST ( daylight saving time indicator) / summertime indicator.
    • SY-LANGU: language code (one character length).
    • SY-SCREEN: screen number (e.g., 1000).
    • SY-CLIENT: client number (e.g., 100).
    • SY-USER: user who performed the operation.
    • SY- PROGRAM: name of the program currently in use.
    • SY-MSGID, SY-MSGTYPE, SY-MSGNO, SY-MSGV1, SY-MSGV2, SY-MSGV3, SY-MSGV4: message details if an error or warning occurs.
    • SY-SUBRC: return code for the last ABAP statement; a non-zero value indicates an issue.
    • Practical use: check SY-MSGID and related fields when debugging errors; use SY-SUBRC to branch logic after statements.
  • Wrap-up and reflections

    • Variables, types, and constants were covered: scope (global vs local), basic and complex data types, and system fields.
    • Key techniques discussed: MOVE CORRESPONDING versus APPEND, read/write into internal tables, and the importance of consistent field names for data movement.
    • If there are no questions, this completes today’s session.
  • Quick reference recap of key terms you should remember

    • Global vs local variables; top include usage; IT report as a global variable example.
    • Data declarations: DATA, TYPE, TYPES, BEGIN OF … END OF, OCCURS, LIKE, LIKE LINE OF.
    • Basic types: C (character), N (numeric characters), P (packed decimal with length and decimals), STRING (dynamic length).
    • Structures and internal tables: TYPE BEGIN OF, TYPE TABLE OF, OCCURS (historical), APPEND, MOVE CORRESPONDING.
    • Dictionary references: Kunener, KNA1, KUNNR, and how to declare types referencing them.
    • System fields: SY-structures (SY-TABIX, SY-DA TE, SY-LANGU, SY-CLIENT, SY-USER, SY-MSGID, SY-MSGNO, SY-MSGV1-4, SY-SUBRC).
  • Optional exam-style takeaway questions to test yourself

    • What is the difference between a local and a global variable? Where should you declare each and why?
    • How does MOVE CORRESPONDING differ from a direct MOVE in ABAP? What are the caveats?
    • When would you use APPEND versus MOVE for populating an internal table?
    • How would you declare a decimal value with 4 total digits and 2 decimals? What does the syntax look like?
    • How can you access a field in a dictionary-based type versus a locally declared type? What are the risks of mismatched types?
    • Which SY fields would you check first if you encounter a runtime error in an ABAP program?