![](/cvo_files/imgs_sap_abap/logos/logo_cvo_academy_it.png)
✒️ABAP Los cortes de control utilizando tablas internas
ABAP Los cortes de control utilizando tablas internas
![ABAP Los cortes de control utilizando tablas internas ABAP Los cortes de control utilizando tablas internas](https://www.cvosoft.com/cvo_files/imgs_sap_abap/og-images/434.jpg)
Control Breaks - Internal Tables
Definition of Control Break:
A control break is a technique used to structure a screen output report in order to summarize data and display totals for different fields in the report.
Implementation Steps for a Control Break:
Control breaks are a technique composed of several steps for correct functionality. Taking the example of the "Customer Report by Region and District"
- The initial step is to declare the internal output table for the report.
TYPES: BEGIN OF ty_customer,
region(10) TYPE c,
district(10) TYPE c,
customer_no(5) TYPE c,
amount TYPE i,
END OF ty_customer.
DATA: it_customers TYPE STANDARD TABLE OF ty_customer,
wa_customer LIKE LINE OF it_customers.
- Declaration of Auxiliary Structure: Due to an ABAP specificity, an auxiliary structure, named wa_customers_aux, needs to be declared.
DATA: wa_customer_aux LIKE LINE OF it_customers.
- Declaration of Summation Variables: Variables of type I are declared for each amount to be summarized.
DATA: v_total_amount_customers TYPE i,
v_total_amount_district TYPE i,
v_total_amount_region TYPE i.
- Table Initialization and Population: As a programming best practice, the internal table is cleared to ensure no content errors. After this, the table is filled with content.
REFRESH it_customers.
CLEAR it_customers.
wa_customer-region = 'CAPITAL'.
wa_customer-district = 'Caballito'.
wa_customer-customer_no = '00001'.
wa_customer-amount = 30.
APPEND wa_customer TO it_customers.
- Sorting the Internal Table: The internal table it_customers needs to be sorted based on the fields used for the control break: REGION, DISTRICT, and CUSTOMER_NO.
SORT it_customers BY region district customer_no.
- Control Break Technique Implementation: Within a LOOP-ENDLOOP construct, the core of the control break technique is executed. It involves copying the content of the wa_customers structure to wa_customers_aux.
LOOP AT it_customers INTO wa_customer.
CLEAR wa_customer_aux.
wa_customer_aux = wa_customer.ENDLOOP.
- AT NEW and AT END OF Statements: ABAP provides two statements, AT NEW and AT END OF, for control breaks. AT NEW is executed for each new value in the field, while AT END OF is executed when processing the last record with a specific field value.
AT NEW district.
CLEAR v_total_amount_district.
FORMAT COLOR 5.
WRITE:/ sy-uline(83).
WRITE:/ 'District:', wa_customer_aux-district.
WRITE:/ sy-uline(83).
ENDAT.
AT END OF district.
FORMAT COLOR 5.
WRITE:/10 'Total Amount District', v_total_amount_district.
ENDAT.
- Total Calculation and Printing: For each new region, district, and customer number, AT NEW statements are executed to initialize corresponding total variables. Then, the accumulated totals are printed.
AT END OF district.
FORMAT COLOR 5.
WRITE:/10 'Total Amount District', v_total_amount_district.
ENDAT.
Summary:
Control breaks involve declaring structures, initializing variables, populating tables, implementing sorting, and utilizing AT NEW and AT END OF statements for effective control break implementation in ABAP.
E.g
* Structure type for customers
TYPES: BEGIN OF ty_customers,
region(10) TYPE c,
district(10) TYPE c,
customer_no(5) TYPE c,
amount TYPE i,
END OF ty_customers.
* Internal tables and structures
DATA: it_customers TYPE STANDARD TABLE OF ty_customers,
wa_customers LIKE LINE OF it_customers,
wa_customers_aux LIKE LINE OF it_customers.
* Accumulation flag
DATA: v_total_amount_accumulated TYPE i.
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Adding records to the internal table
*----------------------------------------------------------------------*
CLEAR wa_customers.
wa_customers-region = 'CAPITAL'.
wa_customers-district = 'Caballito'.
wa_customers-customer_no = '00001'.
wa_customers-amount = 30.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'CAPITAL'.
wa_customers-district = 'Caballito'.
wa_customers-customer_no = '00700'.
wa_customers-amount = 1000.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'CAPITAL'.
wa_customers-district = 'Caballito'.
wa_customers-customer_no = '00705'.
wa_customers-amount = 10000.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'CAPITAL'.
wa_customers-district = 'Flores'.
wa_customers-customer_no = '00005'.
wa_customers-amount = 90.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'CAPITAL'.
wa_customers-district = 'Flores'.
wa_customers-customer_no = '00122'.
wa_customers-amount = 900.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'CAPITAL'.
wa_customers-district = 'Palermo'.
wa_customers-customer_no = '00011'.
wa_customers-amount = 120.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'CAPITAL'.
wa_customers-district = 'Palermo'.
wa_customers-customer_no = '00111'.
wa_customers-amount = 1200.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'CAPITAL'.
wa_customers-district = 'Belgrano'.
wa_customers-customer_no = '00012'.
wa_customers-amount = 20.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'CAPITAL'.
wa_customers-district = 'Belgrano'.
wa_customers-customer_no = '05512'.
wa_customers-amount = 2000.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'CAPITAL'.
wa_customers-district = 'Caballito'.
wa_customers-customer_no = '00001'.
wa_customers-amount = 70.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'AVELLANEDA'.
wa_customers-district = 'Sur'.
wa_customers-customer_no = '00022'.
wa_customers-amount = 30.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'AVELLANEDA'.
wa_customers-district = 'Sur'.
wa_customers-customer_no = '00100'.
wa_customers-amount = 3000.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'AVELLANEDA'.
wa_customers-district = 'Sur'.
wa_customers-customer_no = '00099'.
wa_customers-amount = 6000.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'AVELLANEDA'.
wa_customers-district = 'Norte'.
wa_customers-customer_no = '00029'.
wa_customers-amount = 190.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'AVELLANEDA'.
wa_customers-district = 'Norte'.
wa_customers-customer_no = '00029'.
wa_customers-amount = 190.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'AVELLANEDA'.
wa_customers-district = 'Norte'.
wa_customers-customer_no = '00301'.
wa_customers-amount = 500.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'QUILMES'.
wa_customers-district = 'Centro'.
wa_customers-customer_no = '00084'.
wa_customers-amount = 300.
APPEND wa_customers TO it_customers.
CLEAR wa_customers.
wa_customers-region = 'QUILMES'.
wa_customers-district = 'Ezpeleta'.
wa_customers-customer_no = '00172'.
wa_customers-amount = 50.
APPEND wa_customers TO it_customers.
*----------------------------------------------------------------------*
* Sorting the internal table
*----------------------------------------------------------------------*
SORT it_customers BY region district customer_no.
*----------------------------------------------------------------------*
* Looping through the internal table
*----------------------------------------------------------------------*
LOOP AT it_customers INTO wa_customers.
* Copying the content of the record to an auxiliary structure
CLEAR wa_customers_aux.
wa_customers_aux = wa_customers.
*----------------------------------------------------------------------*
* For each new customer
*----------------------------------------------------------------------*
AT NEW region.
CLEAR v_total_amount_accumulated.
FORMAT COLOR 1.
WRITE:/ sy-uline(55).
WRITE:/ 'Region:', wa_customers_aux-region.
WRITE:/ sy-uline(55).
ENDAT.
AT NEW district.
FORMAT COLOR 5.
WRITE:/ 'District:', wa_customers_aux-district.
ENDAT.
AT NEW customer_no.
CLEAR v_total_amount_accumulated.
ENDAT.
* Accumulating the amount
ADD wa_customers_aux-amount TO v_total_amount_accumulated.
*----------------------------------------------------------------------*
* Printing detail titles
*----------------------------------------------------------------------*
AT END OF customer_no.
FORMAT COLOR 3.
WRITE:/10 'Customer:', wa_customers_aux-customer_no,
30 'Total Amount', v_total_amount_accumulated.
ENDAT.
ENDLOOP.
WRITE:/ sy-uline(55).
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 149 Publicaciones Académicas
🎓Egresado de los módulos:
- Carrera Consultor en SAP Fiori
- Carrera Consultor ABAP Nivel Avanzado
- Carrera Consultor ABAP Nivel Inicial
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez