You are currently viewing Internal Table Operations in ABAP

Internal Table Operations in ABAP

For a ABAP developer, when he/she used Internal Tables in his/her application during development using of Internal table operations in the coding part is most important. Following 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

 

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 <WA> TO <ITAB>
DATA : IT_BILL TYPE ZBILL OCCURS 0.
DATA : WA_BILL TYPE ZBILL.
WA_BILL- BILLNO =  'BIL6344'.
WA_ BILL -AMOUNT = '4000.00'.
WA_ BILL -PAYMOD = 'CASH'.
APPEND WA_ BILL TO IT_BILL . "APPNED WORK AREA TO INTERNAL TABLE

 

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 <WA> INTO <ITAB> INDEX <index><index></index>
DATA : IT_BILL TYPE ZBILL OCCURS 0.
DATA : WA_BILL TYPE ZBILL.
WA_BILL- BILNO =  'BIL6344'.
WA_ BILL-AMOUNT = '4000.00'.
WA_ BILL-PAYMOD = 'CASH'.
INSERT WA_ BILL INTO IT_BILL INDEX 2 . "The record will be inserted into internal table at 2nd position

 

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 <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>
DATA : IT_BILL TYPE ZBILL OCCURS 0.
DATA : WA_BILL TYPE ZBILL.
SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_BILL FROM ZBILL.
SORT IT_BILL BY BILNO.

 

DESCRIBE TABLE in SAP ABAP
DESCRIBE TABLE is used to count the no of records in a internal table

Syntax: DESCRIBE TABLE <ITAB> LINES <v_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_BILL LINES V_LINES ." Count the no. of record of a internal table
Write : v_lines .

 

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 fast, the functionality of binary search it divides the into parts and searches, for full details Binary Search mechanism in SAP ABAP. The internal table must be sorted in ascending order before using binary search.

Syntax: 
READ TABLE <ITAB> INTO <WA> WITH KEY <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 MATNR is '0001'

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_BILL INTO WA_BILL INDEX '2' . "Read a record into work area where
index is 2.

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

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 of fields to be modified instead of all fields.

Syntax1: MODIFY <ITAB> FROM <WA> INDEX <INDEX NO> TRANSPORTING <FIELD1> <FIELD2> .
Syntax2: MODIFY <ITAB> FROM <WA> TRANSPORTING <FIELD1>
<FIELD2> WHERE <CONDITION>.

SY-TABIX is a key word which stores the index no of currently processed record.

DATA : IT_BILL TYPE ZBILL OCCURS 0.
DATA : WA_BILL TYPE ZBILL.
SELECT * FROM ZBILL INTO CORRESPONDING FIELDS OF TABLE IT_BILL . " GET DATA INTO ITAB IT_BILL
WA_BILL-PAYMOD = 'CASH'. 
"ASSIGN A VALUE TO WORKAREA TO MODIFY INTERNAL TABLE
MODIFY IT_BILL FROM WA_BILL INDEX SY-TABIX TRANSPORTING PAYMOD. 
" NOW THE VALUE OF FIELD PAYMOD WILL BE MODIFIED FOR CURRENT RECORD IN IT_BILL 

DATA : IT_BILL TYPE ZBILL OCCURS 0.
DATA : WA_BILL TYPE ZBILL.
SELECT * FROM ZBILL INTO CORRESPONDING FIELDS OF TABLE IT_BILL. " GET DATA INTO ITAB IT_BILL

WA_BILL-AMOUNT = '5000.00'; "ASSIGN A VALUE TO WORKAREA TO MODIFY INTERNAL TABLE

MODIFY IT_BILL FROM WA_BILL TRANSPORTING AMOUNT WHERE BILNO = '6344'. " NOW THE VALUE OF FIELD AMOUNT WILL BE MODIFIED WHERE BILNO = '6344'  IN ITAB 

 

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 IT_BILL INDEX 3. "3rd RECORD WILL BE DELETED IN IT_BILL
DELETE IT_BILL WHERE PAYMOD = 'CASH'. 
"PAYMENT MODE WITH PAYMOD = ‘CASH’ WILL BE DELETED

 

Using DELETE ADJACENT DUPLICATES in SAP ABAP
DELETE ADJACENT DUPLICATES is used to delete delete duplicate records which are adjacent to each-other.
Note:- 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_BILL ASCENDING.
DELETE ADJACENT DUPLICATES FROM IT_BILL . "3rd RECORD WILL BE DELETED IN IT_BILL

SORT IT_BILL ASCENDING.
DELETE ADJACENT DUPLICATES IT_BILL COMPARING PAYMOD. "DUPLICATES WILL BE DELETED BY COMPARING PAYMOD

CLEAR, REFRESH, FREE in SAP ABAP
CLEAR will clear a value in a work area or in a variable or can clear whole work area.
REFRESH will clear all values in an internal table.
FREE is used to clear (free) memory of an internal table or work area. When ever we declare any 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_BILL.

REFRESH IT_BILL.

FREE IT_BILL.

 

APPEND LINES OF in SAP ABAP
APPEND LINES OF will 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_BILL TYPE ZBILL OCCURS 0. "FIRST INTERNAL TABLE
DATA : IT_BILL1 TYPE ZBILL OCCURS 0. "SECOND INTERNAL TABLE

APPEND LINES OF IT_BILL FROM 2 TO 4 TO IT_BILL1. "DATA IN IT_BILL WILL BE APPENDED TO IT_BILL1 FROM INDEX 2 TO INDEX 4.

INSERT LINES OF in SAP ABAP
INSERT LINES OF will 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_BILL TYPE ZBILL OCCURS 0. "FIRST INTERNAL TABLE
DATA : IT_BILL1 TYPE ZBILL OCCURS 0. "SECOND INTERNAL TABLE
INSERT LINES OF IT_BILL FROM 7 TO10 INTO IT_BILL1 INDEX 7. 
"DATA IN IT_MARA WILL BE INSERTED INTO IT_BILL1 FROM INDEX 7 TO INDEX 10 AT INDEX 7 LOCATION.

 

MOVE in SAP ABAP
MOVE keyword will move data of one internal table to another.

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

COLLECT in SAP ABAP
COLLECT is slimier 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 does not exists it will append a new record.

Syntax: COLLECT <WA> INTO <ITAB>.

 

 

Dinesh Kumar Bansal

Dinesh Kumar Bansal is an Indian Software Developer, who is working on ASP.Net, MS-SQL, SAP-ABAP technologies from last one year. His Basic Principle of developing software is, “You have to clear about, what do you want to do, how can it be done”. He always says that development can be done with a cool & fresh mind.

Leave a Reply