Beruflich Dokumente
Kultur Dokumente
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.
Declaring a structure
Before you can use structures and internal tables in your program, you must declare them.
Example
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.
Note
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.
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:
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>.
In the last exercise, you declared a structure. Now, declare an internal table, based on this
structure.
Example
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:
The statement starts with the keyword DATA, followed by the name of the internal table.
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.
Note
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.
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.
Example
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>:
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:
Declaring internal tables is an essential part of writing ABAP code as this is where most of the
data retrieved from database tables will be stored. During the select statement you retrieve data
from a database table into an internal table (multiple rows) or a work area or header line (single
row).
*&-------------------------------------------------------------*
*& Report ZTYPES *
*& *
*&-------------------------------------------------------------*
REPORT ZTYPES .
* Build internal table and work area from existing internal table
DATA: it_datatab LIKE tab_ekpo OCCURS 0, "old method
wa_datatab LIKE LINE OF tab_ekpo.
* Build internal table and work area from existing internal table,
* adding additional fields
TYPES: BEGIN OF t_repdata.
INCLUDE STRUCTURE tab_ekpo. "could include EKKO table itself!!
TYPES: bukrs TYPE ekpo-werks,
bstyp TYPE ekpo-bukrs.
TYPES: END OF t_repdata.
DATA: it_repdata TYPE STANDARD TABLE OF t_repdata INITIAL SIZE 0, "itab
wa_repdata TYPE t_repdata. "work area (header line)
The select command is the most fundamental function of writing ABAP programs allowing the
retrieval of data from SAP database tables. Below are a few examples of the various ways of
selecting data.
IF sy-subrc EQ 0.
* The FOR ALL ENTRIES comand only retrieves data which matches
* entries within a particular internal table.
SELECT bukrs belnr gjahr buzei mwskz umsks prctr hkont xauto koart
dmbtr mwart hwbas aufnr projk shkzg kokrs
FROM bseg
INTO TABLE it_bseg
FOR ALL ENTRIES IN it_bkpf
WHERE bukrs EQ it_bkpf-bukrs AND
belnr EQ it_bkpf-belnr AND
gjahr EQ it_bkpf-gjahr.
ENDIF.