SAP ABAP-Internal Table Operations and Example Program


Internal Table Operations:
Below are the Operations of Internal Table
  1. Append
  2. Insert
  3. Delete
  4. Modify
  5. Describe
  6. Sort
  7. Refresh
  8. Clear
  9. Free
  10. Loop … Endloop
  11. Read Table

 
1.Append  - to append the workarea record at the last position of internal table.

we can append:

 a. single record

 b. multiple records

a. single record (one -> Many / Workarea-> Internal table)

Syntax:

  APPEND <WORKAREA NAME> TO <INTERNAL TABLE NAME>.

Example:

   wa_data-PATIENTID           = 9999.

   wa_data-PATIENTNAME   = 'Addition1'.

   wa_data-HOSPITALNAME = 'Alpha Hospital'. 

   APPEND wa_data to IT_DATA.

   CLEAR wa_data. 

 

b. Multiple records (Many->Many / Internal table1 to internal table2)

Syntax:

APPEND LINES OF <Internal table name1> to <Internal table name2>.

Note: While appending 1 internal table data with another internal table, the structures of the both internal tables should be same.

 

2.Insert - to insert the records into internal table in a specific location.

  a. single record

  b. Multiple records 

Syntax to insert a single record:

INSERT <WORKAREA> INTO <INTERNAL TABLE> INDEX <Index Number>.

Example:

wa_data-PATIENTID           = 8888.

wa_data-PATIENTNAME    = 'Insert1'.

wa_data-HOSPITALNAME = 'Sample Hospital'.

INSERT WA_DATA INTO IT_DATA INDEX 4.

CLEAR wa_data.

 
Syntax to insert multiple records:

INSERT LINES OF <INTERNAL TABLE1> FROM <from Index number> TO <index5>

           INTO <INTERNAL TABLE2> INDEX <INDEX NO>.

3.DELETE

To delete the records of Internal Table.

We can delete records by using below syntax:

 1. Delete the record from a location

Syntax:    

Delete <INTERNAL TABLE NAME> INDEX 2.

 

2.Delete the range of records from internal table

Syntax:

Delete <INTERNAL TABLE NAME> from <N1> TO <N2>.

 

3.Delete the range of records from a record

Syntax:

Delete <INTERNAL TABLE NAME> from <INDEX NO>.

4.Delete internal table data based on a condition

Syntax:

DELETE <INTERNAL TABLE NAME> WHERE <CONDITION>.

 

4.MODIFY

Used to Overwrite the record values of an internal

we can modify single record and multiple records of an internal table.

To modify we need to follow as mentioned below:

Fill the work area fields with new value.

Modify Single field of a record:

Syntax:

MODIFY <Internal table> from <workarea>

                                 transporting <Fieldname> 

                                             where <condition>.

Modify Multiple fields of a record:

MODIFY <Internal table> from <workarea>

                                transporting <Fieldname1> 

                                                       <Fieldname2>   

                                           where <condition>.

For Example:

Data available in the internal table record like below:



0000001007

Krishna

Medwin Hosp

 

Requirement is, modify 'Medwin Hosp' as 'Test Hospital'.

After Modify, Expected Output is as below:



0000001007

Krishna

Test Hospital

 

 5.DESCRIBE

  -  to know the count of internal table records.

   Syntax:

    DESCRIBE table <internal table name> lines <integer variable>.

   Example:

    DESCRIBE TABLE it_data LINES L_COUNT.   

6.SORT

  Arrange the records of internal table in an order(Ascending/Descending).

 Note:

  By default, the data in the table is in 'Ascending order'.

Syntax:

 SORT <Internal Table name> ascending/descending by <field1> <field2> ...etc.

7.REFRESH

  to clear the contents/records of internal table.

Syntax:

  REFRESH <Internal Table name>. 

Example:

REFRESH it_data1. " to delete the contents of internal table

8.CLEAR

  to clear the contents/records of variable/workarea/internal table.

Syntax:

CLEAR <VARIABLE/workarea/internal table>.

9.FREE

  to free up the memory occupied contents/records of variable/workarea/internal table.

Syntax:

  FREE <VARIABLE/workarea/internal table>.

 
10.LOOP...ENDLOOP

 to print/process the multiple records sequentially.

 Multiple ways of using loop:

1. Loop without condition:

 Syntax:

  loop at <internal table> into <work area>.

   ----

    ---

  endloop.

2.Process the records based on condition:

