- SAP ABAP
- What is SAP ABAP
- SAP ABAP Data Dictionary and Domain
- SAP ABAP Data Element
- SAP ABAP Database Table
- SAP ABAP Database tables and views
- SAP ABAP Foreign Key
- SAP ABAP Indexes
- SAP ABAP Structure
- SAP ABAP Package
- SAP ABAP Adding Fields to SAP Standard Table
- SAP ABAP Internal Table and Database Table
- SAP ABAP Select Option and Parameter
- SAP ABAP Types of Internal Table
- SAP ABAP ways of Declaring Internal Tables
- SAP ABAP Mastering Initialization Technique
- SAP ABAP Operations on Internal Table
- SAP ABAP Record Retrieval
- SAP ABAP Insert, Modify and Delete data in the Internal table by using Keywords
- SAP ABAP Sorting and Removing Adjacent Duplicates
- SAP ABAP Seamless Data Transfer Between Internal Tables
- SAP ABAP Search Help Types
- SAP ABAP Lock Objects and Types
- SAP ABAP Buffering and Its Types
- SAP ABAP TMG
- SAP ABAP Table Types
- SAP ABAP Views
- SAP ABAP Control Break Statement
- SAP ABAP COMMIT and ROLLBACK
- SAP ABAP Joins
- SAP For All Entries
- SAP ABAP Procedure to Fill Final Internal Table
- SAP ABAP Modularization
- SAP ABAP Function Group and Function Module
- SAP ABAP SELECT Options
24DDIC0408 – Control Break Statements in SAP ABAP Unlocking the Power of Internal Table Events
What Are Control Break Statements?
Control break statements are special constructs that allow developers to execute code at specific points when processing internal tables. These points, known as control breaks, occur when there is a change in the value of a specified field within a sorted internal table. Control break statements make it easier to handle grouped data, calculate totals, and generate structured reports.
Key Control Break Statements in ABAP
- AT FIRST
- AT LAST
- AT NEW <field>
- AT END OF <field>
AT FIRST
The AT FIRST statement is used to perform operations at the very beginning of the loop, before any rows are processed. This is useful for initializing variables or setting up the environment for data processing.
Example:
LOOP AT itab INTO wa.
AT FIRST.
WRITE: / ‘Start of processing’.
ENDAT.
” Process the row
WRITE: / wa-field1, wa-field2.
ENDLOOP.
In this example, the message “Start of processing” is printed once, before any rows of the internal table itab are processed.
AT LAST
The AT LAST statement is used to perform operations at the very end of the loop, after all rows have been processed. This is often used to finalize totals or clean up resources.
Example:
LOOP AT itab INTO wa.
“Process the row
WRITE: / wa-field1, wa-field2.
AT LAST.
WRITE: / ‘End of processing’.
ENDAT.
ENDLOOP.
Here, the message “End of processing” is printed once, after all rows of the internal table have been processed.
AT NEW <field>
The AT NEW <field> statement triggers whenever there is a change in the value of the specified field within a sorted internal table. This is particularly useful for handling grouped data, where you want to perform an action at the start of each new group.
Example:
LOOP AT itab INTO wa.
AT NEW group_field.
WRITE: / ‘New group:’, wa-group_field.
ENDAT.
” Process the row
WRITE: / wa-group_field, wa-detail_field.
ENDLOOP.
In this example, the message “New group” followed by the group field value is printed each time the value of group_field changes.
AT END OF <field>
The AT END OF <field> statement is used to perform operations when the value of the specified field is about to change, or at the end of the group. This is commonly used for summarizing or finalizing data within each group.
Example:
DATA: lv_total TYPE i.
LOOP AT itab INTO wa.
AT NEW group_field.
lv_total = 0.
ENDAT.
” Process the row
WRITE: / wa-group_field, wa-detail_field.
lv_total = lv_total + wa-amount.
AT END OF group_field.
WRITE: / ‘Total for group:’, lv_total.
ENDAT.
ENDLOOP.
In this scenario, the total for each group is calculated and printed at the end of each group.
Practical Applications
Control break statements are invaluable in many practical scenarios:
Generating Reports: They help in creating structured reports where data is grouped and summarized, such as sales reports by region or product.
Data Aggregation: Useful for aggregating data, like summing up sales figures or counting occurrences within groups.
Data Validation: Ensuring data consistency and integrity by performing checks at the start and end of data processing.
Author : Aniket Pawar, 9373518385
24DDIC0408 – Control Break Statements in SAP ABAP Unlocking the Power of Internal Table Events
What Are Control Break Statements?
Control break statements are special constructs that allow developers to execute code at specific points when processing internal tables. These points, known as control breaks, occur when there is a change in the value of a specified field within a sorted internal table. Control break statements make it easier to handle grouped data, calculate totals, and generate structured reports.
Key Control Break Statements in ABAP
- AT FIRST
- AT LAST
- AT NEW <field>
- AT END OF <field>
AT FIRST
The AT FIRST statement is used to perform operations at the very beginning of the loop, before any rows are processed. This is useful for initializing variables or setting up the environment for data processing.
Example:
LOOP AT itab INTO wa.
AT FIRST.
WRITE: / ‘Start of processing’.
ENDAT.
” Process the row
WRITE: / wa-field1, wa-field2.
ENDLOOP.
In this example, the message “Start of processing” is printed once, before any rows of the internal table itab are processed.
AT LAST
The AT LAST statement is used to perform operations at the very end of the loop, after all rows have been processed. This is often used to finalize totals or clean up resources.
Example:
LOOP AT itab INTO wa.
“Process the row
WRITE: / wa-field1, wa-field2.
AT LAST.
WRITE: / ‘End of processing’.
ENDAT.
ENDLOOP.
Here, the message “End of processing” is printed once, after all rows of the internal table have been processed.
AT NEW <field>
The AT NEW <field> statement triggers whenever there is a change in the value of the specified field within a sorted internal table. This is particularly useful for handling grouped data, where you want to perform an action at the start of each new group.
Example:
LOOP AT itab INTO wa.
AT NEW group_field.
WRITE: / ‘New group:’, wa-group_field.
ENDAT.
” Process the row
WRITE: / wa-group_field, wa-detail_field.
ENDLOOP.
In this example, the message “New group” followed by the group field value is printed each time the value of group_field changes.
AT END OF <field>
The AT END OF <field> statement is used to perform operations when the value of the specified field is about to change, or at the end of the group. This is commonly used for summarizing or finalizing data within each group.
Example:
DATA: lv_total TYPE i.
LOOP AT itab INTO wa.
AT NEW group_field.
lv_total = 0.
ENDAT.
” Process the row
WRITE: / wa-group_field, wa-detail_field.
lv_total = lv_total + wa-amount.
AT END OF group_field.
WRITE: / ‘Total for group:’, lv_total.
ENDAT.
ENDLOOP.
In this scenario, the total for each group is calculated and printed at the end of each group.
Practical Applications
Control break statements are invaluable in many practical scenarios:
Generating Reports: They help in creating structured reports where data is grouped and summarized, such as sales reports by region or product.
Data Aggregation: Useful for aggregating data, like summing up sales figures or counting occurrences within groups.
Data Validation: Ensuring data consistency and integrity by performing checks at the start and end of data processing.
Author : Aniket Pawar, 9373518385