Reading data from an Internal Table

Two statements are commonly used to read the data from an internal table.
  • loop at
  • read table
loop at is used to read multiple rows from the internal table.
read table is used to read a single row.

To read some or all rows from an internal table, you can use the loop at statement. loop at reads the contents of the internal table, placing them once at a time into a work area.

Syntax for the loop at statement :

loop at <internal table body> into <internal table work area>.
<statements>.
endloop.

Example :

loop at ITAB[] into ITAB.
write : / ITAB-A, ITAB-B.
endloop.

Syntax for conditional loop statement :

loop at <internal table body> into <internal table work area> from <m> to <n> where <expression>.
<statements>.
endloop.

Example :

loop at ITAB[] into ITAB from 2 to 5 where B = 10.
write : / ITAB-A, ITAB-B.
endloop.

Explanation :

  • internal table body is the name of the internal table body.
  • internal table work area is the name of the internal table work area.
  • m and n are integer literals, constants, or variables representing a relative row number. For example 1 means the first row in the table, 2 means the second, and so on.
  • expression is a logical expression restricting the rows that are read.
  • <statements> represents any number of lines of code. These lines are executed once for each row retrieved from the internal table.
The rows are read from the internal table one at a time and placed in succession into the work area. The lines of code between the loop at and endloop are executed for each row retrieved. The loop finishes automatically when the last row has been read, and the statement following the endloop is then executed.

The statement loop at ITAB[] into ITAB reads the row from ITAB[] and places it into header line ITAB . The statement loop at ITAB does the same thing, because the default work area is the header line. Being more succint, the latter is usually used.

The below program adds the 5 rows to the internal table and then displays it.

DATA : BEGIN OF ITAB OCCURS 5,
             A TYPE I,                                            
             B TYPE I,                                                    
             END OF ITAB.

ITAB-A = 1.
ITAB-B = 10.
APPEND ITAB.

ITAB-A = 2.
ITAB-B = 20.
APPEND ITAB.

ITAB-A = 3.
ITAB-B = 30.
APPEND ITAB.

ITAB-A = 4.
ITAB-B = 40.
APPEND ITAB.

ITAB-A = 5.
ITAB-B = 50.
APPEND ITAB.

LOOP AT ITAB.
WRITE : / ITAB-A. ITAB-B.
ENDLOOP.
--------------------------------------------------------------------------------------------------------------------------
O/P :

1       10
2       20
3       30
4       40
5       50
------------------------------------------------------------------------------------------------------------------------------




About OCCURS

Now we have to look at the syntax of internal table :


DATA : BEGIN OF <Internal table name> OCCURS  <n>,
           <Field Structure / Line Type>,
           END OF < Internal table name >.

here OCCURS <n> doesn't limit the number of rows that can be added to the internal table. For example, if you specify OCCURS 5, you can put more than 5 rows into internal table. The number of rows you can put into an internal table is theoretically only limited by the amount of virtual memory available on the application server.

The system uses the  OCCURS <n> clause only as a guideline to determine how much memory to allocate. The first time a row is added to the internal table, enough memory is allocated to hold the number of rows specified on the OCCURS <n> clause, If you use that memory up, more is allocated as needed.

Alternatively, you can specify OCCURS 0. If you do this, the system allocates 8KB pages of memory at a time. However, there are no advantages to using OCCURS 0 other than the fact it is only a little easier to code OCCURS 0 than it is to estimate the size of the internal table.

NOTE : Don't use OCCURS 0 if you expect to store less than 8KB in an internal table. If you do, the system will allocate 8KB from the paging area. Memory will be wasted and paging could increse, resulting in poorer performance.  

Adding Data to an Internal Table

To add a single row to an internal table, you can use the append statement. Append copies a single row from any work area and places it in the body at the end of the existing rows. The work are can be the header line, or it can be any other field string having the same structure as a row in the body.

Syntax for append statement

append <wa> to <internal table (body)>.

where:
  • wa is the name of the work area.
The following points apply:
  • wa must have the same structure as a row of the body.
  • wa can be the header line or it can be any field string having the same structure as a row in the body.
  • if you don't specify a work area, by default  the system uses the header line. In effect, the header line is the default work area.
  • After append, SY-TABIX is set to the relative row number of the row just appended. For example, after appending the first row, SY-TABIX will be set to 1. After appending second row, SY-TABIX will be 2, and so on.
The statement append itab to itab[] appends the header line named itab to the body named itab[]. The statement append itab does the same thing, because the default work area is the header line. Being more succint, the latter is usually used.
A work area mentioned explicitly in the append statement is known as an explicit work area. Any field string having the same structure as a row of the internal table can be used as an explicit work area. If a work area is not mentioned, the implicit work area (the header line) is used.

The below program adds the 5 rows to the internal table.

DATA : BEGIN OF ITAB OCCURS 5,
             A TYPE I,                                            
             B TYPE I,                                                    
             END OF ITAB.

ITAB-A = 1.
ITAB-B = 10.
APPEND ITAB.
WRITE : / 'SY-TABIX = ', SY-TABIX.

ITAB-A = 2.
ITAB-B = 20.
APPEND ITAB.
WRITE : / 'SY-TABIX = ', SY-TABIX.

ITAB-A = 3.
ITAB-B = 30.
APPEND ITAB.
WRITE : / 'SY-TABIX = ', SY-TABIX.