Syntax:

  loop at <internal table> into <work area>

                                          where <field> = <Value>.

   ----

    ---

  endloop.

3.Process the range of records:

Syntax:

  loop at <internal table> into <from Index number>

                                                 to <To Index number>.

    ---

    ---

  endloop.

11. READ TABLE (To Process a single record)

Reading a single record from Internal table to workarea.

1.Read the data from a location/Index:

Syntax:

READ TABLE <INTERNAL TABLE NAME> INTO <WORKAREA>

                                                                      INDEX <INDEX NUMBER>.

 

2.Read the data from internal table into workarea based on a Key:

Syntax:

READ TABLE <INTERNAL TABLE NAME> INTO <WORKAREA>

                                                               WITH KEY <CONDITION>

                                                                 BINARY SEARCH.

Notes regarding Binary search:

We need to add BINARY SEARCH along with READ statement.

Reason: Binary search is used to read the record very quickly from internal table.

Functionality of Binary search:

it divides the whole internal table into equal parts to search for a record.

Pre-requisite for Binary search:

do the SORT for internal table which we are using to READ the record.

 
Example Program:
For our example, we will use data from below table.




ABAP Program to demonstrate Internal Table Operations:
REPORT ZINTERNAL_TABLES_EX2.
*& For strcucture creation
*& do this steps : PATTERN-> STRUCTURED DATA OBJECT-> WITH FIELDS FROM STRUCTURE

*&-------------------------------------------------
*& Structure
*&-------------------------------------------------
TYPES: BEGIN OF TY_DATA,
         PATIENTID    TYPE ZPATIENT_DATA-PATIENTID,
         PATIENTNAME  TYPE ZPATIENT_DATA-PATIENTNAME,
         HOSPITALNAME TYPE ZPATIENT_DATA-HOSPITALNAME,
       END OF TY_DATA.
*&--------------------------------------------------
*& Internal table Declaration
*&--------------------------------------------------
DATA: IT_DATA  TYPE TABLE OF TY_DATA,
      IT_DATA1 TYPE TABLE OF TY_DATA.
*&--------------------------------------------------
*& Work area
*&--------------------------------------------------
DATA WA_DATA TYPE TY_DATA.
*&--------------------------------------------------
*& Variable declaration
*&--------------------------------------------------
DATA L_COUNT TYPE I.
*&--------------------------------------------------
*&Business Logic
*&Select the records from table 'ZPATIENT_DATA' into internal tables based on condition.
*&--------------------------------------------------
SELECT *
  FROM ZPATIENT_DATA
INTO TABLE IT_DATA
     WHERE PATIENTNAME = 'Krishna'.  "  1
SELECT *
  FROM ZPATIENT_DATA
INTO TABLE IT_DATA1
     WHERE PATIENTNAME = 'Patient1'. " 2
*&------------------------------------------------------------------
*& Performing Internal table Operations on the Above Internal tables data
*&------------------------------------------------------------------
*& Note :Uncomment the below code using CTRL + > to use


*& How to use REFRESH (Refresh is used to delete the contents of internal table)
*& Note:We can clear the contents of internal table using CLEAR also
*REFRESH IT_DATA1. " to delete the contents of internal table
*& Clear - to clear Variables/Workareas/Internal tables
*CLEAR it_data1. " to clear the contents of internal table
*&-----------------------------------------------
*& Appending Single record to internal table
*& 1. Assign the values to work area fields
*& 2. Append the WORKAREA to INTERNAL TABLE
*& 3. clear WORKAREA, Once Append statement completed.
*&-----------------------------------------------
WA_DATA-PATIENTID    = 9999.
WA_DATA-PATIENTNAME  = 'Addition1'.
WA_DATA-HOSPITALNAME = 'Alpha Hospital'.
APPEND WA_DATA TO IT_DATA.
CLEAR WA_DATA.


*&-----------------------------------------------
*&Merging the 2 internal tables data into one internal table
*& Appending multiple records of internal table IT_DATA1 TO IT_DATA
*&-----------------------------------------------


   APPEND LINES OF  IT_DATA1 TO IT_DATA. " Merging the data


*&-----------------------------------------------------------------
*& INSERT the lines of one internal table to another internal table
*&-----------------------------------------------------------------
*& Note :Uncomment the below code using CTRL + > to use
*INSERT LINES OF it_data1
*            FROM 1 TO 2
*           INTO it_data  INDEX 1.
*&---------------------------------------------------------------
*& INSERT a Single record in a given location of Internal table
*&---------------------------------------------------------------
*wa_data-PATIENTID    = 8888.
*wa_data-PATIENTNAME  = 'Insert1'.
*wa_data-HOSPITALNAME = 'Sample Hospital'.
*
*INSERT WA_DATA INTO IT_DATA INDEX 2.       " Inserting this record in 2nd line
*CLEAR wa_data.


