Working with internal Tables
Below topics we are going to discuss in detail
1.Types of Internal Tables
2.Creating Internal Tables
3.Populating and Processing Internal Tables
4.Usage of work area and Field-symbol
5.Data manipulation in Internal table
6.Parallel Cursor usage
7.Control Break Statements
8.Messages
1. Types of Internal Tables
In SAP ABAP (Advanced Business Application Programming), Internal tables are used to store temporary data during program execution.
There are three main types of internal tables in SAP:
Standard Internal Table
This is default internal table type.
In Standard internal table, Table entries are stored in the order they are inserted.
Usage: Standard internal table is recommended only when sequence of fields are mattered or dealing with small datasets.
Example : DATA: lt_standard TYPE STANDARD TABLE OF mara.
Sorted Internal Table
Sorted internal table entries are automatically sorted based on a defined key.
Usage: Sorted internal table is recommended for reading the entries based on keys
Example: DATA: lt_sorted TYPE SORTED TABLE OF mara WITH UNIQUE KEY matnr.
Hashed Internal Table
Hashed internal table uses a hashed algorithm for key-based access.
Usage: Hashed internal table is recommended for reading single entry from table by using key
Example: DATA: lt_hashed TYPE HASHED TABLE OF mara WITH UNIQUE KEY matnr.
Decision Framework of Standard/ Sorted / Hashed Internal Tables
2. Creating Internal Tables
Examples for creating Internal tables with type declarations and using standard table
3. Populating and processing Internal Table
Example1: Standard table Example with Reading data from table MARA (General Article Data)
Example 2 : Appending the data to Internal table and display.
Example 3 : Sorted table data display
a
Example 4: Hashed table data display
4. Usage of Work area and Field Symbol
Example on using work area
Result:
Example on Field-symbol
Result
5. Data manipulation in Internal Table
Result
6.Parallel Cursor
Parallel cursor is a performance optimization technique used primarily to enhance the efficiency of programs involving nested loops, especially when dealing with large internal tables.
Improve the performance of program.
Decreases the CPU and memory consumption associated with extensive loop iterations.
7.Control Break Statements
Control break statements are events inside the loop statement.
There are 5 control break statements in ABAP.
They are used within loop.(Except ON CHANGE OF which can be used outside the loop as well)
AT FIRST - ENDAT
AT NEW - ENDAT
AT END OF - ENDAT
AT LAST - ENDAT
ON CHANGE OF
AT FIRST: Will trigger at the first run of the loop.
AT LAST: Will trigger at the last run of the loop.
The below 3 events are normally used when the table is sorted.
AT END OF : When we use At end for a field, it will trigger whenever there is any change in any of the fields from the left to that of the particular field. The trigger point will be the at the last occurrence of the same value for the field.
AT NEW: When we use At new for a field, it will trigger whenever there is any change in any of the fields from the left to that of the particular field.The trigger point will be the at the first occurrence of the new value for the field.
ON CHANGE OF: On change of it triggers only when there is any change in the particular field.
On change of can be used outside the loop too
Example code on Control Break Statements
8. Messages
Success Message:
Syntax : MESSAGE 'Success message: Operation successful!' TYPE ’S'.
Output :
Information Message
Syntax: MESSAGE 'Informational message.' TYPE ‘I'.
Output:
Warning Message:
Syntax: MESSAGE 'Warning message: Data inconsistent' TYPE ‘W'.
Output
Syntax: MESSAGE 'Warning message: Data inconsistent' TYPE 'W' DISPLAY LIKE ‘I'.
Error Message:
Syntax: MESSAGE 'Error Message: An error occurred during processing.' TYPE ‘E'.
Output
Abort Message
Syntax: MESSAGE 'Abort Message: Program terminated due to critical error.' TYPE 'A'.
Output:
Exit Message
Syntax: MESSAGE 'Exit Message: Critical error encountered!' TYPE 'X'.
Output:
Creating Message through text messages in report
Code: Message text-001 type 'S'.
Output:
Thanks!!
Finally we covered working with internal tables, creating and populating data in report , work area and field symbol usage , Parallel cursor usage, Control break statements, Messages