Internal Tables and Work Areas
Using Internal Tables and Work Areas in SAP ABAP
programming, internal table operations
What is an internal table and work area in SAP ABAP ? Difference between
internal table and work area
Internal Tables and Work Areas are temporary memory locations which are used to store the
data of a database tables at run time and in other way we can call them as instances of database
tables.
Internal tables and Work areas
Internal Tables
Temporary memory locations which are
used to store the data of database
table.
Internal table is an instance of database
table.
By default 8kb memory will be allocated
and it increases dynamically( +8kb
+8kb).
Work Areas
Temporary memory locations which are
used to store the data of database table.
Work area is an instance of database
table.
Internal tables can store multiple records.
Work areas can store only one record.
General Syntax :
General Syntax:
DATA : <ITAB> TYPE TABLE OF <DBTABLE> DATA : <WA> TYPE <DBTABLE> .
Internal Table declaration
DATA : <ITAB> TYPE TABLE OF <DBTABLE> .
Example :
DATA : IT_ITAB TYPE TABLE OF MARA .
Example Explanation : Here IT_ITAB is the name of Internal table and MARA is the Database
table name, after declaring,IT_ITAB will be the instance of table MARA.
Internal Table declaration with user defined types
DATA : <ITAB> TYPE TABLE OF <userdefinedtype>.
Example is : TYPES : BEGIN OF TY_TABLE,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MEINS TYPE MARA-MEINS,
END OF TY_TABLE.<br>
DATA : IT_ITAB TYPE TABLE OF TY_TABLE .
Here TY_TABLE is a user defined type with 3 fields
Internal Table Deceleration Old Syntax ( Obsolete Method )
Obsolete Syntax: DATA : IT_ITAB TYPE TABLE OF MARA occurs 0 with header line .
In this example Internal Table itself acts as work area, technically a work area will be created at
first row of the internal table.
Reading data from database
Table in SAP ABAP
Reading data from databases tables using OPEN SQL (SELECT) statements
in SAP ABAP
The only way of reading data from a database table is using select statements, in the below
example we will read data from MARA table in various ways.
Operation
Explanation
INTO TABLE ITAB
Means getting data into an internal table ITAB from database table
INTO WA
Means getting data into WAa work area ( Work area can store only one
record )
INTO CORRESPONDING
FIELDS OF TABLE ITAB
Means getting data of common fields of data base table and user
INTO CORRESPONDING
FIELDS OF WA
Means getting data of common fields of data base table and work
defined internal tableITAB( Here ITAB is a user defined internal table )
areaWA( Here WA is a work area )
Read whole data from MARA
To read all records from MARA table, we use below code
data : it_mara type table of mara . " Declare internal table of type MARA
Select * from MARA into table it_mara . " Read all records from MARA table and store in
it_mara internal table
Read single record from MARA based on where condition
data : wa_mara type mara . " Declare work area of type MARA, because we are getting
only one record
Select single * from MARA into wa_mara where matnr = '00001' . " Read one records
from MARA table and store in wa_mara work area
OR
data : it_mara type table of mara . " Declare internal table of type MARA
select * from MARA into table it_mara where matnr = '00001' . " Read all records from
MARA table where MATNR is 00001, MATNR is a key field .
Reading data into corresponding fields
By using INTO CORRESPONDING FIELDS of statement we can get data into a user defined
internal table.
As per performance standards, this method is not preferable
TYPES : BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MEINS TYPE MARA-MEINS,
MBRSH TYPE MARA-MBRSH,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA . "Declare internal table of type user defined
table.
SELECT * FROM MARA INTO CORRESPONDING FIELD OF TABLE IT_MARA . " Here we are
getting data from <code>MARA</code> table into internal table <code>IT_MARA
</code> which contains only four fields.
Reading data into user defined internal table
Reading data from a database table into a user defined table.
This method is advisable as it gets limited fields from database table
TYPES : BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MEINS TYPE MARA-MEINS,
MBRSH TYPE MARA-MBRSH,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA. " Declare a Internal table of user defined type
SELECT MATNR, MTART, MEINS, MBRSH FROM MARA INTO TABLE IT_MARA. " Get list of
fields Data Into internal table
" Now data of four fields is available in IT_MARA
Internal Table Operations
Internal table operations are most important for a ABAP developer, below are some of the most
important internal table operations
APPEND
INSERT
SORT
DESCRIBE TABLE
READ TABLE WITH KEY
READ TABLE WITH INDEX
LOOP....ENDLOOP.
MODIFY
DELETE
DELETE ADJACENT DUPLICATES
CLEAR, REFRESH, FREE
APPEND LINES OF
INSERT LINES OF
MOVE
COLLECT
Using APPEND in SAP ABAP
APPEND statement is used to append or add a record from work area to internal table, the new
record will be added at the end of the internal table.
Syntax: APPEND TO
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
WA_MARA-MATNR = '00001'.
WA_MARA-MTART = 'FERT'.
WA_MARA-MEINS = 'EA'.
APPEND WA_MARA TO IT_MARA . "APPNED WORK AREA TO INTERNAL TABLE
Using INSERT in SAP ABAP
INSERT statement is used to insert or add a record from work area into internal table at specified
location
Syntax: INSERT INTO INDEX
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
WA_MARA-MATNR = '00001'.
WA_MARA-MTART = 'FERT'.
WA_MARA-MEINS = 'EA'.
INSERT WA_MARA INTO IT_MARA INDEX 2 . "The record will be inserted into internal
table at 2nd position
Using SORT in SAP ABAP
SORT is used to sort a Internal table data in ascending order or descending order, by default it will
sort data in ascending order. In addition to this we can able to sort data based on specified fields.
Syntax1 : SORT . "Default sorts data in ascending order
Syntax2 : SORT DESCENDING . " Sort in descending order
Syntax3 : SORT BY ...ASCENDING/DESCENDING
."It sorts data by specified fields , ..
Using DESCRIBE TABLE in SAP ABAP
DESCRIBE TABLE is used to count the no of records in a internal table
Syntax: DESCRIBE TABLE LINES ." Count the no. of
record of a internal table, here v_lines stores count
DATA : V_LINES TYPE I. "Type integer
DESCRIBE TABLE IT_MARA LINES V_LINES ." Count the no. of record of a internal table
Write : v_lines .
Using READ TABLE WITH KEY in SAP ABAP
READ TABLE WITH KEY .. BINARY SEARCH is used to read a single record from an internal table
into work area specified by field name and field value .
BINARY SEARCH is a search mechanism which is used to read a record from internal table into
work area very fastly, the functionality of binary search it divides the into parts and searches. The
internal table must be sorted in ascending order before using binary search .
Syntax: READ TABLE INTO WITH KEY =
=
BINARY SEARCH .
." Read a record into work area where some field = some value
READ TABLE IT_MARA INTO WA_MARA WITH KEY MATNR = '0001' BINARY SEARCH . "Read
a
record into work area where MATNE is '0001'
Using READ TABLE WITH INDEX in SAP ABAP
READ TABLE WITH INDEX is used to read a single record from an internal table into work area
specified by index.
Syntax: READ TABLE INTO INDEX
." Read a record into work area using index ( position )
READ TABLE IT_MARA INTO WA_MARA INDEX '2' . "Read a record into work area where
index is 2.
Using LOOP....ENDLOOP. in SAP ABAP
Loop...Endloop. is also used to read data from a internal table into work area, this is used to read
multiple records serially one after one .
Syntax1: LOOP AT INTO .
ENDLOOP.
Syantax2: LOOP AT INTO WHERE =
ENDLOOP.
Syntax3: LOOP AT INTO FROM TO
ENDLOOP.
Using MODIFY in SAP ABAP
MODIFY is used to modify single or multiple internal table records based on condition
TRANSPORTING is a keyword which is used to specify list pf fields to be modified insted of all
fields.
Syntax1: MODIFY FROM INDEX TRANSPORTING
Syntax1: MODIFY FROM TRANSPORTING
WHERE
SY-TABIX is a key word which stores the index no of currently processed record. For full details
read using sy-tabix in sap abap programs .
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
SELECT * FROM MARA INTO TABLE IT_MARA . " GET DATA INTO ITAB IT_MARA
WA_MARA-MTART = 'FERT'; "ASSIGN A VALUE TO WORKAREA TO MODIFY INTERNAL TABLE
MODIFY IT_MARA FROM WA_MARA INDEX SY-TABIX TRANSPORTING MTART. " NOW THE
VALUE OF
FIELD MTART WILL BE MODIFIED FOR CURRENT RECORD IN IT_MARA
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
SELECT * FROM MARA INTO TABLE IT_MARA . " GET DATA INTO ITAB IT_MARA
WA_MARA-MTART = 'FERT'; "ASSIGN A VALUE TO WORKAREA TO MODIFY INTERNAL TABLE
MODIFY IT_MARA FROM WA_MARA TRANSPORTING MTART WHERE MATNR = '0001'. "
NOW THE
VALUE OF FIELD MTART WILL BE MODIFIED WHERE MATNR = '0001' IN ITAB
Using DELETE in SAP ABAP
DELETE is used to delete single or multiple records from an internal table from work area based
on some condition.
Syntax1: DELETE INDEX .
Syntax2: DELETE WHERE =
=.
DELETE IT_MARA INDEX 3. "3rd RECORD WILL BE DELETED IN IT_MARA
DELETE IT_MARA WHERE MTART = 'FERT'. "MATERIALS WITH MTART = 'FERT' WILL BE
DELETED
Using DELETE ADJACENT DUPLICATES in SAP ABAP
DELETE ADJACENT DUPLICATES is used to delete delete duplicate records which are adjacent to
[Link]-requisite for this is the internal table must be sorted in ascending order
Syntax1: DELETE ADJACENT DUPLICATED FROM ."ADJACENT DUPLICATED WILL
BE DELETED IN INTERNAL TABLE COMPARING ALL FIELDS
Syntax2: DELETE ADJACENT DUPLICATES FROM COMPARING
. "ADJACENT DUPLICATES WILL BE DELETED COMPARING SPECIFIED FIELDS
SORT IT_MARA ASCENDING.
DELETE ADJACENT DUPLICATES FROM IT_MARA . "3rd RECORD WILL BE DELETED IN
IT_MARA
SORT IT_MARA ASCENDING.
DELETE ADJACENT DUPLICATES IT_MARA COMPARING MATR, MTART. "DUPLICATES WILL
BE
DELETED BY COMPARING MATNR AND MTART
Using CLEAR, REFRESH, FREE in SAP ABAP
CLEAR is used to clear a value in a work area or in a variable.
REFRESH is used to clear all values in a internal table.
FREE is used to clear (free) memory of a internal table or work area. We all know when ever we
declare a internal table or work area, 8kb memory will be allocated.
Syntax clear : CLEAR "CLEAR WORK AREA OR VARIABLE
Syntax REFRESH : REFRESH "CLEAR ALL RECORDS OF INTERNAL TABLE BUT
MEMORY WILL BE THERE
Syntax FREE : FREE "FREE INTERNAL TABLE MEMORY
CLEAR WA_MARA.
REFRESH IT_MARA.
FREE IT_MARA.
Using APPEND LINES OF in SAP ABAP
APPEND LINES OF is used to append multiple records to an internal table from another internal
table .
Syntax : APPEND LINES OF FROM TO
TO .
DATA : IT_MARA TYPE TABLE OF MARA. "FIRST INTERNAL TABLE
DATA : IT_MARA1 TYPE TABLE OF MARA. "SECOND INTERNAL TABLE
APPEND LINES OF IT_MARA FROM 3 TO 5 TO IT_MARA1 . "DATA IN IT_MARA WILL BE
APPENDED
TO IT_MARA1 FROM INDEX 3 TO INDEX 5 .
Using INSERT LINES OF in SAP ABAP
INSERT LINES OF is used to INSERT multiple records to an internal table from another internal
table at specified location .
Syntax : INSERT LINES OF FROM TO
INTO INDEX .
DATA : IT_MARA TYPE TABLE OF MARA. "FIRST INTERNAL TABLE
DATA : IT_MARA1 TYPE TABLE OF MARA. "SECOND INTERNAL TABLE
INSERT LINES OF IT_MARA FROM 3 TO 5 INTO IT_MARA1 INDEX 3 . "DATA IN IT_MARA WILL
BE INSERTED INTO IT_MARA1 FROM INDEX 3 TO INDEX 5 AT INDEX 3 LOCATION .
Using MOVE in SAP ABAP
MOVE keyword is used to move one internal table data to another.
Syantax : = . "Move ITAB1 to ITAB2
Using COLLECT in SAP ABAP
COLLECT is similer to APPEND, the difference is it (COLLECT) will check wether the work area
record already exisits with the same key (only C, D, N, T), if exisits it will add numerical fields
(sum) to the existing record, if work area record dosen`t exists it will append a new record .
Syntax: COLLECT INTO .
Writing first ABAP Program
Writing Hello World program in SAP ABAP, write first ABAP program in SE38
Creating first ABAP program.
Step1: Go to T-code SE38.
Step2: Enter a program name ex:ZHELLO_WORLD and click on create .
A popup will open, then.
Step3: Select 'Executable Program' from drop down and click on 'Save'.
Step4: Select 'Local Object' (Click on Local Object, marked yellow in above image
Step5: ABAP editor will open, write 'Hello World' , Save and click on activate icon (marked yellow
in below image)
write :/ 'Hello World'..
Step6: Now you can see 'Active' beside the program name, now click
ABAP Report using Internal
tables and Work Areas ?
SAP ABAP report using internal tables and work areas, write statement in SAP
ABAP
Getting data from a data base table and display data in the form of report using SAP ABAP.
In ABAP write statement is used to print output and : is chain statement ( Print multiple records ).
Display MARA data
DATA : IT_MARA TYPE TABLE OF MARA . "INTERNAL TABLE FOR MARA TABLE
DATA : WA_MARA TYPE MARA. "WORK AREA FOR MARA
SELECT * FROM MARA INTO TABLE IT_MARA. " GET MARA DATA
LOOP AT IT_MARA INTO WA_MARA.
WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MBRSH.
"Display required fields
ENDLOOP.
Display MARA data using user defined internal table
**TYPES is a statement which is used to declare a user defiend internal table with specific
fields
TYPES : BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MEINS TYPE MARA-MEINS,
MBRSH TYPE MARA-MBRSH,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA . "INTERNAL TABLE FOR ABOVE USER DEFINED
TYPES TABLE
DATA : WA_MARA TYPE TY_MARA. "WORK AREA FOR ABOVE USER DEFINED TABLE
SELECT MATNR MTART MEINS MBRSH FROM MARA INTO TABLE IT_MARA. " GET MARA
DATA
LOOP AT IT_MARA INTO WA_MARA.
WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MBRSH.
"Display required fields
ENDLOOP.
Using messages in SAP ABAP
programing
Using messages in SAP ABAP programming, types of messages in SAP
ABAP, display error, success and warning messages in SAP ABAP
Like all programing languages, we need to raise status, error, warning, information, abort etc
messages in ABAP programs, in SAP we can raise the following messages.
I - information message
S - status message
E - error message
W - warning
A - termination message
X - exit message.
The below syntax is used to raise messages in SAP.
MESSAGE '<MESSAGE TEXT>' TYPE '<MESSAGE TYPE>'. "Message text = any string,
message type = A, E, I, S, W, X
Types of messages available in SAP
Messag
e Type
Effect
Description
Termination The message appears in a dialog box, and the program terminates.
Message
When the user has confirmed the message, control returns to the nexthighest area menu.
Error
Message
Depending on the program context, an error dialog appears or the
program terminates.
Information
The message appears in a dialog box. Once the user has confirmed the
message, the program continues immediately after
the MESSAGE statement.
Messag
e Type
Effect
Description
Status
Message
The program continues normally after the MESSAGE statement, and
the message is displayed in the status bar of the next screen.
Warning
Depending on the program context, an error dialog appears or the
program terminates.
Exit
No message is displayed, and the program terminates with a short
dump. Program terminations with a short dump normally only occur
when a runtime error occurs. Message type X allows you to force a
program termination. The short dump contains the message ID.
Types of internal tables and
differences
What are the different types of internal tables in SAP ABAP ? Difference
between standard, sorted and hashed internal tables in SAP ABAP
There are three types of internal tables in SAP ABAP programming, an ABAP developer must
know and understand before using them in ABAP programs.
Standard Internal Tables
Sorted Internal Tables
Hashed Internal Tables
Difference between Standard internal tables, Sorted internal tables and
Hashed internal tables
# Standard Internal tables Sorted Internal Tables
Hashed Internal tables
1 These are default internal These are special type of internal tables,
These are used with
tables.
2 To read a record we use
either key or index
where data is already(automatically)
logical databases i:e with
sorted as you insert the record
all fields and all records.
To read a record we use either key or
Here index operation is
index operation.
not allowed, we only use
operation.
key operation.
3 To search for a record, we To search for a record, we use binary
can use either linear
To search for a record we
search as data is already sorted.
use hashed algorithm.
We don`t use sort as data is already
search or binary search.
4 We can use sort
operation.
5 We can use insert and
append to add records.
sorted.
We only use insert, not append.
These are mainly used in
ABAP with BI projects