ABAP Insights: Demystifying Internal Table Operations - Sorting Tricks, Smart Data Summation, and Table Types Unveiled

 Welcome back everyone, So basically we were discussing about internal table operations in our last blog. So let’s continue it.


Internal Table Operations - SORT :-

  • It is to sort the internal table.
  • If we are not specifying anything, then by default it sorts in the ascending order.
  • If we want to sort in descending order, then we need to specify the keyword descending.

Practical implementation :-

*************************************************
*Start of Program
SORT lt_header BY order_number. "here we have not specified
"anything, then by default it
" will sort in ascending order.

*Printing data
LOOP AT lt_header INTO ls_header.
  WRITE :/ ls_header-order_number, ls_header-payment_mode.
ENDLOOP.

*End or Program
********************************************************


Output :-



Sorting in Descending Order :-

*************************************************
*Start of Program
SORT lt_header BY order_number DESCENDING.

*Printing data
LOOP AT lt_header INTO ls_header.
  WRITE :/ ls_header-order_number, ls_header-payment_mode.
ENDLOOP.

*End or Program
********************************************************


Output :-




Internal Table Operations - Collect :-

  • It is used to make sum of amount values based upon unique character values.

Suppose I have this sample data :-



  • Suppose, above is the total expenses of different department in some ABC company and I as owner want to calculate the entire expense for each department.

  • My result would be :-



  • So basically we are adding values based upon unique characters.

Practical Implementation of Collect :-

  • Create a executable program in ABAP Editor.



  • Create a type structure for above requirement.

    *****************************************
    *Start of Program
    *Creating a type Structure
    TYPES: BEGIN OF ty_collect,
             cname(3) TYPE c, "Company name
             dept(10) TYPE c,  "Department
             amount   TYPE zar_total_amount, "Amount
           END OF ty_collect.
    
    ****************************************
    


  • Create internal table and work area, also create another internal table where collected data

  • Enter the data into internal table.

  • Perform Collect statement.

  • Display the data.

Code :-

************************************************************
*Start of Program
*Creating a type Structure
TYPES: BEGIN OF ty_collect,
         cname(3) TYPE c, "Company name
         dept(10) TYPE c,  "Department
         amount   TYPE zar_total_amount, "Amount
       END OF ty_collect.

*Creating internal table and Work area
DATA : lt_collect TYPE TABLE OF ty_collect,
       ls_collect TYPE ty_collect.

*Create a different internal to store collected data
DATA : lt_collect_data TYPE TABLE OF ty_collect.

*Adding data into internal table
ls_collect-cname = 'ABC'.
ls_collect-dept = 'ADMIN'.
ls_collect-amount = '10000.00'.
APPEND ls_collect TO lt_collect.
CLEAR ls_collect.

ls_collect-cname = 'ABC'.
ls_collect-dept = 'HR'.
ls_collect-amount = '20000.00'.
APPEND ls_collect TO lt_collect.
CLEAR ls_collect.

ls_collect-cname = 'ABC'.
ls_collect-dept = 'ADMIN'.
ls_collect-amount = '50000.00'.
APPEND ls_collect TO lt_collect.
CLEAR ls_collect.

ls_collect-cname = 'ABC'.
ls_collect-dept = 'TRAINING'.
ls_collect-amount = '10000.00'.
APPEND ls_collect TO lt_collect.
CLEAR ls_collect.

ls_collect-cname = 'ABC'.
ls_collect-dept = 'HR'.
ls_collect-amount = '20000.00'.
APPEND ls_collect TO lt_collect.
CLEAR ls_collect.

*Applying Collect Statement.
LOOP AT lt_collect INTO ls_collect.
  COLLECT ls_collect INTO lt_collect_data.
  CLEAR ls_collect.
ENDLOOP.

*Display the data
LOOP AT lt_collect_data INTO ls_collect.
  WRITE :/ ls_collect-cname, ls_collect-dept, ls_collect-amount.
ENDLOOP.

*End of Program
*************************************************************


Output :-



  • You can see the output is same as it is in the above requirement.

Types of Internal Tables :-

1. Standard Internal Table

2. Sorted Internal Table

3. Hashed Internal Table

1. Standard Internal Table :-

  • They are the default internal tables.

  • They are the index based internal tables.

  • Records can be inserted or appended.

  • Data is not sorted by default, We can use SORT statement to sort the internal table.

  • We read a record using KEY or Index.

  • Either Linear Search or binary search is used to search a record.

  • Response time depends upon the number of entries in the internal table.

  • Append Inserts the record at the last of the internal table whereas

    • Insert inserts the record at anywhere in the internal table.

    Example :-

    ****************************************************
    *Start of Program
    *Creating a structure.
    TYPES : BEGIN OF lty_header,
              order_number TYPE zar_order_number,
              payment_mode TYPE zar_payment_mode,
            END OF lty_header.
    
    *Defining a standard internal table.
    DATA : lt_header TYPE table of lty_header.
    *OR
    DATA : lt_header1 type STANDARD TABLE OF lty_header.
    *Both are same
    *End of Program
    *******************************************************n**
    



