You are on page 1of 12

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

1. This statement is used to append a single record from work area to Internal Table.
2. The record is always added at the bottom.

Syntax: APPEND <WA> TO <ITAB>


DATA: IT_MARA TYPE TABLE OF MARA.
DATA: WA_MARA TYPE MARA.
WA_MARAMATNR = '00001'.
WA_MARAMTART = 'FERT'.
WA_MARAMEINS = 'EA'.
APPEND WA_MARA TO IT_MARA.
"APPNED WORK AREA TO INTERNAL TABLE.
Using INSERT in SAP ABAP

This statement is used to INSERT a single record from work area to Internal Table at a specified
location.

Syntax: INSERT <WA> INTO <ITAB> INDEX <index>

DATA: IT_MARA TYPE TABLE OF MARA.


DATA: WA_MARA TYPE MARA.
WA_MARAMATNR = '00001'.
WA_MARAMTART = 'FERT'.
WA_MARAMEINS = '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 an internal table data in ascending order or descending order, by default it will
sort data in ascending order. By default it sorts based on field1, field2, field3...etc.
.
Syntax1: SORT <ITAB>. "Default sorts data in ascending order.
Syntax2: SORT <ITAB> DESCENDING. Sort in descending order.
Syntax3: SORT <ITAB> BY <FIELD1> <FIELD2>... ASCENDING/DESCENDING

."It sorts data by specified fields <FIELD1>, <FIELD2>...

Using DESCRIBE TABLE in SAP ABAP

DESCRIBE TABLE is used to count the no of records in an internal table [OR]

This statement is used to find the total number of records in an Internal Table.

Syntax:

DESCRIBE TABLE <ITAB> LINES <v_lines>." Count the no. of record of an


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, for full
details Binary Search mechanism in SAP ABAP /recourse/binary search.html. The internal table
must be sorted in ascending order before using binary search.

Read Table:

This statement is used to read a record from Internal Table into work area specified by either index
Number or key.

Syntax:

READ TABLE <ITAB> INTO <WA> WITH KEY <FIELD1> = <FIELD1 VALUE>
<FIELD1> =
<FIELD1 VALUE>
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 <ITAB> INTO <WA> INDEX <index_no>.


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

Read table with index:

Read table <ITAB> into <wa> index <index number>.

Read table with key:

Read table <ITAB> into <wa> with key <fname1> = <fval>


<fname2> = <fval> .
.
.
Binary Search .
Example:

Read table with index:

Read table i_kna1 into wa_kna1 index 5.

WRITE: / wa_kna1-kunnr, wa_kna1-name1, wa_kna1-land1, wa_kna1-ort01.

Save->Act->test

Read table with KEY:

Read table i_kna1 into wa_kna1 with key kunnr = 0000001011 Binary search.

WRITE: / wa_kna1-kunnr, wa_kna1-name1, wa_kna1-land1, wa_kna1-ort01.

Save->Act->test
Using LOOP....ENDLOOP. In SAP ABAP
Loop...End loop. Is also used to read data from an internal table into work area, this is used to
read multiple records serially one after one.

Syntax1: LOOP AT <ITAB> INTO <WA>.

ENDLOOP.

Syantax2: LOOP AT <ITAB> INTO <WA> WHERE <FIELDS1> =<VALUE>.

ENDLOOP.

Syntax3: LOOP AT <ITAB> INTO <WA> FROM <INDEX1> TO <INDEX2>.

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
instead of all fields. [OR]

This statement is used to MODIFY the Internal Table records based on Condition [OR]
From work area.
Syntax 1: MODIFY <ITAB> FROM <WA> INDEX <INDEX NO> TRANSPORTING
<FIELD1> <FIELD2>

Syntax 2: MODIFY <ITAB> FROM <WA> TRANSPORTING <FIELD1>


<FIELD2> WHERE <CONDITION>

Syntax 3: Modify <ITAB> from <wa> index sy-tabix transporting F1 F2.etc.

SYTABIX is a key word which stores the index no of currently processed record. For full
Details read using sy tabix in sap abap programs /resourses/sytabis.html.

SY-TABIX (System ITAB Index):

It is a system variable which stores the index number of the internal table records which is currently
being processed in work area.

TRANSPORTING: It specifies the list of the fields to be modified from work area to internal table.
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_MARAMTART = 'FERT';
"ASSIGN A VALUE TO WORKAREA TO MODIFY INTERNAL TABLE.
MODIFY IT_MARA FROM WA_MARA INDEX SYTABIX 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_MARAMTART = '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 <ITAB> INDEX <INDEX NO>.


