0% found this document useful (0 votes)
4 views24 pages

Internal Table and Work Area

The document explains the concepts of Internal Tables and Work Areas in programming, detailing their definitions, usage, and syntax. Internal Tables are used to store record sets fetched from a database, while Work Areas hold a single record and are necessary for interacting with tables. It also outlines various operations that can be performed on Internal Tables, such as APPEND, INSERT, SORT, DELETE, and provides best programming practices.

Uploaded by

Laksh Mhr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views24 pages

Internal Table and Work Area

The document explains the concepts of Internal Tables and Work Areas in programming, detailing their definitions, usage, and syntax. Internal Tables are used to store record sets fetched from a database, while Work Areas hold a single record and are necessary for interacting with tables. It also outlines various operations that can be performed on Internal Tables, such as APPEND, INSERT, SORT, DELETE, and provides best programming practices.

Uploaded by

Laksh Mhr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

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

Work Area 3 M15 Material 3


4 M16 Material 4

Internal Table
Syntax: Internal table
Data:
DATA : <ITAB_NAME> TYPE TABLE OF <DATABASE TABLE NAME>
(Or)

DATA : <ITAB_NAME> TYPE TABLE OF <USER DEFINED TYPE>

Syntax: WORK AREA

DATA: <WANAME> TYPE <DATABASE TABLENAME>.


(Or)
DATA: <WANAME>TYPE<USER DEFINED TYPE>.

Accessing work areas:


Syntax:
<WANAME>-<FIELDNAME> = ‘VALUE’.
Reading the data from data base tables:
Select is the statement which is used to read the data from data base tables into
internal tables.
Syntax:

SELECT <F1>
<F2>
<F3> (OR)
*
FROM <DATABASE TABLE NAME>
INTO TABLE <ITAB NAME>
WHERE <CONDITION>.

Displaying internal table data:


To display each record from internal table, we need to read each record in to work area
and then use the WRITE statement and display each field.

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>.

Best programming standards:


1. Always declare the internal tables using user defined TYPES statement.
2. Never use SELECT *, instead of use select list of the fields.
3. Never use INTO CORRESPONDING FIELDS OF.
4. The source list of the fields and the target internal table fields list must be same.

List of the internal table operations:


1. APPEND
2. INSERT
3. SORT
4. DESCRIBE TABLE
5. READ TABLE
6. LOOP AT
7. MODIFY
8. DELETE
9. DELETE ADJACENT DUPLICATES FROM
10. CLEAR
11. REFRESH
12. FREE
13. COLLECT
14. MOVE-CORRESPONDING
15. APPEND LINES OF
16. INSERT LINES OF
APPEND
This statement is used to add a single record from work area to internal
table. The record always added at the bottom of the internal table.
Syntax:
APPEND <WA> TO <ITAB>.

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

Read Table with Index:


This statement will read the record specified by the index number into work area.
Syntax:
READ TABLE <ITAB> INTO <WA> INDEX <NO>.

Read Table with Key:


This statement is used to read a single record from internal table into work area which
is specified by the field values.
Syntax:
READ TABLE <ITAB> INTO <WA> WITH KEY<F1> = <VALUE>
<F2> = <VALUE>
BINARY SEARCH
Note:
The pre-requisite for the above statement is the internal table should be sorted in
ascending order.
Binary Search:
It is one type of algorithm which is used to improve the time taken for reading a record from an
internal table.
In real time every read statement will have Binary search keyword.

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.

The difference between the APPEND and COLLECT is,


The COLLECT statement check’s whether the work area record already exists in the
internal table or not.
➢ If it exists it will add the numeric field.
➢ If it doesn’t exist it will add new record to internal table. (APPEND)

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.

PARAMETERS p_cntry TYPE land1.