2. Sorted Internal Table :-

  • They are the special type of internal tables in which data is automatically sorted. We need to specify the key while declaring the sorted internal table.
  • They are also the indexed based internal table.

Example :-

****************************************************
*Start of Program
*Creating a structure.
TYPES : BEGIN OF lty_header,
          order_number TYPE zar_order_number,
          payment_mode TYPE zar_payment_mode,
        END OF lty_header.

*Defining a sorted internal table.
*Declaring sorted internal table using unique key and non unique key
DATA : lt_header TYPE SORTED TABLE OF lty_header WITH UNIQUE KEY order_number.
DATA : lt_header1 TYPE SORTED TABLE OF lty_header WITH NON-UNIQUE KEY order_number.
*End of Program
*********************************************************


  • Records can be inserted. We will not use append statement in sorted internal table.

    • Let’s see why we will only use insert statement in sorted internal table and not the append statement.
    ****************************************************
    *Start of Program
    *Creating a structure.
    TYPES : BEGIN OF lty_header,
              order_number TYPE zar_order_number,
              payment_mode TYPE zar_payment_mode,
            END OF lty_header.
    
    *Defining a sorted internal table.
    DATA : lt_header TYPE SORTED TABLE OF lty_header WITH UNIQUE KEY order_number.
    DATA : ls_header type lty_header.
    
    *Using append statement.
    ls_header-order_number = '1'.
    ls_header-payment_mode = 'C'.
    append ls_header to lt_header.
    clear ls_header.
    
    ls_header-order_number = '2'.
    ls_header-payment_mode = 'C'.
    APPEND ls_header to lt_header.
    clear ls_header.
    
    ls_header-order_number = '3'.
    ls_header-payment_mode = 'C'.
    APPEND ls_header to lt_header.
    clear ls_header.
    
    *Displaying the data
    LOOP at lt_header into ls_header.
      write :/ ls_header-order_number, ls_header-payment_mode.
    ENDLOOP.
    *End of Program
    *********************************************************
    
    • In the above code, I have inserted three data.

    • When I will execute it, I will get a perfect fine output.



  • Now, Suppose I try to insert the unsorted data using append statement.

    *Using append statement.
    ls_header-order_number = '1'.
    ls_header-payment_mode = 'C'.
    APPEND ls_header TO lt_header.
    CLEAR ls_header.
    
    ls_header-order_number = '3'.
    ls_header-payment_mode = 'C'.
    APPEND ls_header TO lt_header.
    CLEAR ls_header.
    
    ls_header-order_number = '2'.
    ls_header-payment_mode = 'C'.
    APPEND ls_header TO lt_header.
    CLEAR ls_header.
    
  • As soon as I execute the above code, I will get a runtime error.



    • Since, We have already defined order number as unique in our sorted internal table therefore, we cannot append an unsorted data into it.

Now, we will use insert statement.

****************************************************
*Start of Program
*Creating a structure.
TYPES : BEGIN OF lty_header,
          order_number TYPE zar_order_number,
          payment_mode TYPE zar_payment_mode,
        END OF lty_header.

*Defining a sorted internal table.
DATA : lt_header TYPE SORTED TABLE OF lty_header WITH UNIQUE KEY order_number.
DATA : ls_header TYPE lty_header.

*Using insert statement.
ls_header-order_number = '1'.
ls_header-payment_mode = 'C'.
INSERT ls_header INTO TABLE lt_header.
CLEAR ls_header.

ls_header-order_number = '3'.
ls_header-payment_mode = 'C'.
INSERT ls_header INTO TABLE lt_header.
CLEAR ls_header.

ls_header-order_number = '2'.
ls_header-payment_mode = 'C'.
INSERT ls_header INTO TABLE lt_header.
CLEAR ls_header.

*Displaying the data
LOOP AT lt_header INTO ls_header.
  WRITE :/ ls_header-order_number, ls_header-payment_mode.
ENDLOOP.
*End of Program
*********************************************************
  • Here, we have used the insert statement in our program, Since insert statement is used to add the row at any place in our table, therefore Sorted table automatically added it to its fixed place and no runtime error was caused.

Output :-




  • Data already sorted, there is no need to perform sort statement on sorted internal table.
  • We read a record using key or index.
  • Since, data is already sorted we can use binary search to search a record.
  • Response time of a sorted internal table is very fast as compared to Standard Internal table.

So, that’s enough for today.

We will continue with hashed internal table in the next blog.

Thanx alot for being a part of this wonderful journey.

Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Unlocking SAP ABAP Secrets: Interactive Classical Reports with GET CURSOR, At User Command, and a Comparison of HIDE Statements

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming