Saturday, May 31, 2025

πŸ“„ Types of Internal Tables

 There are three types of Internal Table in ABAP Programming :- '

 1. Standard Internal Table

 2. Sorted Internal Table

 3. Hashed Internal Table

 

1. Standard Internal Table 
1. They are the default internal tables.

TYPES : BEGIN OF ty_employee, 

               emp_id TYPE zar_emp_id,

               emp_name TYPE zar_emp_name,

               END OF ty_employee.

DATA : lt_employee TYPE TABLE OF ty_employee,

             ls_employee TYPE ty_employee.

 OR

          DATA : lt_employee type standard table of ty_employee. 

2. They are the index based internal tables ( i.e. in the debugging we can check each and every record has a index ).

3. Records can be inserted or appended.
Append :- Append statement inserts the record at the last of the internal table.
Insert :- Insert statement inserts the records anywhere in the internal table. 

4. Data is not sorted by default, We can use SORT statement to sort the internal table.
                        SORT lt_employee BY emp_id.

5. For standard internal table we can read a record based on key and based on index too.

Based on key

READ TABLE lt_employee INTO ls_employee WITH KEY emp_id = '10

IF sy-subrc EQ 0.

WRITE :/ ls_employee-emp_id, ls_employee-emp_name.

ENDIF.

Based on index

READ TABLE lt_employee INTO ls_employee index 1.

 IF sy-subrc EQ 0.

WRITE :/ ls_employee-emp_id, ls_employee-emp_name.

 ENDIF. 

6. For standard internal tables either we can use the linear search ( sequential search ) or we can use the binary search to search a record. 
Note :- You can use binary search if your internal table has been sorted.

READ TABLE lt_employee INTO ls_employee with key emp_id = '010' Binary Search.

IF sy-subrc EQ 0.

 WRITE :/ ls_employee-emp_id, ls_employee-emp_name. 

ENDIF.

Or else if it is not sorted you can go for linear search.

READ TABLE lt_employee INTO ls_employee with key emp_id = '010'.

IF sy-subrc EQ 0.

 WRITE :/ ls_employee-emp_id, ls_employee-emp_name.

ENDIF.

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

2. Sorted Internal Table

1. Sorted internal tables are a type of internal tables in which data is automatically sorted.
We need to specify the key while declaring the sorted internal table. 

TYPES : BEGIN OF ty_employee,

              emp_id TYPE zar_emp_id,

              emp_name TYPE zar_emp_name,

              END OF ty_employee.

DATA : lt_employee TYPE SORTED TABLE OF ty_employee WITH UNIQ

             ls_employee TYPE ty_employee.

2. They are also indexed based internal table, i.e. data will be stored indexed wise.

3. We should not used append statement for sorted internal table and we should only go for insert statement.  
                   ls_employee-emp_id = '101'.
                   ls_employee-emp_name = 'Yuvraj Singh'.
                   insert ls_employee into table lt_employee.
                   CLEAR ls_employee.

                   ls_employee-emp_id = '102'.
                   ls_employee-emp_name = 'Virat Kohli'.
                   insert ls_employee into table lt_employee.
                   CLEAR ls_employee.

                   ls_employee-emp_id = '104'.
                   ls_employee-emp_name = 'K.L Rahul'.
                   insert ls_employee into table lt_employee.
                   CLEAR ls_employee.

                   ls_employee-emp_id = '103'.
                   ls_employee-emp_name = 'Rohit Sharma'.
                   insert ls_employee into table lt_employee.
                   CLEAR ls_employee.

Q. Why we should not use append statement in sorted internal table ?

Suppose, first three records that we have stored was employee id 101, 102 and 104 and then the fourth record we want to store is 103. So, if you will try to store this as a fourth record and our internal table is sorted by employee so logically 103 should be stored as third row, therefore in this situation it will generate a runtime error. And if we will use the insert operation, it will automatically add the record at its suitable position. 