TYPES :BEGIN OF t001,
bukrs TYPE t001-bukrs,
butxt TYPE t001-butxt,
land1 TYPE t001-land1,
END OF t001.
DATA wa_t001 TYPE t001.
DATA it_t001 TYPE TABLE OF t001.
SELECT bukrs butxt land1 FROM t001
INTO TABLE it_t001
UP TO 5 ROWS
WHERE land1 = p_cntry.
WRITE : / 'TOTAL RECORDS FETCHED', sy-dbcnt.
ULINE.
LOOP AT it_t001 INTO wa_t001.
WRITE:/ sy-tabix,
wa_t001-bukrs,
wa_t001-butxt,
wa_t001-land1.
ENDLOOP.
ULINE.
Output:
Internal Table operation
1. Append: To append or add the work area record at the last
position of internal table.
Syntax: Append <WA> to <ITab>.
Example: Before Append
REPORT zinternal_table.

PARAMETERS p_cntry TYPE land1.


TYPES :BEGIN OF t001,
bukrs TYPE t001-bukrs,
butxt TYPE t001-butxt,
land1 TYPE t001-land1,
END OF t001.
DATA wa_t001 TYPE t001.
DATA it_t001 TYPE TABLE OF t001.
SELECT bukrs butxt land1 FROM t001
INTO TABLE it_t001
UP TO 5 ROWS
WHERE land1 = p_cntry.
write: /25 ' Before Append' color 3.

WRITE : / 'TOTAL RECORDS FETCHED', sy-dbcnt.


ULINE.
LOOP AT it_t001 INTO wa_t001.
WRITE:/ sy-tabix,
wa_t001-bukrs,
wa_t001-butxt,
wa_t001-land1.
ENDLOOP.
ULINE.
*Append
wa_T001-BUKRS = 'TS22'.
wa_T001-BUTXT = 'Tata steel'.
wa_T001-LAND1 = 'India'.

write: /25 'After Append' color 3.


append wa_t001 to it_t001.

LOOP AT it_t001 INTO wa_t001.


WRITE:/ sy-tabix,
wa_t001-bukrs,
wa_t001-butxt,
wa_t001-land1.
ENDLOOP.
ULINE.

Output:
2. Insert: To insert the records into internal table in a specific
location.
Syntax: Insert <WA> into <Itab> index n.
REPORT zinternal_table.

PARAMETERS p_cntry TYPE land1.


TYPES :BEGIN OF t001,
bukrs TYPE t001-bukrs,
butxt TYPE t001-butxt,
land1 TYPE t001-land1,
END OF t001.
DATA wa_t001 TYPE t001.
DATA it_t001 TYPE TABLE OF t001.
SELECT bukrs butxt land1 FROM t001
INTO TABLE it_t001
UP TO 5 ROWS
WHERE land1 = p_cntry.
write: /25 ' Before Insert' color 3.

WRITE : / 'TOTAL RECORDS FETCHED', sy-dbcnt.


ULINE.
LOOP AT it_t001 INTO wa_t001.
WRITE:/ sy-tabix,
wa_t001-bukrs,
wa_t001-butxt,
wa_t001-land1.
ENDLOOP.
ULINE.

*Append
wa_T001-BUKRS = 'TS22'.
wa_T001-BUTXT = 'Tata steel'.
wa_T001-LAND1 = 'India'.

Insert wa_t001 INTO it_t001 index 2.


Insert wa_t001 INTO it_t001 index 4.

write: /25 'After Insert' color 3.

LOOP AT it_t001 INTO wa_t001.


WRITE:/ sy-tabix,
wa_t001-bukrs,
wa_t001-butxt,
wa_t001-land1.
ENDLOOP.
ULINE.
3. Sorting: Arrange the records of internal table in an order
(Ascending/Descending).
Syntax: Sort <Itab> table name.

Example:
REPORT zinternal_table.

PARAMETERS p_cntry TYPE land1.


TYPES :BEGIN OF t001,
bukrs TYPE t001-bukrs,
butxt TYPE t001-butxt,
land1 TYPE t001-land1,
END OF t001.
DATA wa_t001 TYPE t001.
DATA it_t001 TYPE TABLE OF t001.
SELECT bukrs butxt land1 FROM t001
INTO TABLE it_t001
UP TO 5 ROWS
WHERE land1 = p_cntry.
write: /25 ' Before Sorting' color 3.

