Unlocking ABAP Mastery: A Comprehensive Guide to Internal Table Operations - Loop, Delete, Modify, Read, Clear, Describe

 Welcome back everyone, So basically we have started with internal table operations in our last blog. So let’s continue it.


Internal Table Operations - Loop :-

  • Loop allows you to execute a group of statements multiple

times.

  • Loop at <itab> where stands for internal table.
  • Loop is used to read the records one by one from the internal table.

Practical Implementations :-

***********************************************************
*Start Of Program
*Using loop
LOOP AT lt_header INTO ls_header.
  WRITE :/ ls_header-order_number, ls_header-payment_mode.
ENDLOOP.
*End of Program.
************************************************************


Output :-




Internal Table Operations - DELETE :-

  • Delete is used to delete the records from the internal table.
  • There are two ways to perform delete operations.
    • Delete based on where condition.
    • Delete based on index.

Delete based on where condition :-

*********************************
*Start of Program
*Using delete to delete the record where payment mode = 'D'.
DELETE lt_header WHERE payment_mode = 'D'.
*End Of Program
**********************************


Output :-



Note :- In our above operation, where we have performed loop operation, we had three records in our internal table, but because of delete operation, the record which have payment mode as ‘D’ got deleted.

Delete based on Index :-

************************************
*Start of Program
*Using delete to delete a record.
DELETE lt_header INDEX 2.
*End of Program
****************************************


Output :-



  • Second Row got deleted.

Internal Table Operations - MODIFY :-

  • It is used to modify the records of the internal table.

Requirement :- Suppose, we have three records in our internal table and we want to modify the payment mode of the record where order number = 1.

*************************************
*Start Of Program
LOOP at lt_header into ls_header.
  if ls_header-order_number = 1.
    ls_header-payment_mode = 'N'.
  endif.
  modify lt_header from ls_header TRANSPORTING payment-mode.
  clear ls_header.
ENDLOOP.
*End of Program
**************************************

Note :- Here, In transporting we use those columns where we are performing some changes.



Output :-



  • As you can see, in the output for order number 1, the payment mode got updated to ‘N’.
  • Note :- For best practice, it is always very importing to use debugger to understand the flow of program.

Internal Table Operations - READ :-

  • Read statement is used to read the first matching record from the internal table.
  • We should never forget to check the sy-subrc condition after the read table.
  • We already have covered, the system variable where we have discussed the sy-subrc.

Read Using Key :-

*********************************************
*Start Of Program
READ TABLE lt_header INTO ls_header WITH KEY order_number = 1.
IF sy-subrc EQ 0.
  WRITE : 'Record :',
         / ls_header-order_number,
         / ls_header-payment_mode.
ELSE.
  WRITE : 'Unsuccessful'.
ENDIF.
*End of Program
*******************************************


Output :-



Read Using Index :-

*********************************************
*Start Of Program
READ TABLE lt_header INTO ls_header INDEX 2.
IF sy-subrc EQ 0.
  WRITE : 'Record :',
         / ls_header-order_number,
         / ls_header-payment_mode.
ELSE.
  WRITE : 'Unsuccessful'.
ENDIF.
*End of Program
*******************************************


Output :-




Internal Table Operations :- Clear or Refresh

  • Both are used to clear the contents of the internal table.
  • In simple words these statements are used to remove the entire contents.

Implementation :-

Suppose, our internal table has 3 records, Now we will use clear statement and we will see what change it caused to our internal table.

********
*Start
CLEAR lt_header.
*End
*******


Output :-




Internal Table Operation :- Describe

  • It returns the number of records in the internal table.
  • Syntax :- DESCRIBE table <itab> lines <lv_lines>.
  • In the above syntax : DESCIBE TABLE = keyword, <itab> = name of the internal table, LINES = keyword, <lv_lines> = local variable which returns the number of records.

Implementation :-

*************************************
*Start
DATA : lv_lines type i.
DESCRIBE TABLE lt_header LINES lv_lines.
WRITE : 'Number of rows : ',
      / lv_lines.
*End
**************************************


Output :-




So, that’s enough for today.

We will continue with types of 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