*&--------------------------------------
*& Modify - Overwrite the Values of internal table
*&--------------------------------------


*& Note :Uncomment the below code using CTRL + > to use


*Modify-Data for Single field
*--------------------------------
*wa_data-PATIENTNAME  = 'Mr.Krishna'.
*
*MODIFY it_data from wa_data
*       transporting PATIENTNAME
*             where PATIENTID = 1007.



*Modify-Data for multiple fields
*--------------------------------------
*wa_data-PATIENTNAME  = 'Mr.Krishna'.
*wa_data-HOSPITALNAME = 'Test Hospital'.
*MODIFY it_data from wa_data
*       transporting PATIENTNAME
*                    HOSPITALNAME
*             where PATIENTNAME = 'Patient1'.
*& DELETE - To delete the record/records from internal table
*&---------------------------------------
*&1. Delete - Delete the record from a respective location
*&----------------------------------------
*& Note :Uncomment the below code using CTRL + > to use

*& Logic:
*delete it_data INDEX 2.
*2.Delete the range of records from internal table
*---------------------------------
*& Logic:
*Delete it_data from 1 TO 2.
*3.Delete the range of records from a record
*-----------------------------------------
*& Logic:
*Delete it_data from 2.


*4.Delete internal table data based on a condition
*-----------------------------------------
*& Logic:
*DELETE it_data WHERE PATIENTID = 1007.


*&-----------------------------------------
*& SORT - To print/display the data in an Order (Ascending/Descending)


*& Note :Uncomment the below code using CTRL + > to use

*& Logic:
*SORT IT_DATA BY PATIENTID DESCENDING.  " To print the data in descending order
SORT IT_DATA BY PATIENTID ASCENDING.  " To print the data in descending order
*&-----------------------------------------
*&-----------------------------------------
*& Describe- To know the count of internal table records
*&-----------------------------------------
DESCRIBE TABLE IT_DATA LINES L_COUNT.
WRITE: / 'The no of records are', L_COUNT COLOR 3.
*& To print Horizontal line/Underline in the output
ULINE.


*& To skip the no of lines between the rows/lines during output display
SKIP 1.

*&------------------
*& Processing Logic
*&------------------
*& Processing Multiple records from Internal Table into Work area Using (Loop--Endloop)
*& Processing Single record from Internal Table into Work area Using (Read Table)
*&---------------------------------------------------------------------------
*& Process the data available in IT_DATA
*& Note: Multiple data processing from Internal table can be done only by using Loop----Endloop.
*&---------------------------------------------------------------------------
*& To Apply the color we need to use below syntax:
  
  Format COLOR 1 ON.


*& Output Header display
WRITE:/         'Patient ID',
            15 '|', 'Patient Name',  
            40 '|', 'Hospital Name'.
*&Logic
LOOP AT IT_DATA INTO WA_DATA.
  WRITE:/     WA_DATA-PATIENTID,
           15 '|' WA_DATA-PATIENTNAME,
           40 '|' WA_DATA-HOSPITALNAME.
  CLEAR WA_DATA.
ENDLOOP.
*&-----------------------------
*& Read table base on Index
*&-----------------------------
*& Note :Uncomment the below code using CTRL + > to use
*&Logic
*READ TABLE IT_DATA INTO WA_DATA INDEX 2.
*  WRITE:/  WA_DATA-PATIENTID,
*           WA_DATA-PATIENTNAME,
*           WA_DATA-HOSPITALNAME.
*  CLEAR WA_DATA.

*&-----------------------------
*& Read table base on Condition
*&-----------------------------
*& Note :Uncomment the below code using CTRL + > to use
*&Logic
*READ TABLE IT_DATA INTO WA_DATA with KEY PATIENTID = 1007.
*if sy-subrc = 0.
*  WRITE:/  WA_DATA-PATIENTID,
*           WA_DATA-PATIENTNAME,
*           WA_DATA-HOSPITALNAME.
*  CLEAR WA_DATA.
*ENDIF.

 

 

 


 

Comments

Popular posts from this blog

HR ABAP Interview Questions and Answers

BDC (Batch Data Communication) - SAP ABAP