How Loop Works in Internal Tables

How loop works in internal tables with example using any SD table? 
What is the effect on INTERNAL TABLE ITAB using CLEAR ITAB, REFRESH ITAB?

Internal table is a temporary storage location which is only available at runtime.

Now we are defining one internal table IT_KNA1 using the database table KNA1.

DATA : BEGIN OF IT_KNA1 OCCURS 0,
               CUST_NO LIKE KNA1-KUNNR,
               NAME LIKE KNA1-NAME1,
               CNTRY LIKE KNA1-LAND1,
             END OF IT_KNA1.

With the above statement internal table is created with body and work area.

Now we have populated the IT_KNA1 using select statement.

Select ... from KNA1 into  IT_KNA1 where.....
 append IT_KNA1.
Endselect. 

(or)

select... from KNA1 into table IT_KNA1 where..... 

The total number of records statisfying the where condition will enter into the internal table. First one record enters work-area of the internal table then it gets appended into the body of the internal table, same procedure repeats for all the records. 

For displaying the data which is in the internal table.

Loop at IT_KNA1.
 WRITE :/IT_KNA1-CUST_NO, IT_KNA1-NAME, IT_KNA1-CNTRY.
ENDLOOP.

Similarly as mentioned above for entering data, for displaying also. The first record from the body of the internal table is fetched into the work-area and then output is displayed, and procedure repeats for all the records in the internal table.

Notes:

How to Loop from second row in internal table?

You can use if condition in the loop endloop.

loop at it_tab1  .

  if sy-tabix > 1.
    write: / it_tab1-mandt,it_tab1-matnr .
   endif.

endloop.

This skips the first record and you can continue the execution of the statements in the loop.

ABAP Tips

See Also
Data Fetching With Internal Table

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Main Index
SAP ERP Modules, Basis, ABAP and Other IMG Stuff

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.