After completing this lesson, you will be able to:
After completing this lesson, you will be able to:
Define simple internal tables
Process data using simple internal tables
Defining a Simple Internal Table
Internal Tables
Internal tables are variable data objects in which you can store several values of identical type. This type has to be specified in the declaration and is called the row type of the internal table.
Each value occupies one row of the internal table. The number of rows is not restricted. Theoretically, you can store any number of values in one internal table. Limitations only come from technical boundaries like available memory or system configuration.
The initial value of an internal table is an empty table or, in other words, a table with 0 lines. There are different techniques for filling an internal table. The example uses the APPEND statement to add a new row at the end of the internal table and fill it with a value.
Table Types
The type of an internal table is called a table type.
In the previous example we used TYPE TABLE OF in the DATA statement directly. The table type was bound to the declared variable.
As an alternative you can use TYPE TABLE OF in a TYPES statement to define a table type with a name. You can then use this table type, for example, in a DATA statement. The visibility of these types depends on the position of the TYPES statement.
There are also table types in the ABAP Dictionary. These table type are maintained with a dedicated editor. They are called global table types because they are visible anywhere in the system.
Processing Data with a Simple Internal Table
Let's look at how you can process data with a simple internal table.
Try It Out: Simple Internal Tables
Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
Code snippet
* Declarations
**********************************************************************
" Internal tables
DATA numbers TYPE TABLE OF i.
"Table type (local)
TYPES tt_strings TYPE TABLE OF string.
DATA texts1 TYPE tt_strings.
" Table type (global)
DATA texts2 TYPE string_table.
" work areas
DATA number TYPE i VALUE 1234.
DATA text TYPE string.
* Example 1: APPEND
**********************************************************************
APPEND 4711 TO numbers.
APPEND number TO numbers.
APPEND 2 * number TO numbers.
out->write( `-----------------` ).
out->write( `Example 1: APPEND` ).
out->write( `-----------------` ).
out->write( numbers ).
* Example 2: CLEAR
**********************************************************************
CLEAR numbers.
out->write( `----------------` ).
out->write( `Example 2: CLEAR` ).
out->write( `----------------` ).
out->write( numbers ).
* Example 3: table expression
**********************************************************************
APPEND 4711 TO numbers.
APPEND number TO numbers.
APPEND 2 * number TO numbers.
out->write( `---------------------------` ).
out->write( `Example 3: Table Expression` ).
out->write( `---------------------------` ).
number = numbers[ 2 ] .
out->write( |Content of row 2: { number }| ).
"Direct use of expression in string template
out->write( |Content of row 1: { numbers[ 1 ] }| ).
* Example 4: LOOP ... ENDLOOP
**********************************************************************
out->write( `---------------------------` ).
out->write( `Example 4: LOOP ... ENDLOOP` ).
out->write( `---------------------------` ).
LOOP AT numbers INTO number.
out->write( |Row: { sy-tabix } Content { number }| ).
ENDLOOP.
* Example 5: Inline declaration in LOOP ... ENDLOOP
**********************************************************************
out->write( `-----------------------------` ).
out->write( `Example 5: Inline Declaration` ).
out->write( `-----------------------------` ).
LOOP AT numbers INTO DATA(number_inline).
out->write( |Row: { sy-tabix } Content { number_inline }| ).
ENDLOOP.