≡ Menu

Performing Data Manipulations using Internal Table

Perform Data Manipulations using Internal Table

The following SAP training tutorials guides how to perform data manipulations on an internal table step by step. We can perform the following data manipulations on internal tables.

  1. Moving and assigning internal tables
  2. Clearing internal tables
  3. Refreshing internal tables
  4. Releasing memory of internal tables
  5. Comparing internal tables
  6. Performing the sort operation in internal tables
  7. Determining the attributes of internal tables

1. Moving and assigning internal tables

This makes use of the MOVE statement

MOVE <internal_tab1> TO <internal_tab2>.

Basically, here one internal table is assigned to another.

<internal_tab2> = <internal_tab1>.

—————————————–

REPORT ZINTERNAL_TABLE_DEMO.

*/Creating two internal tables-Tab1 and Tab2

DATA: BEGIN OF wa_itab1 OCCURS 0,

Name(10) TYPE c,

Salary TYPE I,

END OF line.

DATA: It-itab2 LIKE TABLE OF wa_Itab1,

It_itab3 LIKE TABLE OF wa_Itab1.

wa_Itab1-Name = ‘Shilpa’.

 

wa_Itab1-Salary = 10000.

APPEND wa_Itab1 TO It_Itab2.

*/Moving a line from Tab1 to Tab2

MOVE It_Itab2 [] TO It_Itab3[].

LOOP AT It_Itab3 INTO wa_Itab1

WRITE: wa_Itab1-Name, wa_Itab1-Salary.

ENDLOOP

 

2. Clearing or Initializing Internal Tables

CLEAR statement is used to initialize a table.

CLEAR <internal_tab>.

CLEAR <internal_tab>[].

 

3. Refreshing Internal Tables

The REFRESH statement is used to ensure that an internal table is initialized.

REFRESH <internal_tab>.

 

4. Releasing the Memory of Internal Tables

The FREE statement is used to initialize an internal table and release the memory allocated to it.

FREE <internal_tab>.

 

5. Comparing Internal Tables

Internal tables can be used as operands as well…..

<internal_tab1> <operator> <internal_tab2> …

Here, the <operator> expression stands for EQ, =, NE, <>, ><, GE, >=, LE, <=, GT, >, LT, and <.

 

6. Sorting an Internal Table

An Internal Table may be arranged in ascending or descending order by using the SORT statement.

SORT <internal_tab> [ASCENDING|DESCENDING] [AS TEXT]

[STABLE]

BY <internal_tab_field 1> [ASCENDING|DESCENDING

… <internal_tab_field n> [ASCENDING|DESCENDING] .

 

7. Determining the Attributes of Internal Tables

DESCRIBE TABLE specifies the attributes of an internal table which are not available statically at run-time. This may include information such as the initial size, current size, and table type.

DESCRIBE TABLE <internal_tab> [LINES <l>] [OCCURS <n>]

[KIND <k>].

LINES specifies the number of populated lines in the <l> expression, OCCURS specifies the value of the INITIAL SIZE clause of the internal table in the <n> expression, KIND clause specifies the table type of the internal table in the <k> expression

 

Data Manipulations programming example

Given below is an example of an ABAP program which data manipulates using an Internal Table.

REPORT ZITAB_OPS2.

 

TABLES : ZEMPDET.

 

DATA : IT_ITAB LIKE ZEMPDET OCCURS 0 WITH HEADER LINE,

WA_ITAB LIKE ZEMPDET.

 

START-OF-SELECTION.

 

SELECT * FROM ZEMPDET INTO TABLE IT_ITAB.

 

WA_ITAB-ZEMPNO = ‘110’.

WA_ITAB-ZEMPNAME = ‘EXTERNAL’.

WA_ITAB-ZEMPSAL = ‘11000’.

 

 

END-OF-SELECTION.

**

INSERT WA_ITAB INTO IT_ITAB INDEX 1.

 

LOOP AT IT_ITAB.

 

WRITE :/10 IT_ITAB-ZEMPNO,IT_ITAB-ZEMPNAME,IT_ITAB-ZEMPSAL,SY-TABIX,SY-INDEX.

 

ENDLOOP.

 

*******FETCHING A RECORD USING READ,INDEX,KEY AND LOOP

**RETRIEVE A SPECIFIC REC FROM THE LIST OF RECS

*READ TABLE IT_ITAB WITH KEY ZEMPNAME = ‘RICHARD’.

*READ TABLE IT_ITAB INDEX 5.

 

**RETRIVING THE REC USING LOOPS.

*SKIP 2.

*WRITE :/10 ‘RECS AFTER LOOP’.

*SKIP 2.

*LOOP AT IT_ITAB WHERE ZEMPNAME EQ ‘RICHARD’.

* WRITE :/10 IT_ITAB-ZEMPNO,IT_ITAB-ZEMPNAME,IT_ITAB-ZEMPSAL. “SY-TABIX.

*ENDLOOP.

*SKIP 3.

*WRITE :/10 IT_ITAB-ZEMPNO,IT_ITAB-ZEMPNAME,IT_ITAB-ZEMPSAL.

 

******* MODIFY A SPECIFIC RECORD USING READ

*READ TABLE IT_ITAB WITH KEY ZEMPNO = ‘104’.

*IT_ITAB-ZEMPSAL = 8000.

*

*MODIFY TABLE IT_ITAB TRANSPORTING ZEMPSAL.

 

******* MODIFY A SPECIFIC RECORD USING LOOP

 

*LOOP AT IT_ITAB WHERE ZEMPNAME = ‘RICHARD’.

*

* IT_ITAB-ZEMPSAL = 8000.

*

* MODIFY IT_ITAB TRANSPORTING ZEMPSAL.

*

* ENDLOOP.

 

**DELETION OF A SPECIFIC REC USING A READ OPERATION

*READ TABLE IT_ITAB WITH KEY ZEMPNO = ‘104’.

*DELETE TABLE IT_ITAB.

 

****DELETION OF A SPECIFIC REC USING A LOOP OPERATION

 

*LOOP AT IT_ITAB WHERE ZEMPNAME = ‘RICHARD’.

*

* DELETE TABLE IT_ITAB.

*

*ENDLOOP.

 

*READ TABLE IT_ITAB INTO WA_ITAB WITH KEY ZEMPNO = ‘104’.

*DELETE TABLE IT_ITAB FROM WA_ITAB.

*DELETE IT_ITAB FROM 1 TO 4.

 

* WRITE :/10 ‘RECS AFTER DELETION’.

* SKIP.

*

*LOOP AT IT_ITAB.

*

* WRITE :/10 IT_ITAB-ZEMPNO,IT_ITAB-ZEMPNAME,IT_ITAB-ZEMPSAL,SY-TABIX,SY-INDEX.

*

*ENDLOOP.

Read more for data manipulations using internal table interview questions with real time scenarios.