When you are using ABAP/4, you will mainly work with tables. Tables are the most important data structures in the R/3 system. Data with a long life are stored in relational databases.
Apart from database tables, you can create internal tables that exist only at runtime of the program. ABAP/4 offers several operations for working with internal tables. You can, for example, search for lines or append, insert, or delete lines.
The number of lines of an internal table is not fixed. If required, the system expands internal tables at runtime. That means, if you want to read a database table into an internal table, you must not know the size of the database table in advance. This makes internal tables easy to use.
You can use internal tables for table calculations on subsets of database tables. You can, for example, read a certain part of one or more database tables into an internal table. You can use internal tables for calculating totals or for creating ranked lists.
And you can use internal tables to reorganize table contents or database tables according to the needs of the program. To create a list, you can read certain data from one or more large customer tables into an internal table. At runtime of your program, you can then access this data directly, without having to start the time-consuming database scan for each program call.
After working through the exercises below, you are able to
· declare an internal table
· fill an internal table
· change, output, and delete individual elements
· sort tables by any fields
· understand and use control levels.
Declaring a structure
Before you can use structures and internal tables in your program, you must declare them.
Declare a structure that contains one column for the departure city (CITYFROM), one for the arrival city (CITYTO), one for the airline (CARRID), and one for the flight connection (CONNID). Use parts of the structure of database table SPFLI.
1. Create a new program. When maintaining the attributes, make sure to use the logical database F1S for data retrieval.
2. Declare the structure using the statement pattern. Position the cursor in the declaration section of your program and choose Pattern.
3. On the dialog box, mark the radio button Internal table.
There is no extra pattern for structures. Since it takes only two modifications to change the declaration of an internal table into the declaration of a structure, we, nevertheless, use the statement pattern for internal tables here.
4. Mark with LIKE fields fr. and enter the name of the database table into the adjacent input field.
The item with LIKE fields fr. allows you to select parts of the structure of a database table. If you mark with LIKE for struct. of instead, you copy the entire structure of the database table.
5. Choose Continue.
6. Mark the fields which you need in the internal table.
7. Choose Copy.
8. Specify the name <structure> of the structure to be defined and choose Continue.
9. Replace the keyword DATA, used for the declaration of an internal table, by the keyword TYPES, used for structures, and delete the option OCCURS 0 from the pattern. The declaration then looks like this:
TYPES: BEGIN OF structure,
CARRID LIKE SPFLI-CARRID,
CONNID LIKE SPFLI-CONNID,
CITYFROM LIKE SPFLI-CITYFROM,
CITYTO LIKE SPFLI-CITYTO,
END OF structure.
The statement block starts with the keyword TYPES. The beginning of the declaration of structure <structure> is marked by the statement BEGIN OF <structure>. Then, the individual fields of the structure are declared. The declaration ends with the line END OF <structure>. To declare additional fields for the structure, include the field declarations between BEGIN OF <structure> and END OF <structure>.
Declaring an internal table
In the last exercise, you declared a structure. Now, declare an internal table, based on this structure.
Declare an internal table, based on the structure <structure> you declared in the last exercise.
1. Fetch the program created for the last exercise into the editor.
2. Declare the internal table in the declaration section of your program below the declaration of the structure. The statement you need is:
DATA itab TYPE structure OCCURS 0.
The statement starts with the keyword DATA, followed by the name of the internal table.
The keyword TYPE refers to the previously declared structure <structure>.
The keyword OCCURS defines the structure as internal table. The number behind OCCURS determines the number of lines the table has after initialization. As mentioned in Internal tables, the size of an internal table is variable, that is, the system can expand the number of lines beyond the predefined size, if required.
If you forget to include the keyword OCCURS into the declaration, the result is a field string instead of an internal table. However, you use such a field string as work area for an internal table.
Defining the work area
To change or output the contents of an internal table, you need a work area. When processing an internal table, the system always fills the work area with the contents of the current table line. You can then process the work area.
How to define a work area:
1. If you use an internal table with header line, the system automatically creates a work area (the header line) with the same name as the internal table.
2. If you use an internal table without header line, define a field string of the same structure and use it as work area for the internal table.
In the exercises below, you will work with internal tables without header lines. Therefore, you need a work area.
Define a work area for the internal table without header line created in the last exercise.
1. Fetch the program you created in the last exercise into the editor.
2. Incluse this line to declare a work area <wa> for the internal table <itab>:
DATA: wa TYPE structure.
3. Check the syntax and save your program.
You defined a work area <wa> with the same structure <structure>as the internal table <itab>. You can use this work area in the exercises below to display or change the contents of the internal table. You can access the fields of the work area as you would access fields in the work area of database tables. First, specify the name of the work area <wa>, then a hyphen, and finally the field name:
WA-CONNID is the field CONNID of work area WA.
FOR RELATED POSTS U CAN ALSO BROWSE