4. Data is already sorted, there is no need to use SORT statement. 

5. We read a record using key or index. 

6. Since, data is already sorted we can use binary search to search a record. 

7. Response time of a sorted internal table is very fast as compared to standard internal table.  


3. Hashed Internal Table
1. Hashed internal tables are the special type of internal table which works on HASH algorithms. 

TYPES : BEGIN OF ty_employee, 

               emp_id TYPE zar_emp_id, 

              emp_name TYPE zar_emp_name, 

              END OF ty_employee.

 DATA : lt_employee TYPE HASHED TABLE OF ty_employee WITH UNIQ

              ls_employee TYPE ty_employee.


Hashing :-
Hashing is a technique which returns the address of the record based upon the search key without index. Example: if we will go for order number 10, hashing will directly go for address of order number 10. 

2. Hashed internal tables do not support the concept of index i.e. why they are not the index based internal table. 

3. We use the insert operation, append statement is not supported here, if you will use append statement, it will give a compile time error. 

4. There is no impact of SORT, as we read the record based upon the hashed algorithm.

5. We Read a record using key, index is not applicable.

READ TABLE lt_employee INTO ls_employee index 1.

IF sy-subrc EQ 0.

 WRITE :/ ls_employee-emp_id, ls_employee-emp_name.

ENDIF.


The above line will give syntax error 

6. Hashed algorithm is used to search a record.

7. Response time is faster as compared to standard and sorted internal table, as response time is independent of number of entries. It is well suited for tables, where a table has huge number of records and we want to search based upon unique key.


4. Comparing Standard vs Sorted vs Hashed Internal Tables

1. Index :- 

  • Standard Internal tables are the index based internal tables. 
  • Sorted Internal tables are also the index based internal tables. 
  • Hashed internal tables are not the index based internal tables.

2. Insertion Of Records:-

  • Both append and insert operations can be used to insert the records to standard internal tables. 
  • Only insert operation can be used to insert the records to sorted internal tables. 
  • Only insert operation can be used to insert the records to hashed internal tables.

3. Sorting :- 

  • Data is not sorted by default in standard internal table.SORT operation is used sort the data to standard internal table. 
  • Data is sorted by default. There is no need for SORT operation. 
  • SORT does not have any impact on the performance of HASHED internal table.

4. Read :- 

  • The record can be read from standard internal table using KEY or Index. 
  • They record can be read from sorted internal table using KEY or Index. 
  • The record can be read from hashed internal table using KEY only. 

5. Search :-

  • Linear or Binary Search 
  • Binary Search 
  • Hashed algorithm

6. Response Time :- 

  • Response time of standard internal table is less as compared to sorted and hashed internal tables as it uses linear search by default. The response time of standard internal tables depends upon the number of entries. The response time of standard internal table can be improved by using binary search.
  •  Response time of sorted internal table is fast as compared to standard internal table as it uses binary search.
  •  Response time of hashed internal table is fast as compared to standard and sorted internal tables. The response time of hashed internal tables does not depend upon the number of Types of Internal Tables in ABAP Programming 10 entries. It is well suited for tables, where a table has huge number of records and we want to search based upon unique key. 

Some Important Questions:
1. Which of the internal tables are indexed based internal tables ? 
Ans :- Standard, Sorted

 2. Is standard internal table only uses linear search ? 
Ans :- By default, standard internal table uses linear search, We can use binary search also.

 3. Can we use append to insert a record to sorted internal table ?
 Ans :- No, append is never preferred , as it will result in to runtime error, if we insert a record that is not in sorted order. 

4. Which internal table has highest performance when there is large amount of data and search is based upon unique key ? 
Ans :- Hash internal Table 

5. What is the difference between append and insert ? 
Ans :- Append inserts the record at the last of the internal whereas Insert inserts the record at anywhere in the internal table.

No comments:

Post a Comment