ITAB-A = 4.
ITAB-B = 40.
APPEND ITAB.
WRITE : / 'SY-TABIX = ', SY-TABIX.

ITAB-A = 5.
ITAB-B = 50.
APPEND ITAB.
WRITE : / 'SY-TABIX = ', SY-TABIX.
------------------------------------------------------------------------------------------------------------
O/P:

SYITABIX = 1
SYITABIX = 2
SYITABIX = 3
SYITABIX = 4
SYITABIX = 5

Internal Tables - Index

Internal Tables


An internal table is a temporary table stored in RAM on the application server. It is created and filled by a program during execution and is discarded when the program ends. Like a database table, an internal table consists of one or more rows with an identical structure, but unlike a database table, it can’t hold data after the program ends. Use it as temporary storage for manipulating data or as a temporary private buffer.

Most Of the times, Each Department want to see the same data in a different view and want to Manipulate according their requirements and that Manipulated data is NOT required by the other departments So that Data in Database Should not be changed , When the Changes are required by all the Departments. Instead the Copy of the required Database Tables should be fetched into Program and Manipulate it accordingly, Which doesn't change the Data base. To Maintain the Copy Of the Database Table Data in the Program, the Program should have a variable (Temporary Table) , Which is Nothing but Internal Table.

  •   Internal tables are like array of a structure.
  •  They resemble data tables.
  •   They are used to store & process data extracted from database tables.
  •   They can dynamically manage memory allocation & deallocation.
An internal table consists of a body and an optional header line.
The body holds the rows of the internal table. All rows within it have the same structure. The term “internal table” itself usually refers to the body of the internal table.

The header line is a field string with the same structure as a row of the body, but it can only hold a single row. It is a buffer used to hold each record before it is added or each record as it is retrieved from the internal table. 


Syntax:

DATA : BEGIN OF <Internal table name> OCCURS  <n>,
           <Field Structure / Line Type>,
           END OF < Internal table name >.

Example:

DATA : BEGIN OF ITAB OCCURS 3,
           A TYPE I,
           B TYPE I,
           END OF ITAB.


To define an internal table body, use occurs n on the definition of any field string except tables. Occurs creates the body of the internal table. The memory for the body is not allocated until the first row is added to it. Without occurs, you only have a field string.


To define an internal table with a header line, you must include either begin of or with header line in the definition. A header line is automatically created if begin of appears in the definition. If you use like instead of begin of, the internal table will not have a header line unless you add with header line after the occurs clause.


The only time you would create an internal table without a header line is in the case of a nested internal table. A nested internal table can't have a header line.


Defining Internal Tables with & without a header line:


TYPES : BEGIN OF STRUCT,      ------> Defining Structure
            A TYPE I,
            B TYPE I,
            END OF STRUCT.


DATA : BEGIN OF ITAB1 OCCURS 3,     ------> Defining Internal Table with a 
           A TYPE I,                                         header line.
           B TYPE I,
           END OF ITAB.


DATA ITAB2 LIKE STRUCT OCCURS 10    -----> Doesn't have a header line


DATA ITAB3 LIKE STRUCT OCCURS 10 WITH HEADER LINE -->With header line


In the above code, ITAB1 is an internal table because of the presence of the occurs clause. It has two components A and B. Each type is I. This internal table has a header line because the definition contains the words begin of.


ITAB2 contains the same components as defined in the structure. ITAB2 doesn't have a header line because neither "begin of" nor "with header line" appear in the definition.


ITAB3 definition is almost same as that of ITAB2. The difference is the addition "with header line". This, of course, causes ITAB3 to have a header line.

DATA TYPES


To work with data at run time, you need to store it in the program at run time and address it from your program. The system needs to know the type of the data.

A data object is the name of a memory location, used to store or change values of a specific data type.


Predefined Data Types:


Defining & Declaring Data Objects:


Key Words:
                                         DATA    -   Used to define data objects.
                                         TYPE     -   Used to specify the data type.


Syntax:
DATA  <Object Name> TYPE <Data Type>.


Example
DATA A TYPE I.


NOTE: For Integer and Float Values are assigned directly. But for Packed Decimal, Character, Numeric Character, Date, Time, String values are assigned within the single quotes.

User defined Data Types:


Local Data Types: Data types defined and used within a program.

Local Data Types are defined within a program unit with the key word "TYPES".

Syntax:
TYPES  <new data type name> TYPE <data type>.


Example:
TYPES A TYPE I.


TYPES A TYPE I                                                                           DATA A TYPE I
            |                                                                                                  |
            |                                                                                                  |
    Data Type                                                                                   Data Object


  • Data Type is a specification, it provides the characteristics of the data type specified.
  • Data Object is the name of a memory location with a specific data type to store or change the values.
Using Keyword : Like


"LIKE" is used in place of  "TYPE" to define user defined data types od data objects with reference to existing data objects.

Example1 :   DATA A TYPE I.
                     DATA B LIKE A.

Example2 :   DATA NAME(10) TYPE C.
                     DATA EMPNAME LIKE NAME.

Example3 :  DATA : BEGIN OF EMP,
                                EMPNO(3) TYPE N,
                                EMPNAME(10) TYPE C,
                                EMPSAL TYPE I,
                                END OF EMP.
                     DATA : EMP1 LIKE EMP.