With my third blog series on SAP ABAP, I am trying to put together the term Internal table. The next important concept after Data Dictionary (DDIC) is the Internal table.
From this blog, you'll learn:
- What is an ABAP Internal table
- What is an ABAP Work Area
- What is the difference between the internal table and work area
- Need of internal table in SAP ABAP, when we already have a database table
- Types of Internal table
- Internal table Append operation
- Internal table Insert operation
- Internal table Read operation
- Internal table Delete operation
- Control Break Statements
Now, let's understand a very fundamental question.
What is an ABAP Internal table
Internal tables in SAP ABAP are the local tables that contain the data of the ABAP program. The lifespan of the internal table is till the time the SAP Progam is alive. The format of storing data in the internal table is rows and columns. The indication of each row is a 'line' and the indication of each column is a 'field'.
The size of the internal table gets fluctuated. It depends on the SAP program. The main reason for using internal tables is that we cannot directly hit the database. We cannot create, insert values directly into the database. Hence there is the need for internal tables.
A work area is part of the internal table. The format of the work area should be the same as that of the internal table since its the part (subset). The work area holds a single row data. We use work areas when there is a need to change the data of a particular field or to display the data of a single field.
NOTE: The entire SAP programs are done with the help of internal tables and work areas.
What is the difference between the internal table and work area
Types of Internal Tables in SAP ABAP
There are three types of internal table in SAP ABAP namely:
- Standard Table
- Sorted Table
- Hashed Table
Below is the difference between these three tables.
Internal table operations in SAP ABAP
1. APPEND:
SAP ABAP APPEND statement is used to append to insert rows at the end of the internal table.
Syntax: APPEND work_area TO internal_table.
DATA: IT_SFLIGHT TYPE TABLE OF SFLIGHT. "INTERNAL TABLE
DATA : WA_SFLIGHT TYPE SFLIGHT. " WORK AREA
WA_SFLIGHT-CARRID = 'UH'.
WA_SFLIGHT-CONNID = '0144'.
WA_SFLIGHT-FLDATE = '06.07.2020'.
APPEND WA_SFLIGHT TO IT_SFLIGHT . "APPEND WORK AREA TO INTERNAL TABLE
DATA : WA_SFLIGHT TYPE SFLIGHT. " WORK AREA
WA_SFLIGHT-CARRID = 'UH'.
WA_SFLIGHT-CONNID = '0144'.
WA_SFLIGHT-FLDATE = '06.07.2020'.
APPEND WA_SFLIGHT TO IT_SFLIGHT . "APPEND WORK AREA TO INTERNAL TABLE
2. INSERT:
SAP ABAP INSERT statement is used to insert rows at a specified location to the internal table.
Syntax: INSERT WORK_AREA INTO INTERNAL_TABLE INDEX INDEX.
DATA: IT_SFLIGHT TYPE TABLE OF SFLIGHT. "INTERNAL TABLE
DATA : WA_SFLIGHT TYPE SFLIGHT. " WORK AREA
WA_SFLIGHT-CARRID = 'UH'.
WA_SFLIGHT-CONNID = '0144'.
WA_SFLIGHT-FLDATE = '06.07.2020'.
INSERT WA_SFLIGHT INTO IT_SFLIGHT INDEX 5 . "The record will be inserted at 5th location into an internal table.
DATA : WA_SFLIGHT TYPE SFLIGHT. " WORK AREA
WA_SFLIGHT-CARRID = 'UH'.
WA_SFLIGHT-CONNID = '0144'.
WA_SFLIGHT-FLDATE = '06.07.2020'.
INSERT WA_SFLIGHT INTO IT_SFLIGHT INDEX 5 . "The record will be inserted at 5th location into an internal table.
NOTE: We can also insert multiple inset lines to an internal table with a single insert statement.
Syntax: INSERT LINES OF ITAB1 [FROM INDEX 1] [TO INDEX 2] INTO TABLE ITAB2.
3. READ:
SAP ABAP READ statement takes place via three approach.
- loop
- index
- key - transporting no fields, transporting fields.
READ TABLE IT_SFLIGHT INTO WA_SFLIGHT WITH KEY CARRID = 'AC' TRANSPORTING NO FIELDS.
Below is the syntax of using a read statement with an index in SAP ABAP.
READ TABLE IT_SFLIGHT INTO WA_SFLIGHT INDEX 3.
4. DELETE:
SAP ABAP DELETE statement is used to delete single or multiple records from an internal table.
DELETE IT_SFLIGHT INDEX 5.
DELETE IT_SFLIGHT WHERE CARRID = 'AC'.
DELETE IT_SFLIGHT WHERE CARRID = 'AC'.
Now, we will look at the 'Control Break Statements' offered by ABAP. Control Break Statements are used when we want to display the data according to the user requirements. Also, it displays the data in a specific, systematic format. Let's dive deep to learn various 'Control Break Statements'.
Control Break Statements
Those statements which are enclosed between AT and ENDAT are called control break statements.
These statements are wrapped always inside a loop.
Following are the types of Control Break Statements:
- AT FIRST / ENDAT: This statement is triggered only for the first iteration of the loop.
- AT LAST/ENDAT: This statement is triggered only for the last iteration of the loop.
- AT NEW/ENDAT: This statement is triggered when it finds the new value of the particular field.
- AT END OF/ENDAT: This statement is triggered whenever new value ends on a particular field.
- ON CHANGE OF This statement plays the same role as AT NEW. Moreover, this statement is obsolete.
1. Below is the code snippet which will give you an idea of control break statements namely ( AT FIRST/ ENDAT, AT LAST/ENDAT, SUM).
LOGIC:
*&---------------------------------------------------------------------*
*& Report ZRDS_CONTROL_STATEMENT1
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zrds_control_statement1.
DATA : it_sflight TYPE TABLE OF sflight,
wa_sflight TYPE sflight.
SELECT * FROM sflight INTO TABLE it_sflight .
LOOP AT it_sflight INTO wa_sflight.
AT FIRST.
WRITE : /5 'mandt',20 'connid',
30 'fldate',
40 'currency',
50 'planetype',
60 'seatsmax',
70 'seatsocc',
85 'paymentsum',
107 'seatsmax_b',
125 'seatsocc_b',
145 'seatsmax_f',
160 'seatsocc_f'.
ULINE.
ENDAT.
WRITE: /5 wa_sflight-mandt,20 wa_sflight-connid,30 wa_sflight-fldate,45 wa_sflight-currency,50 wa_sflight-planetype,60 wa_sflight-seatsmax,
70 wa_sflight-seatsocc, 80 wa_sflight-paymentsum,100 wa_sflight-seatsmax_b, 125 wa_sflight-seatsocc_b, 145 wa_sflight-seatsmax_f,160
wa_sflight-seatsocc_f.
at end of mandt.
uline.
endat.
at last.
sum.
write: 'total', 80 wa_sflight-paymentsum.
uline.
endat.
ENDLOOP.
*& Report ZRDS_CONTROL_STATEMENT1
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zrds_control_statement1.
DATA : it_sflight TYPE TABLE OF sflight,
wa_sflight TYPE sflight.
SELECT * FROM sflight INTO TABLE it_sflight .
LOOP AT it_sflight INTO wa_sflight.
AT FIRST.
WRITE : /5 'mandt',20 'connid',
30 'fldate',
40 'currency',
50 'planetype',
60 'seatsmax',
70 'seatsocc',
85 'paymentsum',
107 'seatsmax_b',
125 'seatsocc_b',
145 'seatsmax_f',
160 'seatsocc_f'.
ULINE.
ENDAT.
WRITE: /5 wa_sflight-mandt,20 wa_sflight-connid,30 wa_sflight-fldate,45 wa_sflight-currency,50 wa_sflight-planetype,60 wa_sflight-seatsmax,
70 wa_sflight-seatsocc, 80 wa_sflight-paymentsum,100 wa_sflight-seatsmax_b, 125 wa_sflight-seatsocc_b, 145 wa_sflight-seatsmax_f,160
wa_sflight-seatsocc_f.
at end of mandt.
uline.
endat.
at last.
sum.
write: 'total', 80 wa_sflight-paymentsum.
uline.
endat.
ENDLOOP.
OUTPUT:
2. Below is the code snippet which will give you an idea of control break statements namely ( AT NEW/ ENDAT).
LOGIC:
*&---------------------------------------------------------------------*
*& Report ZRDS_CONTROL_STATEMENT2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZRDS_CONTROL_STATEMENT2.
DATA : it_sflight TYPE TABLE OF sflight,
wa_sflight TYPE sflight.
SELECT * FROM sflight INTO TABLE it_sflight .
LOOP AT it_sflight INTO wa_sflight.
AT FIRST.
WRITE : /5 'mandt' color 3,
20 'connid' color 3,
30 'fldate' color 3,
40 'currency' color 3,
50 'planetype' color 3,
60 'seatsmax' color 3,
70 'seatsocc' color 3,
85 'paymentsum' color 3,
107 'seatsmax_b' color 3,
125 'seatsocc_b' color 3,
145 'seatsmax_f' color 3,
160 'seatsocc_f' color 3.
ULINE.
ENDAT.
at new mandt.
write: /5 wa_sflight-mandt,':CLIENT'.
uline.
endat.
WRITE: /20 wa_sflight-connid,30 wa_sflight-fldate,45 wa_sflight-currency,50 wa_sflight-planetype,60 wa_sflight-seatsmax,
70 wa_sflight-seatsocc, 80 wa_sflight-paymentsum,100 wa_sflight-seatsmax_b, 125 wa_sflight-seatsocc_b, 145 wa_sflight-seatsmax_f,160
wa_sflight-seatsocc_f.
at end of mandt.
uline.
endat.
at last.
sum.
write: 'total', 80 wa_sflight-paymentsum.
uline.
endat.
ENDLOOP.
*& Report ZRDS_CONTROL_STATEMENT2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZRDS_CONTROL_STATEMENT2.
DATA : it_sflight TYPE TABLE OF sflight,
wa_sflight TYPE sflight.
SELECT * FROM sflight INTO TABLE it_sflight .
LOOP AT it_sflight INTO wa_sflight.
AT FIRST.
WRITE : /5 'mandt' color 3,
20 'connid' color 3,
30 'fldate' color 3,
40 'currency' color 3,
50 'planetype' color 3,
60 'seatsmax' color 3,
70 'seatsocc' color 3,
85 'paymentsum' color 3,
107 'seatsmax_b' color 3,
125 'seatsocc_b' color 3,
145 'seatsmax_f' color 3,
160 'seatsocc_f' color 3.
ULINE.
ENDAT.
at new mandt.
write: /5 wa_sflight-mandt,':CLIENT'.
uline.
endat.
WRITE: /20 wa_sflight-connid,30 wa_sflight-fldate,45 wa_sflight-currency,50 wa_sflight-planetype,60 wa_sflight-seatsmax,
70 wa_sflight-seatsocc, 80 wa_sflight-paymentsum,100 wa_sflight-seatsmax_b, 125 wa_sflight-seatsocc_b, 145 wa_sflight-seatsmax_f,160
wa_sflight-seatsocc_f.
at end of mandt.
uline.
endat.
at last.
sum.
write: 'total', 80 wa_sflight-paymentsum.
uline.
endat.
ENDLOOP.
OUTPUT:
Here I come to the end of my tutorial on the Internal table.
If you enjoyed gaining knowledge out of this tutorial, stay awaited for my upcoming tutorial on SAP ABAP Report Programming.
Next blog: Introduction to ABAP Report Programming
Previous blog: Brief Introduction to ABAP Data Dictionary
0 Comments