This action might not be possible to undo. Are you sure you want to continue?
The purpose of this lesson is to examine the use of internal tables in ABAP/4 programs. An internal table is a collection of a variable number of records. They are used for temporary storage and exist or live only during the runtime of an ABAP/4 program. Internal tables and database tables work together. The contents of a database table can be copied to an internal table at runtime, so that the internal table is a snapshot of a database table and can then be used as a working copy of the data within the program. Internal tables are used as containers for volatile data in a program.
This lesson consists of the following main topics:
• • •
Data types used with tables Defining internal tables for use within a program Statements that are used in working with internal tables
MOVE and MOVE-CORRESPONDING APPEND, COLLECT, INSERT, MODIFY, and DELETE
• • • •
Use of header lines and work areas with internal tables Filling internal tables with data or removing data from a table Sorting the contents of an internal table Reading individual records from an internal table for processing
Slide 3 of 33
Upon completion of this lesson you should be able to: Define data type for internal tables Create an internal table Create internal tables with and without header lines or work areas Assign table values using the MOVE and MOVECORRESPONDING statements • Understand the use of the APPEND, COLLECT, INSERT, MODIFY, DELETE, and LOOP AT statements for use with internal tables • Understand filling internal tables with data from persistent database tables • Use the SORT statement to arrange the contents of an internal table
• • • •
In structured data types there is a difference between field string types and table types: Field string types (known as structures or records in other terminology) can be made up of components of any type.
Table types (known as arrays in other terminology) can be made up of lines or rows or lines of any type.
Field string types and table types at the highest level can themselves contain further field string types and table types.
Slide 5 of 33
An internal table is a sequence of lines with the same type. If you want to define a table type, follow the line type declaration with the addition OCCURS 0. • You can also write OCCURS <n> instead of OCCURS 0, where <n> is any integer.
For additional information see: Creating Internal Table Data Types
Creating Internal Table Data Types
To create an internal table data type, you use the TYPES statement as follows: Syntax
TYPES <t> <type> OCCURS <n>. This creates an internal table data type <t> by using the OCCURS option of the TYPES statement. The lines of the internal table have the data type specified in <type>. To specify the data type of the lines, you can use either the TYPE or the LIKE parameter. By using the LIKE parameter to refer to an object defined in the ABAP/4 Dictionary, you can create internal tables which have the same line structure as objects stored in the Dictionary, and which reflect the structure of database tables. This is very important when reading and processing database tables <n> specifies an initial number of lines. Memory is reserved for the number of lines specified as soon as the first line is written to an internal table data object created with type <t>. If more lines are added to an internal table than specified by <n>, the reserved memory expands automatically. If there is not enough space in memory for an internal table, it is written to a buffer or to the disk (paging area).
TYPES VECTOR TYPE I OCCURS 10. This example creates an internal table data type VECTOR which has lines consisting of the elementary type I field.
TYPES: BEGIN OF LINE, COLUMN1 TYPE I, COLUMN2 TYPE I, COLUMN3 TYPE I, END OF LINE. TYPES ITAB TYPE LINE OCCURS 10. This example creates an internal table data type ITAB which has lines with the same structure as the field string LINE.
TYPES VECTOR TYPE I OCCURS 10. TYPES: BEGIN OF LINE, COLUMN1 TYPE I, COLUMN2 TYPE I, COLUMN3 TYPE I,
END OF LINE. TYPES ITAB TYPE LINE OCCURS 10. TYPES: BEGIN OF DEEPLINE, TABLE1 TYPE VECTOR, TABLE2 TYPE ITAB, END OF DEEPLINE. TYPES DEEPTABLE TYPE DEEPLINE OCCURS 10. This example creates the same internal table data types (VECTOR and ITAB) as in the above examples. It then creates a data type DEEPLINE as a field string which contains these internal tables as components. Via this field string, a data type DEEPTABLE is created as internal table. Therefore, the elements of this internal table are themselves internal tables. Creating Internal Tables by Referring to Another Table
Creating Internal Tables by Referring to Another Table
To create an internal table data object by referring to an existing internal table data type or data object, you use the DATA statement as follows: Syntax DATA <f> <type> [WITH HEADER LINE]. You can use the <type> option to refer to a table data type or table data object by using TYPE or LIKE . The data object <f> is declared as an internal table with the same structure. If you use the WITH HEADER LINE option, the internal table is created with a table work area <f>. If you want to create an internal table with a header line, the line type cannot directly be an internal table. However, it can be a structure which has internal tables as components.
TYPES: BEGIN OF LINE, COLUMN1 TYPE I, COLUMN2 TYPE I, COLUMN3 TYPE I, END OF LINE. TYPES ITAB TYPE LINE OCCURS 10.
DATA TAB1 TYPE ITAB. DATA TAB2 LIKE TAB1 WITH HEADER LINE. As shown in Creating Internal Table Data Types , this example creates a data type ITAB as an internal table. The data object TAB1 has the same structure as ITAB by referring to ITAB using the TYPE parameter of the DATA statement. The data object TAB2 has the same structure by referring to TAB1 using the LIKE parameter of the DATA statement. TAB2 is created with header line. Therefore, the table work area TAB2 can be addressed in the program by using TAB2-COLUMN1, TAB2-COLUMN2, and TAB2-COLUMN3. Creating Internal Tables by Referring to a Structure
Creating Internal Tables by Referring to a Structure
To create an internal table data object by referring to an existing line structure, you use the DATA statement as follows: Syntax DATA <f> <type> OCCURS <n> [WITH HEADER LINE]. This creates an internal table <f> by using the OCCURS option of the DATA statement. The lines of the internal table have the data type specified in <type>. To specify the data type, you can use either the TYPE or the LIKE parameter. By using the LIKE parameter to refer to an object defined in the ABAP/4 Dictionary, you can create internal tables which have the same line structure as objects stored in the Dictionary, and which reflect the structure of database tables. This is very important when reading and processing database tables <n> specifies an initial number of lines. Memory is reserved for the number of lines specified as soon as the first line is written to an internal table data object created with type <f>. If more lines are added to an internal table than specified by <n>, the reserved memory expands automatically. If there is not enough space in memory for an internal table, it is written to a buffer or to the disk (paging area). The features described above are the same as those for creating internal table data types with the TYPES statement. As an additional feature, you can use the WITH HEADER LINE option with the DATA statement. This creates a table work area <f> with the same structure as the lines of the internal table <f>.
DATA FLIGHT_TAB LIKE SFLIGHT OCCURS 10. This example creates a data object FLIGHT_TAB which has the same structure as the database table SFLIGHT.
This example shows you how to create the same internal table using two different procedures. TYPES VECTOR_TYPE TYPE I OCCURS 10.DATA VECTOR TYPE VECTOR_TYPE WITH HEADER LINE. Here, an internal table data type VECTOR_TYPE is created with lines consisting of an elementary type I field is created first. Then, a data object VECTOR is created as an internal table by referring to VECTOR_TYPE. A table work area VECTOR is also created by using the WITH HEADER LINE option. In this case, the table work area consists of one type I field which can be addressed by the name VECTOR.
Slide 6 of 33
Complex Field String Types
Field string types can themselves contain field string types or table types.
Slide 7 of 33
Complex Table Types
Table types can themselves contain field string types and table types.
Slide 8 of 33
Assigning Values for Complex Data Objects
MOVE also works for copying complex tables and structures. Embedded field strings and table components are also copied.
Slide 9 of 33
Assigning Values Field by Field
The MOVE-CORRESPONDING <rec1> to <rec2> statements transports values field by field between field strings <rec1> and <rec2>. This only works if the components have identical names. The system looks for all fields in <rec1> whose name also occurs in <rec2> and transports field <rec1>-<field name> to <rec2>-<field name> in all cases where it finds a match. All other fields remain unchanged
Slide 10 of 33
As you have already seen, internal tables (‘arrays’ or ‘matrices’ in other terminology) are a set of lines with the same type(s). To define an internal table, simply use the addition OCCURS <n> when you declare a data object with the desired line type. • The OCCURS addition turns the data object into an internal table with the same line type as that of the data object. It also determines the number of lines with which the internal table is created. Unlike the array concept of other programming languages, ABAP/4 can increase the number of lines in the table dynamically at runtime. • If you do not know how big your internal table will be, set the OCCURS addition to 0. If you know that your internal table will be smaller than 8KB, specify the number of table lines using the OCCURS parameter. This ensures that only this amount of memory area is occupied. This is particularly important when you are working with nested structures. If the memory area is insufficient, further table lines are allocated.
For additional information see: Purpose of Internal Tables
Purpose of Internal Tables
In ABAP/4, you work mainly with tables. Tables are the essential data structures in the R/3 System. Long-life data (also known as persistent data) is stored in relational database tables. Besides database tables, you can create internal tables which exist only during the runtime of your program. ABAP/4 provides various operations for working with internal tables. You can, for example, search for, append, insert, or delete lines. The number of lines in an internal table is not fixed. Depending on requirements, the system increases the size of internal tables at runtime. If, for example, you want to read a database table into an internal table, you do not have to know the size of the database table in advance. This feature makes working with internal tables an easy task and also supports dynamic programming. You can use internal tables to perform table calculations on subsets of database tables. For example, you can read a certain part of a database table into an internal table. From the internal table, you can then calculate totals or generate a ranked list. Another use for internal tables is reorganizing the contents of database tables according to the needs of your program. For example, you can read data relevant for creating a telephone list from one or several large customer tables into an internal table. During the runtime of your program, you can then access this list directly without having to perform a time-consuming database query for each call. Besides using them when working with data from database tables, internal tables are an important feature in ABAP/4 for implementing very complex data structures in your program. Structure of Internal Tables
Structure of Internal Tables
In ABAP/4, you can distinguish between internal table data types, which define the structure of internal tables, and internal table data objects, which are the actual internal tables and can be filled with data. An internal table data type is an abstract definition of a data structure which can be used to declare data objects as internal tables. Data type An internal table is one of the two structured data types in ABAP/4. The other structured data type is the field string. An internal table consists of any number of lines which all have the same data type. The data type of the lines can be elementary or structured. This definition opens a
variety of internal table structures which range from lines consisting of one field to lines consisting of field strings which have internal tables as components. You can define a data type as an internal table by using the TYPES statement with the OCCURS parameter. No memory is occupied when defining a data type. Data object A data object which has a data type defined as an internal table is the actual internal table you work with. It occupies memory and you can fill or read its lines. You create a data object as an internal table by using the DATA statement either with the OCCURS parameter or by referring to another internal table by using the TYPE or LIKE parameters.
Slide 11 of 33
Working with Internal Tables
In contrast with the array concepts found in other programming languages, ABAP/4 does NOT use direct access to table entries. Instead, operations on table entries are carried out
using a work area. This is a temporary holding area that contains the content of the current entry for almost ALL commands dealing with the table. This work space may be either a separately defined work area or a header line that is defined when the table is defined.
ABAP/4 recognizes essentially the following operations on internal tables: Command APPEND COLLECT INSERT MODIFY DELETE LOOP AT READ TABLE SORT CLEAR REFRESH FREE Effect Appends the contents of the work area at the end of the internal table Inserts the cumulative contents of the work area into the internal table Inserts the contents of the work area into a particular table entry Overwrites a particular table entry with the content of the work area Deletes a specified entry from the internal table Places the entries of an internal table in a work area one at a time Places a single internal table entry in a work area Sorts the internal table Deletes the work area or the internal table Deletes the internal table Releases space in memory previously occupied by table
Slide 12 of 33
Internal Tables With Header Line
You access internal tables record by record. You must use a work area as an interface for transferring data to and from the table. When you read data from an internal table, the contents of a specified table record or line overwrites the contents of the work area. Then, you can reference the contents of the work area in your program. When you write data to an internal table, you must first enter the data in the work area from with the system can transfer the data to the internal table.
To avoid inconsistencies, it is beneficial if the work area has the same data type as the records or lines of the internal table. A safe procedure for creating work areas which are compatible with internal tables is to use the same data type for declaring both the internal table and the work area. • In ABAP/4, you can distinguish between two kinds of internal tables:
• • • •
Internal tables WITH header lines Internal tables WITHOUT header lines
A header line is similar to a work area for a database table. A work area is used as temporary storage for one entry of a database table. In a similar way, a header line is used to hold one line of an internal table. • An internal table with header line is a tuple from a work area (header line) and the bulk of the table itself. Both are addressed using the same name, the interpretation of the name is context-sensitive. Hence it would stand for the header line in a MOVE statement, but would stand for the bulk of the table in a SEARCH statement. • When you create an internal table WITH a header line, a work area is created automatically with the same data type as the rows of the internal table.
The header line and the internal table have the same name. Then, the system uses this work area implicitly. • In ABAP/4 statements for accessing internal tables, you can specify the work area to be used. For internal tables with header lines, you can leave out this specification. Then, the system uses the table work area implicitly.
Slide 13 of 33
Internal Tables Without Header Line
If a table does not have a header line, you MUST provide a work area as a separate record to hold the content of the current entry for most commands used in processing tables. Internal tables WITHOUT a header line do NOT have a table work area declared which can be used implicitly. To access internal tables WITHOUT header lines, you must specify a work area explicitly in the corresponding ABAP/4 statements. • It is a matter of your preference whether you use a header line or a work area. • Remember that, for internal tables with header lines, the internal table itself and the table work areas have the same name. If you use the name in a
statement, the system interprets it as the name of the table work area and NOT as the table itself. In some statement, however, you can enter square brackets after the name to address the internal table instead of the table work area as follows: <name> [ ].
Slide 14 of 33
Declaring Internal Tables with Header Line
You define an internal table with a header line using the addition WITH HEADER LINE. A HEADER line is a work area where the currently active, individual record in the table resides.
Slide 15 of 33
Internal Tables Before Release 3.0
The DATA: BEGIN OF ..., ..., END OF ... can also be extended using the OCCURS parameter. These internal tables always have a header line, even if you do not use the WITH HEADER addition. • Before Release 3.0 this syntax was the only way to declare internal tables. You will still see it frequently in ABAP/4 coding.
For additional information see: Creating Internal Tables with a New Structure
Creating Internal Tables with a New Structure
To create an internal table data object without referring either to an existing object or to an existing line structure, you use the DATA statement as follows: Syntax
DATA: BEGIN OF <f> OCCURS <n>, <component declaration>, .............. END OF <f>. This defines an internal table <f> and declares the components of its lines in <component declaration>. Apart from the OCCURS parameter, the syntax is the same as for defining field strings. This statement ALWAYS creates a table work area <f>. Its effect is therefore the same as when you create a field string <f> first and then an internal table <f> with the same line structure as the field string. <n> specifies an initial number of lines. Memory is reserved for the number of lines specified as soon as the first line is written to an internal table data object created with type <f>. If more lines are added to an internal table than specified by <n>, the reserved memory expands automatically. If there is not enough space in memory for an internal table, it is written to a buffer or to the disk (paging area).
Slide 16 of 33
Filling Internal Tables
The SELECT command, which you first encountered in Lesson 1, places one entry from a database table into the table work area of the same name in each loop pass. If you want to be able to address all table entries during the program runtime, each table entry must be saved in the internal table.
You use the APPEND statement to fill the internal table: APPEND <wa> TO <itab> adds the contents of work area <wa> to the end of the internal table <itab>. If you are working with an internal table with header line, the syntax is shortened to APPEND <itab>.
For additional information see: Filling Internal Tables
Filling Internal Tables
To fill an internal table, you can either append data line by line, or you can copy the contents of another table. To fill an internal table line by line, you can use either the APPEND, COLLECT, or INSERT statements. To use an internal table just for storing data, you are recommended to use APPEND for performance reasons. With APPEND, you can also create ranked lists. To calculate totals of numeric fields or to ensure that no duplicate entries occur in an internal table, use the COLLECT statement which processes lines depending on the standard key. To insert a new line before an existing line in an internal table, use the INSERT statement. To copy the contents of one internal table into another one, use the variants of the APPEND, INSERT, or MOVE statements. To append lines of an internal table to another internal table, use a variant of the APPEND statement. To insert lines of an internal table in another internal table, use a variant of the INSERT statement. To copy the entire contents of an internal table into another internal table, and overwrite that target table, use the MOVE statement.
Reading Data from Database Tables
Reading Data from Database Tables
To read data from a database table, use the SELECT statement. Syntax SELECT <result> FROM <source> [INTO <target>] [WHERE <condition>] [GROUP BY <fields>] [ORDER BY <sort_order>]. This statement has several basic clauses. Each clause is described in the following table. Clause SELECT <result> Description The SELECT clause defines whether the result of the selection is a single line or a table, which columns are to be selected, and whether identical lines are to be excluded. The FROM clause specifies the database table or view <source> from which the data is to be selected. The INTO clause determines the target area <target> into which the selected data is to be read. It can also be placed before the FROM clause. If you do not specify an INTO clause, the system uses the table work area. The table work area is a header line which is automatically created by the TABLES statement. The WHERE clause specifies which lines are to be read by specifying conditions for the selection. The GROUP-BY clause produces a single line of results from groups of several lines. A group is a set of lines with identical values for each column listed in <fields>. The ORDER-BY clause defines a sequence <sort_order> for the lines resulting from the selection.
FROM <source> INTO <target>
WHERE <condition> GROUP BY <fields>
ORDER BY <sort_order>
Slide 17 of 33
You can fill an internal table with entries from a database table using a single SELECT statement. The database system reads the entries in bundles, not singly. Once read, the bundle is inserted en masse into the internal table. This method has performance advantages over reading in records individually via a loop. • Since this is not a loop, there is no ENDSELECT. • In the basic form (... INTO TABLE ...) the internal table is filled with the database entries found and existing entries are overwritten. • In the variant ... APPENDING TABLE... the entries are appended to the existing entries in the internal table.
Slide 18 of 33
Filling Internal Tables with Cumulative Values
You use the COLLECT statement to add the work area or header line to an existing entry of the same type or (if no such entry exists) to add it to the table as a new entry. To do this, COLLECT searches in the internal table for an entry, all of whose alphanumeric fields are identical with those of the entry in the work area or header line. • If such an entry is found, COLLECT adds all numeric fields from the work area or header line to the corresponding fields in the table entry. • Otherwise, the COLLECT statement appends the contents of the work area or header line to the end of the table.
Slide 19 of 33
Sorting an Internal Table
You can sort an internal table using the SORT statement. If you do NOT specify any sort criteria, the table is sorted by ALLl fields except those with data type P, I and F. The fields are sorted ascending in the order in which they occur. • You can use the additions BY <field name> and ASCENDING or DESCENDING to limit the sort to certain fields and determine the sort sequence and hierarchy. • You should use the BY addition to narrow down the sort criteria for performance reasons. • As of Release 3.0D you can perform a language-specific alphabetical sort of text fields using the TEXT addition.
For additional information see: Sorting Internal Tables
Sorting Internal Tables
To sort an internal table, use the SORT statement as follows: Syntax SORT <itab> [<order>] [AS TEXT] [BY <f1> [<order>] [AS TEXT] ... <fn> [<order>] [AS TEXT]]. If you do not use the BY option, the internal table <itab> is sorted by its standard key. To define a different sort key, use the BY option. The system then sorts the dataset according to the specified components <f1> ... <fn>. These fields can be of any type, including type P, I, and F fields, or tables. The number of sort key fields is restricted to 250. If you specify more than one key field, the system sorts the records first by <f1>, then by <f2>, and so on. The system uses the options you specify before BY as a default for all fields specified behind BY. The options that you specify after individual fields overwrite for these fields the options specified before BY. If a sort criterion is not known until runtime, you can set it dynamically by writing (<name>) instead of <fi>. The field <name> contains the name of the sort key field. If <name> is empty at runtime, the system ignores it. If it contains an invalid component name, a runtime error occurs. For any field you use in the sort key, you can specify the offset and length. You can specify the sorting sequence by entering DESCENDING or ASCENDING in the <order> option. The standard sequence is ascending order. You can influence the sorting method for character fields with the option AS TEXT. Without the option AS TEXT, the system sorts character fields binarily and according to their platformdependent internal coding. With the option AS TEXT, the system sorts character fields alphabetically according to the current text environment. By default, the text environment is set in the user's master record. As an exception, you can set the text environment with the statement SET LOCALE LANGUAGE. The option AS TEXT frees you from converting a character field into a sortable format before sorting. Such a conversion is only necessary, if you want to: sort an internal table alphabetically first and search it binarily afterwards. The order of an internal table after alphabetical sorting differs from the order after binary sorting. • sort an internal table several times with aphabetical keys. In this case, the performance is better, because the conversion is processed only once. • create alphabetical indexes for database tables in your program.
If you specify AS TEXT before BY, the option influences only the character fields in the sort key. If you specify AS TEXT after a field name, this field must be of type C. Note!
If you specify the sort key yourself, you can improve performance by keeping the key relatively short. However, if the sort key contains an internal table, the sorting process may be slowed down considerably. Sorting is not stable. This means that the old sequence of lines with the same sort key may not necessarily be retained. If there is not enough space for sorting in the main memory, the system writes data into a temporary external file. The name of this file is defined in the SAP profile parameter DIR_SORTTMP.
DATA: BEGIN OF ITAB OCCURS 10, LAND(3) TYPE C, NAME(10) TYPE C, AGE TYPE I, WEIGHT TYPE P DECIMALS 2, END OF ITAB. ITAB-LAND = 'USA'. ITAB-NAME = 'Nancy'. ITAB-AGE = 35. ITAB-WEIGHT = '45.00'. APPEND ITAB. ITAB-LAND = 'USA'. ITAB-AGE = 40. APPEND ITAB. ITAB-LAND = 'GB'. ITAB-AGE = 18. APPEND ITAB. ITAB-LAND = 'F'. ITAB-AGE = 30. APPEND ITAB. ITAB-LAND = 'G'. ITAB-AGE = 60. APPEND ITAB. SORT ITAB. LOOP AT ITAB. WRITE: / ITAB-LAND, ITAB-NAME, ITAB-AGE, ITAB-WEIGHT. ENDLOOP. SKIP. ITAB-NAME = 'Howard'. ITAB-WEIGHT = '95.00'. ITAB-NAME = 'Jennifer'. ITAB-WEIGHT = '50.00'. ITAB-NAME = 'Michele'. ITAB-WEIGHT = '60.00'. ITAB-NAME = 'Karl'. ITAB-WEIGHT = '75.00'.
SORT ITAB DESCENDING BY LAND WEIGHT ASCENDING. LOOP AT ITAB. WRITE: / ITAB-LAND, ITAB-NAME, ITAB-AGE, ITAB-WEIGHT. ENDLOOP. This produces the following output: F G GB USA USA Michele Karl Jennifer Howard Nancy 30 60 18 40 35 60.00 75.00 50.00 95.00 45.00
USA USA GB G F
Nancy Howard Jennifer Karl Michele
35 40 18 60 30
45.00 95.00 50.00 75.00 60.00
Here, an internal table ITAB is created with a header line and filled with 5 lines. First, it is sorted by its standard key, which is LAND and NAME. Then, it is sorted by a sort key defined as LAND and WEIGHT. The general sort order is defined as descending, but for WEIGHT it is defined as ascending. This is why the line with the NAME field "NANCY" is output before the line with the NAME field "HOWARD" after the second SORT statement. To create a ranked list, you can also use the SORTED BY option of the APPEND statement instead of the SORT statement. You find examples for using SORT to sort data from database tables in R/3 Help under Refining Data Using Internal Tables.
Slide 20 of 33
Using Loops with an Internal Table
You can process an internal table using the loop statement LOOP AT ... ENDLOOP. With each loop pass the system places the next table entry in the work area <wa> or the header line of the internal table <itab>. You can restrict the entries which are read using the WHERE addition in the same way as you do when using the SELECT command. • At the beginning of each loop pass, SY-TABIX is set to the value of the current table entry. When the system leaves the LOOP, SY-TABIX has the same value as it had before the loop started.
Slide 21 of 33
Reading an Entry from an Internal Table
You use the READ TABLE <itab> statement to read a single table entry. When the entry has been read, it is in the work area <wa> or the header line of <itab>. If the entry is successfully read, the value of SY-SUBRC is zero, otherwise it is not equal to zero. SY-TABIX takes the value of the table entry read. • READ TABLE <itab> INDEX <n>. The nth table entry is read. • If you wish to specify individual fields as a search argument you can use the following syntax: READ TABLE <itab> WITH KEY <ki> = <v1> <k2> = <v2>...<kn> = <vn>. In this case, the first entry from <itab> is read which corresponds with the components specified in <k1>...<kn>. • If the internal table is sorted by the search argument, you can use the BINARY SEARCH addition. The system then carries out a more performanceefficient binary search for the specified entry. • The online documentation for the READ statement contains details of further additions.
Slide 22 of 33
Changing an Internal Table I
The MODIFY <itab> INDEX <i> [FROM <wa>] statement overwrites table entry ‘i’ with the contents of the work area or the header line. Entry ‘i’ must already exist. The INSERT [<wa> INTO] <itab> INDEX <i> statement creates a new table entry before entry ‘i’ containing the contents of the work area or the header line. If the table has ‘i’-1 entries, the contents of the work area or header line are appended to the internal table. The DELETE <tab> INDEX <i> statement deletes table entry ‘i’.
Slide 23 of 33
Changing an Internal Table II
You can make changes to an internal table from within a LOOP. The changes always apply to the current table entry (SY-TABIX). INSERT A new record is inserted before the current entry containing the contents of the work area or header line. MODIFY The current entry is overwritten with the contents of the work area or header line. DELETE The current record is deleted. The following additions can be used with the DELETE statement (cf. corresponding additions with the LOOP statement): ...WHERE <condition>. The DELETE command applies to all table entries which satisfy the condition. • ...FROM <n1> TO <n2>. All entries from <n1> to <n2> inclusive are deleted. If only the FROM addition is specified, all entries from <n1> to the end
of the table are deleted. If only the TO addition is specified, all entries from the beginning of the table to <n2> are deleted. Internal Table Index The index is the sequential number of a table line. It is NOT a table field, but is created and managed automatically by the system. You can use the index with the DELETE, INSERT, MODIFY, LOOP, and READ statements. In these statements, you can specify the index either as a literal or as a variable. After processing a particular line of an internal table, the system field SY-TABIX generally contains the index of that line.
Slide 24 of 33
Deleting an Internal Table
If you are using an internal table without a header line, CLEAR <itab> deletes the body of the table.
If your internal table has a header line, CLEAR <itab> deletes the header line. If you only want to address the body of an internal table with a header line, you use the form <itab>. Example 1: ITAB_WITHOUT and ITAB_WITH are two internal tables without and with header lines respectively. The command ITAB_WITHOUT = ITAB_WITH assigns the body of ITAB_WITH to ITAB_WITHOUT. • Example 2: CLEAR ITAB_WITH deletes only the body of the internal table ITAB_WITH.
REFRESH <itab> deletes the body of the table. For additional information see: Initializing Internal Tables
Initializing Internal Tables
To initialize an internal table with or without a header line, you use the REFRESH statement as follows: Syntax REFRESH <itab>. This statement resets an internal table to the state before it was filled. This means that the table contains no lines. If you are working with an internal table without table work area, you can use the CLEAR statement instead of the REFRESH statement as follows: Syntax CLEAR <itab>. If you are working with an internal table with a header line, the CLEAR statement clears only the table work area as explained in Resetting Values to Default Values. To reset the whole internal table without clearing the table work area, use either the REFRESH statement or the CLEAR statement as follows: Syntax CLEAR <itab>. The square brackets after the name of the internal table refer to the body of the internal table.
After using REFRESH or CLEAR to initialize an internal table, the system keeps the space in memory reserved. You can release the memory with the FREE statement as follows: Syntax FREE <itab>. You can also use the FREE statement to reset an internal table and to release its memory directly, without using REFRESH or CLEAR beforehand. Like REFRESH, FREE works on the table body, not on the table work area. After a FREE statement, you can address the internal table again. The system then reserves memory space again. You can check whether an internal table is empty by using the following logical expression: Syntax ... <itab> IS INITIAL ...
DATA: BEGIN OF LINE, COL1, COL2, END OF LINE. DATA ITAB LIKE LINE OCCURS 10. LINE-COL1 = 'A'. LINE-COL2 = 'B'. APPEND LINE TO ITAB. REFRESH ITAB. IF ITAB IS INITIAL. WRITE 'ITAB is empty'. FREE ITAB. ENDIF. The output appears as follows: ITAB is empty. In this program, an internal table ITAB is filled and then initialized with REFRESH. In an IF statement, a logical expression with the IS INITIAL parameter is used to check whether ITAB is empty. If so, the memory is released.
Slide 25 of 33
Information about Internal Tables
You can get information about an internal table using the DESCRIBE TABLE statement: The LINES addition returns the number of entries currently in the table. The OCCURS addition returns the number of OCCURS in the table definition.
You can display information about any data object using the DESCRIBE FIELD statement (see the online documentation for the DESCRIBE statement).
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue reading from where you left off, or restart the preview.