Data Fetching With Internal Table

I am always facing a problem with feching data from sap dbtable thru internal table in all spheres, like alv, module pool, smartform, etc. This has become an issue to me. 

Internal table is a 2D structure similar to database table.  The difference between them is, the database table is permanently stored in the database, where as internal table lies in the memory as long as it is retained.

Internal table is a temporary area created through program for storing and retrieving data for processing within the program.  The structure of the internal table is decided based on the program requirement.  Since it is created afresh, obviously there will not be any data in the internal table.  We need to fill the internal table as we require.

There are many ways to fill the internal table.  First subject the internal table to select statement and fill the db data into it.  Second is write loops or other statements and fill data through append statements.  Copy the content of one internal table to other.

After filling, the internal table is just like the database table with multiple rows and columns.  If you want to process all the data in the internal table, you have to read through every line of the internal table through loop.  If you know very well that the internal table contains only one row, then single read statement can be used.  The internal table processing decision is purely based on the contents of the table.  You must be remembering you smartform, where the mistake was that, you were trying with single read statement on the internal table of multiple rows..  You got incorrect total for net price.

The work area is single record strcture of your internal table.  In OO context, for the internal tables without headerline, a separate work area is used to read the records from the internal table.  While the loop runs, each time the record is copied to the work area from internal table during loop pass.

Work area can contain only one record at a time.  Hence unless it is confirmed that select statement is going to fetch only one record from the database, work area should not be used as an receiving area for the database table.  There is syntax difference also for the select statement in this scenario.

Why we go for internal tables:

The database tables are been used across so many sap standard programs.  We can not directly play around with them.  In order to avoid that as per the requirement of the program we go for the concept of internal tables during the program execution and use the internal tables and take the required data from the db table and then we can always play around with the data in the internal table.  It is just like when you go the any supermarket, you take a basket (internal table) and select some items out of the racks of the supermarket (db table) and put into the basket - its just an example for your better understanding.

Well, you always need to declare the internal table structure and its type before you are going to use it. 

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

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

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.