Syntax2: DELETE <ITAB> WHERE <FIELD1> = <FIELD1 VALUE>
<FIELD2> = <FIELD2 VALUE>.
DELETE:

This statement is used to DELETE a single record or multiple records based on Condition.

SYNTAX: Delete <ITAB> index <N>.


Delete <ITAB> where <condition>.

Ex on Delete:

Ex1: (Deleting single record)

Delete i_kna1 index 5.


Loop . Endloop.
Save->Act->test

Ex2: (Deleting single record)

Delete i_kna1 where kunnr = 0000001011.


Loop. Endloop.
Save->Act->test

Ex3: (Deleting multiple records)

Delete i_kna1 where land1 = USA


Loop. Endloop
Save->Act->test

Using DELETE ADJACENT DUPLICATES in SAP ABAP

DELETE ADJACENT DUPLICATES is used to delete duplicate records which are adjacent to
eachother. Pre requisite for this is the internal table must be sorted in ascending order

Syntax1: DELETE ADJACENT DUPLICATED FROM <ITAB>.ADJACENT DUPLICATED WILL


BE DELETED IN INTERNAL TABLE COMPARING ALL FIELDS
Syntax2: DELETE ADJACENT DUPLICATES FROM <ITAB> COMPARING <FIELD1> <FIELD2>.
"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 an internal table.
FREE is used to clear free memory of an internal table or work area.
We all know when Ever we declare an internal table or work area 8kb memory will be allocated.

Syntax clear: CLEAR <WA> "CLEAR WORK AREA OR VARIABLE

Syntax REFRESH: REFRESH <ITAB>


"CLEAR ALL RECORDS OF INTERNAL TABLE BUT MEMORY WILL BE THERE

Syntax FREE: FREE <WA> 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 <ITAB1> FROM <index no> TO <index no2> TO <ITAB2>.

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 <ITAB1> FROM <index no> TO <index no2>


INTO <ITAB2> INDEX <Index no>.
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.

Syntax: <ITAB2 []> = <ITAB1 []>. "Move ITAB1 to ITAB2.

Using COLLECT in SAP ABAP

COLLECT is similar to APPEND, the difference is it COLLECT will check whether the work
area record already exists with the same key only C, D, N, T, if exists it will add numerical fields
sum to the existing record, if work area record doesnt exists it will append a new record .

Syntax: COLLECT <WA> INTO <ITAB>.

COLLECT:

This statement checks whether the WA record already exists in the internal table with the
same key.
If YES, it will just add the numerical fields.
If NO, A new record is appended.

SYNTAX: Collect <wa> into <ITAB>.

NOTE: KEY means character fields: C,N,D,T.


Numerical fields: I,P,F.
DELETE ADJACENT DUPLICATES:

1. This statement is used to DELETE the Duplicate records which are adjacent or side by side to
each other.
2. The prerequisite for this statement is, the ITAB should be sorted in ascending order.

SYNTAX:

Delete adjacent duplicates from <ITAB> comparing F1 F2 F3


Delete adjacent duplicates from <ITAB> comparing all fields.

CLEAR:

1. This statement is used to CLEAR or Delete the data from work area.

2. In older version this clear statement was used to delete the data from ITAB also.

SYNTAX:
Clear <wa>.
Clear <ITAB>[ ]---- IN older version.

REFRESH:

1. This statement is used to DELETE the ITAB records.

SYNTAX: Refresh <ITAB>.

FREE:

1. This statement is used to DELETE the Data from work area and ITAB.

2. It is same as clear and refresh.

3. The difference is, the free statement delete the data as well as memory Occupied by ITAB or
work area, where as clear and refresh will delete only the data not the memory.

SYNTAX: Free <wa/ITAB>.


APPEND LINES OF:

This statement is used to APPEND multiple records one internal table to another internal table.

SYNTAX:
Append lines of ITAB1 to ITAB2.
Append lines of ITAB1 from 3 to 7 to ITAB2.

INSERT LINES OF:

This statement is used to INSERT multiple records one internal table to another internal table at a
specified location.

SYNTAX:
Insert lines of ITAB1 to ITAB2 index <N>.
Insert lines of ITAB1 from 3 to 7 to ITAB2.

MOVE ITAB1 TO ITAB2:

This statement is used to move entire internal table records from one ITAB to another.

SYNTAX: Move ITAB1 to ITAB2.