Internal Table and Work Area
Internal Table and Work Area
Internal table acts as a container which is used to store the record sets. That is used to store the
data fetched from the database table.
So due to performance reason every time access to database would not be so good and decrease
the performance. So you just select the data from the database and store it in the intermediate
table. This table is called INTERNAL TABLE. So it's an replica of the database.
WORKAREA is a structure that can hold only one record at a time. It is a collection of fields. We
use work area as we cannot directly read from a table. In order to interact with a table we need
work area.
Example:
Sr. No Material Number Description
1 M13 Material 1
2 M14 Material 2
Internal Table
Syntax: Internal table
Data:
DATA : <ITAB_NAME> TYPE TABLE OF <DATABASE TABLE NAME>
(Or)
SELECT <F1>
<F2>
<F3> (OR)
*
FROM <DATABASE TABLE NAME>
INTO TABLE <ITAB NAME>
WHERE <CONDITION>.
Syntax:
LOOP AT
<ITAB NAME > INTO <WA NAME>
ENDLOOP.
Types:
It is a statement which is used to declare our own custom data types.
Then we can declare our variables referring to above user defined data types.
Syntax:
TYPES: BEGIN OF <USER DEFINED TYPE NAME>,
<F1> TYPE <TABLE NAME- FIELD NAME>,
<F2> TYPE <TABLE NAME- FIELD NAME>,
<F3> TYPE <TABLE NAME- FIELD NAME>,
END OF <USER DEFINED TYPE NAME>.
INSERT
This statement is used to insert the record from work area into internal table at a specified
location.
Syntax:
INSERT <WA> INTO <ITAB> INDEX <NO>.
SORT
This is the statement used to sort the internal table data either in ascending order or
descending order.
Sort statement start’s the data by ascending order by
default. It sort’s based on order of the fields i.e., F1, F2, and
F3......
Syntax:
SORT <ITAB> BY <F1>
<F2>
<F3> <ASCENDING / DESCENDING>.
DESCRIBE TABLE
This statement is used to find the total no of records in an internal table.
Syntax:
DESCRIBE TABLE <ITAB> LINES <VAR NAME>
READ TABLE
This statement is used to read a single record from internal table into work
area. There are two forms of the read statement
1. With index
2. With Key
LOOP AT.........ENDLOOP
This statement is used to read multiple records from internal table in to work area one by one.
Syntax 1:
LOOP AT <ITAB> INTO <WA>.
....
....
....
ENDLOOP.
Syntax 2:
LOOP AT <ITAB> INTO <WA> FROM <N1> TO <N2>.
.....
.....
ENDLOOP.
Syntax 3:
LOOP AT <ITAB> INTO <WA> WHERE <FNAME1 = VALUE>
<FNAME2 = VALUE>
....
....
....
ENDLOOP.
MODIFY
This statement is used to modify a single record or multiple records based on the condition.
Syntax:
MODIFY <ITAB> FROM <WA>
INDEX<NO>
TRANSPORTING <FNAMEl>
<FNAME2>
<FNAME3>.
DELETE
This statement is used to delete the data from internal table.
Syntax:
1. DELETE <ITAB> FROM <WA>.
2. DELETE <ITAB> INDEX <NO>.
3. DELETE <ITAB> WHERE<F1> =
<VALUE>
<F2> = <VALUE>.
DELETE ADJACENT DUPLICATES FROM
This statement is used to delete the duplicate records which are adjacent i.e., side by
side to each other.
The pre-requisite for this statement is the internal table should be sorted in ascending order.
Syntax:
DELETE ADJACENT DUPLICATES FROM <ITAB> COMPARING <F1>
<F2>
<F3> (Or)
ALL FIELDS.
CLEAR
This statement is used to delete the data from a variable or work area or internal table.
Syntax:
1. CLEAR <VARIABLE NAME>.
2. CLEAR <WORK AREA>
3. CLEAR <ITAB> [].
REFRESH
This statement is used to delete the data from internal table only.
Syntax:
REFRESH <ITAB NAME>.
FREE
This statement is similar to CLEAR and REFRESH
i.e., it will also delete the data from a variable, work area, internal
table. But the difference is,
➢ With the clear and refresh statement the data will be deleted but not the memory.
➢ With the FREE statement the data will be deleted and memory also.
Syntax:
1. FREE <VARIABLE NAME>.
2. FREE <WORK>.
3. FREE <ITAB>.
COLLECT
It is similar to APPEND statement.
Syntax:
COLLECT <WA> INTO <ITAB>.
APPEND LINES OF
This statement is used to add multiple records from one ITAB to another ITAB.
Syntax:
APPEND LINES OF <ITAB> FROM <N1> TO <N2><ITAB2>.
INSERT LINES OF
This statement is used to add the data from one internal table another internal table at a
specified location with INDEX number.
Syntax:
INSERT LINES OF <ITAB> FROM <N1> TO <N2><ITAB2>INDEX <NO>.
MOVE CORRESPONDING:
This statement is used to move the data for corresponding fields from one work area to
another work area.
Syntax:
MOVE-CORRESPONDING <WAI> TO <WA2>.
Note:
This is a very dangerous statement as it has to compare each field from source work area
to target work area instead of use below statement.
Source code:
*&---------------------------------------------------------------------
*
*& Report ZINTERNAL_TABLE
*&---------------------------------------------------------------------
*
*&
*&---------------------------------------------------------------------
*
REPORT zinternal_table.
Output:
2. Insert: To insert the records into internal table in a specific
location.
Syntax: Insert <WA> into <Itab> index n.
REPORT zinternal_table.
*Append
wa_T001-BUKRS = 'TS22'.
wa_T001-BUTXT = 'Tata steel'.
wa_T001-LAND1 = 'India'.
Example:
REPORT zinternal_table.
wa_T001-BUKRS = 'TS22'.
wa_T001-BUTXT = 'Tata steel'.
wa_T001-LAND1 = 'India'.
Output:
4. Deleting: To delete the duplicate record from internal
table.
Syntax: Delete adjacent duplicate from Itab comparing
BUKRS(field name).
Source code:
REPORT zinternal_table.
wa_T001-BUKRS = 'TS22'.
wa_T001-BUTXT = 'Tata steel'.
wa_T001-LAND1 = 'India'.
Output:
5. Describing: To know the count of internal table records.
Syntax: Describe table <Itab> lines <integer variable>.
Source Code:
REPORT zinternal_table.