Thursday, March 27, 2008

Internal Tables in SAP ABAP

Internal Tables in SAP ABAP

Internal tables are an important part of any ABAP development. Internal tables are used every where. We will see how to Initialize an internal table. 

Basically we will see what the following statements do to an internal table.

CLEAR
REFRESH
FREE

Consider an Internal table with name ITAB. If you want to clear the header line of the Internal Table ITAB use the statement given below

CLEAR ITAB.

Many a times you want to just clear the header line of the internal table but want to keep the contents as they are. CLEAR ITAB will just clear the header line of the internal table.

CLEAR ITAB[]

The above statement will clear the contents of the internal table but the header line contents will not be cleared.

REFRESH ITAB

The above statement will also clear the contents of the internal table completely. If the table has a header line the contents of the header line will still remain. This is used when the internal table is without the header line.

FREE ITAB

Will deallocate the memory for the internal table. For better performance of ABAP programs you should free the memory if the internal table is not going to be used again in the program.

DELETE

You can use DELETE statement to delete a particular record from the internal table.

 
Examples

CLEAR ITAB.

 If the ITAB has 4 fileds and 4 Records as follows

 

VBELN            KUNNR             MATNR             WERKS

00010              7777                 A123                  1000

00020              7777                 B122                  1000

00030              7777                 C506                  1000

00040              7777                 D777                  1000

 

and the header line contents are as follows

 

Header Line           <00010 7777  A123  1000>

 

Note: CLEAR ITAB will clear only the contents of the header line. and the ITAB values will remain as shown below.

ITAB

 

VBELN           KUNNR           MATNR           WERKS

00010             7777               A123                1000

00020             7777               B122                1000

00030             7777               C506                1000

00040             7777               D777                1000

Header Line <                             >

 

CLEAR ITAB[]

Will clear the header line as well as the contents of ITAB

VBELN KUNNR MATNR WERKS

<>

Header Line

Header Line           <00010 7777  A123  1000>

REFRESH

Will clear the header line as well as the contents of ITAB

VBELN KUNNR MATNR WERKS

Header Line           <00010 7777  A123  1000>

Header Line

<>

FREE ITAB 

Will clear the contents of the ITAB as shown above for REFRESH and CLEAR in addidtion to that it also frees the memory allocation from SAP memory.

Header Line           <00010 7777  A123  1000>

DELETE

If you just want to delete a particular row then you need to use the DELETE statement.

Example

DELETE ITAB INDEX IDX.

SY-SUBRC = 0 The entry was deleted.

SY_SUBRC = 4 The entry does not exist.

Consider a case when your internal table has duplicate entries, in this situation you can SORT the internal table and delete the duplicate entries as follows

SORT ITAB

DELETE ADJACENT DUPLICATES FROM ITAB.

 COMPARING field1 field2

 COMPARING ALL FIELDS


Related Interview Questions

1) IF ITAB is an internal table with a header line declared in ABAP what is the difference between the following

CLEAR ITAB
CLEAR ITAB[]
REFRESH ITAB
FREE ITAB

Ans. CLEAR ITAB will clear the contents of the header line only.
        CLEAR ITAB[] will clear the contents of the table but the header line contents will remain as they are.
        REFRESH ITAB will clear the contents of the table but the header line contents will remain as they are.FREE ITAB will clear the
        contents  of the ITAB keeping the header line contents intact. This will also dealocate the internal table from the SAP memory.

2 How ill you delete adjacent duplicates from an internal table ITAB

Ans. First you need to SORT the internal table as shown below
        SORT ITAB.
Then you need to delete the duplicates as shown below
        DELETE ADJACENT DUPLICATES FROM ITAB

Also

DELETE ADJACENT DUPLICATES FROM ITAB COMPARING field1 field2

DELETE ADJACENT DUPLICATES FROM ITAB COMPARING ALL FIELDS