WRITE : / 'TOTAL RECORDS FETCHED', sy-dbcnt.


ULINE.
LOOP AT it_t001 INTO wa_t001.
WRITE:/ sy-tabix,
wa_t001-bukrs,
wa_t001-butxt,
wa_t001-land1.
ENDLOOP.
ULINE.

wa_T001-BUKRS = 'TS22'.
wa_T001-BUTXT = 'Tata steel'.
wa_T001-LAND1 = 'India'.

Insert wa_t001 INTO it_t001 index 2.


Insert wa_t001 INTO it_t001 index 4.

sort it_t001 DESCENDING by bukrs.


write: /25 'After sorting' color 3.

LOOP AT it_t001 INTO wa_t001.


WRITE:/ sy-tabix,
wa_t001-bukrs,
wa_t001-butxt,
wa_t001-land1.
ENDLOOP.
ULINE.

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.

PARAMETERS p_cntry TYPE land1.


TYPES :BEGIN OF t001,
bukrs TYPE t001-bukrs,
butxt TYPE t001-butxt,
land1 TYPE t001-land1,
END OF t001.
DATA wa_t001 TYPE t001.
DATA it_t001 TYPE TABLE OF t001.
SELECT bukrs butxt land1 FROM t001
INTO TABLE it_t001
UP TO 5 ROWS
WHERE land1 = p_cntry.

WRITE : / 'TOTAL RECORDS FETCHED', sy-dbcnt.


ULINE.

wa_T001-BUKRS = 'TS22'.
wa_T001-BUTXT = 'Tata steel'.
wa_T001-LAND1 = 'India'.

Insert wa_t001 INTO it_t001 index 2.


Insert wa_t001 INTO it_t001 index 4.

sort it_t001 DESCENDING by bukrs.


write: /25 'Before Deleting' color 3.

LOOP AT it_t001 INTO wa_t001.


WRITE:/ sy-tabix,
wa_t001-bukrs,
wa_t001-butxt,
wa_t001-land1.
ENDLOOP.
ULINE.

delete ADJACENT DUPLICATES FROM it_t001 COMPARING bukrs.

write: /25 'After deleting' color 3.

LOOP AT it_t001 INTO wa_t001.


WRITE:/ sy-tabix,
wa_t001-bukrs,
wa_t001-butxt,
wa_t001-land1.
ENDLOOP.
ULINE.

Output:
5. Describing: To know the count of internal table records.
Syntax: Describe table <Itab> lines <integer variable>.

Source Code:
REPORT zinternal_table.

PARAMETERS p_cntry TYPE land1.


TYPES :BEGIN OF t001,
bukrs TYPE t001-bukrs,
butxt TYPE t001-butxt,
land1 TYPE t001-land1,
END OF t001.
DATA wa_t001 TYPE t001.
DATA it_t001 TYPE TABLE OF t001.
SELECT bukrs butxt land1 FROM t001
INTO TABLE it_t001
UP TO 5 ROWS
WHERE land1 = p_cntry.

WRITE : / 'TOTAL RECORDS FETCHED', sy-dbcnt.


ULine.

write: /25 'Before Describing' color 3.

LOOP AT it_t001 INTO wa_t001.


WRITE:/ sy-tabix,
wa_t001-bukrs,
wa_t001-butxt,
wa_t001-land1.
ENDLOOP.
ULINE.

write: /25 'After Describing' color 3.

LOOP AT it_t001 INTO wa_t001.


WRITE:/ sy-tabix,
wa_t001-bukrs,
wa_t001-butxt,
wa_t001-land1.
ENDLOOP.
ULINE.

data: num1 type i.


DESCRIBE TABLE it_t001 LINES num1.
write: / 'Total number of records:', num1.
Output:

You might also like