Documente Academic
Documente Profesional
Documente Cultură
Field strings
Internal tables with header line
Internal tables without header line
SAP AG
Chapter 06 Objectives
SAP AG
Field Strings / Internal Tables (with Header Line)
Field string
FLIGHT DATA
Carrier Connection Date Price
Internal tables
SAP AG
A field string is a group of fields which logically belong together. You can address a field string by
name (it is regarded by as a character structure). You address individual fields within a field string by
specifying the name of the field string followed by the field name (e.g. FLIGHTDATA-CARRIER).
You can use field strings in connection with subroutines and sequential datasets.
If you define a table work area in an ABAP/4 program with the TABLES statement, it has a similar
structure to a field string.
An internal table consists of a header line and a number of identically structured table lines. The
header line has the same structure as a field string.
The header line is the work area for the internal table. When you fill an internal table, the data is first
placed in the header line. By using the appropriate statements, you then transfer the contents of the
header line to the table. When reading an internal table, ABAP/4 places the contents of the line being
read in the header line.
Declaring Field Strings
DATA: BEGIN OF <nam e>,
END OF <name>.
REPORT RSAAA06A.
You declare field strings with the DATA statement. Here, you specify the beginning and the end of the
field string with BEGIN OF <name> and END OF <name>.
You define the sub-fields of a field string like individual fields with a length, type and, if applicable,
an initial value. You can also use the LIKE parameter to take over the attributes of previously declared
internal fields or fields defined in the ABAP/4 Dictionary.
You address the sub-fields of a field string as follows:
CLEAR <f1>.
SAP AG
The MOVE-CORRESPONDING <f1> to <f2> statement transports values field by field between the
ABAP/4 data structures f1 and f2. Transport is only possible if the field names are identical.
The system searches in f1 for all single fields with names which also occur in f2 and then generates
the following statement for all these field pairs:
FLIGHTINFO_LIST
FLAG CARRID CONNID FLDATE PRICE
header line
SAP AG
Declaring an internal table is similar to declaring a field string. After BEGIN, however, the OCCURS
parameter indicates that an internal table is being created. After OCCURS, you specify a numeric
literal. If you know that your internal table requires less than 8 KB of memory, you specify the number
of table lines with the OCCURS parameter and the system allocates only that amount of memory. This
is particularly important for nested structures. If the amount of memory space is insufficient, the
system allocates further blocks of 8 KB or 16 KB. If you are unable to estimate the size of the internal
table, the OCCURS parameter value can remain set to 0. The system then generates blocks of 8 KB or
16 KB to accommodate the table lines, depending on their length. In line with the new memory
management process, internal tables are stored in Extended Memory, a directly adressable memory
area.
Using ABAP/4 Dictionary Structures
INCLUDE STRUCTURE <tab>.
REPORT RSAAA06D.
.
.
.
DATA: BEGIN OF ITAB OCCURS 5.
INCLUDE STRUCTURE SFLIGHT.
DATA: END OF ITAB,
NAME2 LIKE SCUSTOM-NAME.
SFLIGHT-MANDT CLNT C 3
SFLIGHT-CARRID CHAR C 3
SFLIGHT-CONNID NUMC N 4
SFLIGHT-FLDATE DATS D 8
SAP AG
INCLUDE STRUCTURE allows you to use the structure of an ABAP/4 Dictionary table as the basis
for an internal table or field string. You can also use the field names, attributes and conversion
routines. The same applies to LIKE with single fields.
INCLUDE STRUCTURE also allows you to refer to internal report structures.
Flling Internal Tables (with Header Line) I
AA 0064 89000
header AA 0064 81200
line
AA 0064 12300 LH 0400 100000
AA 0064 66667 LH 0402 100000
DL 1699 66667 UA 0007 100000
LH 0400 100000 AA 0017 66667
LH 0402 100000 DL 1984 43200
UA 0007 100000 UA 0007 23300
SAP AG
The APPEND <itab> statement stores the contents of the header line at the end of the internal table.
The APPEND <itab> SORTED BY <field> statement allows you to generate ranked lists. When you
add a new entry in this way, the table is always re-sorted by the field f in descending order.
The maximum number of table entries is determined by the OCCURS parameter. If the table is full
and there are new entries to be added and sorted, the entry or entries at the end of the table are forced
out.
If you do not want to fill the internal table from the header line, but from an identically structured field
string <wa>, use the APPEND <wa> TO <itab> statement. This is faster than <itab> = <wa>.
APPEND <itab>.
Filling Internal Tables (with Header Line) II
REPORT RSAAA06E.
TABLES: SFLIGHT.
DATA: BEGIN OF FLIGHTINFO_LIST
OCCURS 10,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID,
PRICE LIKE SFLIGHT-PRICE,
END OF FLIGHTINFO_LIST.
ENDSELECT.
.
.
.
REPORT RSAAA06F.
.
.
SELECT * FROM SFLIGHT.
MOVE-CORRESPONDING SFLIGHT TO
FLIGHTINFO_LIST.
APPEND FLIGHTINFO_LIST SORTED BY PRICE.
ENDSELECT.
.
.
SAP AG
In the first example, the table entries are stored in the sequence in which they are read in. Here, it is
possible to store more than the number of lines specified with OCCURS (in this case 10).
In the second example, the table entries are sorted in descending order by the field PRICE. Here, the
number of lines stored cannot exceed that specified with OCCURS.
Adding Data to an Internal Table (with
Header Line)
TABLES: SFLIGHT.
CA RRID PRICE (TYP P)
LH 100000 header line DATA: BEGIN OF FLIGHTINFO_LIST
AA 2090938 1 OCCURS 10,
CARRID LIKE SFLIGHT-CARRID,
DL 933338 2
PRICE LIKE TSFLIGHT-PRICE,
LH 1766669 3 1866669
END OF FLIGHTINFO_LIST.
4
5 SELECT * FROM SFLIGHT.
6 MOVE-CORRESPONDING SFLIGHT TO
7 FLIGHTINFO_LIST.
COLLECT FLIGHTINFO_LIST.
8
9 ENDSELECT.
SAP AG
The COLLECT statement adds the contents of the header line of an internal table to the table as a new
entry or adds it to an existing entry of the same type.
ABAP/4 searches in the table for an entry which matches the contents of header line in all fields other
than those of type P, I or F. If it finds such an entry, it adds all the type P, I and F fields in the header
line to the corresponding fields in the table entry.
If no matching table entry is found, the contents of the header line are appended to the end of the table
as a new table entry.
Sorting Internal Tables
SORT <itab> BY <field1> <field2> . . . .
REPORT RSAAA06H.
TABLES: SFLIGHT.
DATA: BEGIN OF FLIGHTINFO_LIST OCCURS 10,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID,
FLDATE LIKE SFLIGHT-FLDATE,
PRICE LIKE SFLIGHT-PRICE,
END OF FLIGHTINFO_LIST.
SELECT * FROM SFLIGHT.
MOVE-CORRESPONDING SFLIGHT TO FLIGHTINFO_LIST.
APPEND FLIGHTINFO_LIST.
ENDSELECT.
SORT FLIGHTINFO_LIST.
.
.
SORT FLIGHTINFO_LIST BY PRICE FLDATE.
.
.
SORT FLIGHTINFO_LIST BY PRICE ASCENDING
FLDATE DESCENDING.
SAP AG
To sort an internal table, you use the SORT statement. If you do not specify a sort criterion, the table
is sorted by all fields, with the exception of data types P, I or F, in ascending order of their declaration.
The additions BY <field name> and ASCENDING or DESCENDING allow you to restrict the sort to
particular fields and to determine the sort sequence and hierarchy (even for type P, I and F fields!).
Whenever possible, you should restrict the sort criteria with the BY parameter. ABAP/4 then needs
less memory for the sort process in the roll area.
Processing Internal Tables (with Header Line)
LOOP AT <itab>. ...ENDLOOP.
REPORT RSAAA06I.
TABLES: SFLIGHT.
DATA: BEGIN OF FLIGHTINFO_LIST OCCURS 10,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID,
FLDATE LIKE SFLIGHT-FLDATE,
PRICE LIKE SFLIGHT-PRICE,
END OF TAB.
SELECT * FROM SFLIGHT.
MOVE-CORRESPONDING SFLIGHT TO FLIGHTINFO_LIST.
APPEND FLIGHTINFO_LIST.
ENDSELECT.
LOOP AT FLIGHTINFO_LIST.
WRITE: / FLIGHTINFO_LIST-CARRID, FLIGHTINFO_LIST-CONNID...
ENDLOOP.
.
.
.
LOOP AT FLIGHTINFO_LIST WHERE CARRID = 'LH'.
WRITE: / FLIGHTINFO_LIST-CARRID, FLIGHTINFO_LIST-CONNID...
ENDLOOP.
IF SY-SUBRC NE 0.
.
.
ENDIF.
SAP AG
You use the loop statement LOOP AT ... ENDLOOP to process an internal table. Each time the
processing passes through the loop, the system places the next table entry in the header line. If you are
addressing table fields in the program, these are fields from the header line.
After WHERE, you can specify a logical expression (see the section on control statements in Chapter
4). If no table entry satisfies the logical expression and the loop is not processed, the system field SY-
SUBRC is set to a value other than 0. However, the table is read in its entirety.
When an internal table is being read, the system field SY-TABIX contains the index value of the table
entry currently in the header line.
Reading Entries of Internal Tables (with Header Line)
READ TABLE <itab> W ITH KEY ...
REPORT RSAAA06L.
TABLES: SFLIGHT.
DATA: BEGIN OF FLIGHTINFO_LIST
OCCURS 10,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID,
FLDATE LIKE SFLIGHT-FLDATE,
PRICE LIKE SFLIGHT-PRICE,
END OF FLIGHTINFO_LIST,
TAB_INDEX TYPE I.
SELECT * FROM...
MOVE-CORRESPONDING...
APPEND...
ENDSELECT.
.
READ TABLE FLIGHTINFO_LIST
WITH KEY 'LH 0400'.
IF SY-SUBRC NE 0.... ENDIF.
. = SY-TABIX
TAB_INDEX + 1.
IF SY-SUBRC
. NE 0.... ENDIF.
READ TABLE FLIGHTINFO_LIST
INDEX TAB_INDEX.
SAP AG
You use the READ TABLE <itab> WITH KEY... statement to read single table entries. After a read
access, the table entry appears in the header line. If the access was successful, the return code value of
the system field SY-SUBRC is 0. Otherwise, it is set to some other value.
The KEY parameter should be qualified by a search argument. ABAP/4 then compares the beginning
of each table entry with the search argument, which is usually a field string.
If the internal table is sorted by the search argument, you can use the addition ...BINARY SEARCH.
This performs a binary search for the entry and is more efficient than an ordinary search. If the internal
table contains more than one line identical to the argument, the READ commands returns the first
entry (even with the addition BINARY SEARCH).
READ TABLE <itab> INDEX <i>.
READ TABLE <itab> WITH KEY <k1> = <v1> <k2> = <v2>...<kn> = <vn>.
This statement reads the first entry from <itab> where the components specified with <k1>...<kn>
match <v1>...<vn>.
The system field SY-TABIX always contains the line number of the table entry just read.
For information about other additions, please read the online documentation for READ.
Changing Internal Tables (with Header Line) I
INSERT <itab> INDEX <i>.
M ODIFY <itab> INDEX <i>.
DELETE <itab> INDEX <i>.
REPORT RSAAA06M.
TABLES: SFLIGHT.
DATA: BEGIN OF FLIGHTINFO_LIST OCCURS 5,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID,
FLDATE LIKE SFLIGHT-FLDATE,
PRICE LIKE SFLIGHT-PRICE,
END OF TAB.
.
.
READ TABLE FLIGHTINFO_LIST WITH
KEY 'LH 0400'.
MOVE '345.89' TO FLIGHTINFO_LIST-PRICE.
MODIFY FLIGHTINFO_LIST INDEX SY-TABIX.
IF SY-SUBRC NE 0. ...ENDIF.
. .
INSERT FLIGHTINFO_LIST INDEX 1.
.
.
.
DELETE FLIGHTINFO_LIST INDEX 1.
IF SY-SUBRC NE 0. ...ENDIF.
.
.
SAP AG
The MODIFY <tab> INDEX <i> statement overwrites the table entry i with the contents of the header
line. The entry i must already exist.
The INSERT <tab> INDEX <i> statement uses the contents of the header line to generate a new table
entry before the entry i. If the table <i> has -1 entries, the contents of the header line are added to the
table.
The DELETE <tab> INDEX <i> statement deletes the table entry i.
Several additions are available with the DELETE statement (see also corresponding additions of the
LOOP statement). These include:
...WHERE <condition>. In this case, the DELETE statement is applied only to those table entries
which satisfy the condition.
...FROM <n1> TO <n2>. Here, all entries from <n1> to <n2> are deleted, including the upper and
lower limts of the range. If you specify only FROM or only TO in a DELETE statement, either all
entries from <n1> to the end are deleted, or all entries from the beginning to <n2>.
Changing Internal Tables (with Header Line) II
REPORT RSAAA06N.
.
.
.
LOOP AT FLIGHTINFO_LIST.
.
.
.
INSERT FLIGHTINFO_LIST.
.
.
.
MODIFY FLIGHTINFO_LIST.
.
.
.
DELETE FLIGHTINFO_LIST.
.
.
.
ENDLOOP.
SAP AG
You can make changes to an internal table in a loop. When doing this, you are always working on the
current line (as contained in SY-TABIX).
INSERT:
This statement uses the contents of the header line to insert a new entry before the current line.
MODIFY:
This statement overwrites the current line with the contents of the header line.
DELETE:
CLEAR <tab>.
REFRESH <tab>.
FREE <tab>.
SAP AG
The CLEAR <tab> statement initializes all single fields in the header line of an internal table
according to their type.
The REFRESH <tab> statement deletes all table entries. The memory space used by the table is not
released. The header line remains unchanged.
The FREE <tab> statement releases the memory space used by the table. The header line reamins
unchanged.
Information about Internal Tables
REPORT RSAAA06P.
TABLES: SFLIGHT.
DATA: BEGIN OF FLIGHTINFO_LIST OCCURS 10,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID,
FLDATE LIKE SFLIGHT-FLDATE,
PRICE LIKE SFLIGHT-PRICE,
END OF FLIGHTINFO_LIST,
LINE_COUNT TYPE I,
OCCURS_COUNT TYPE I.
SELECT *...
MOVE-CORRESPONDING...
APPEND...
ENDSELECT.
DESCRIBE TABLE FLIGHTINFO_LIST
LINES LINE_COUNT
OCCURS OCCURS_COUNT.
SAP AG
You can use the DESCRIBE statement to obtain information about an internal table.
The LINES parameter returns the number of existing table entries.
The OCCURS parameter returns the OCCURS value in the table definition.
Other parameters exist for the DESCRIBE statement (see the online documentation).
Declaring Internal Tables (without Header Line)
DATA: <itab> TYPE <type> OCCURS <n>.
REPORT RSAAA06O.
TYPES: BEGIN OF FLIGHTINFO_TYPE,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID,
FLDATE LIKE SFLIGHT-FLDATE,
PRICE LIKE SFLIGHT-PRICE,
END OF FLIGHTINFO_TYPE.
DATA: FLIGHT_LIST TYPE FLIGHTINFO_TYPE OCCURS 5,
LIST_WA TYPE FLIGHTINFO_TYPE.
.
.
.
without header
line !!!
SAP AG
From Release 3.0, you can define internal tables without a header line.
Tables without header lines consist of any number of identically structured table entries according to
the type <type> specified in the program.
You can use the OCCURS parameter to determine how many entries the table is to have initially, but
this value can be increased if necessary (with the exception of APPEND ...SORTED BY...).
You can also use the addition LIKE...OCCURS... to declare an internal table without a header line.
Example: DATA: FLIGHT_LIST LIKE SFLIGHT OCCURS 5.
If you want the internal table to have a header line, you can use the addition ...WITH HEADER LINE.
Example: DATA: FLIGHT_LIST LIKE SFLIGHT OCCURS 5 WITH HEADER LINE.
The DATA: <field> TYPE <type> OCCURS <n>. statement creates an internal table without a header
line where the structure consists of only one field.
If you want to address only the body of an internal table with a header line (i.e. without its header
line), you should use the expression <itab>[].
Example: ITAB_WITHOUT and ITAB_WITH are two internal tables without and with a header line.
The command ITAB_WITHOUT = ITAB_WITH[] assigns the body of the table ITAB_WITH to the
table ITAB_WITHOUT.
Filling Internal Tables (without Header Line)
SAP AG
The APPEND and COLLECT statements have the same effect on tables without header lines as on
tables with header lines.
Both take the contents of the line to be filled from an identically structured work area.
Reading Internal Tables (without Header Line)
SAP AG
Like internal tables with a header line, you can read internal tables without a header line using the
READ TABLE... statement or the LOOP...ENDLOOP statement.
The records read are placed in identically structured work areas.
The same applies to the MODIFY and INSERT statements:
MODIFY <itab> FROM <wa> INDEX <i>. modifies the i-th line of the internal table.
INSERT <wa> INTO <itab> INDEX <i>. inserts the record from the work area before the i-th line.
The CLEAR statement does not behave in the same way for internal tables with a header line and
internal tables without a header line:
When processing internal tables with a header line, it sets the fields in the header line to the
appropriate initial value for their type.
When processing internal tables without a header line, it deletes the whole table, similar to the
REFRESH statement.
Complex Structures I (Tables within Tables)
CARRID CONNID CREW
SAP AG
Using internal tables without a header line allows you to construct multi-dimensional internal tables.
Here, part of one internal table is constructed from components of another internal table. In the above
example, the field CREW in the first internal table comprises fields from another internal table with
the structure PERSNR, FUNCTION and FLIGHTS.
Complex Structures II
REPORT RSAAA06Q.
TYPES: BEGIN OF PERS_INFO,
PERSNR TYPE N,
FUNKTION(20),
FLIGHTS TYPE I,
END OF PERS_INFO.
DATA: CREW_WA TYPE PERS_INFO.
DATA: BEGIN OF FLIGHT_TABLE OCCURS 10,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID,
CREW TYPE PERS_INFO OCCURS 5,
END OF FLIGHT_TABLE.
CREW_WA-PERSNR = 459015. CREW_WA-FUNKTION = 'Pilot'.
CREW_WA-FLIGHTS = 1902. APPEND CREW_WA TO FLIGHT_TABLE_CREW.
.
.
.
FLIGHT_TABLE-CARRID = 'LH'. FLIGHT_TABLE-CONNID = '0400'.
APPEND FLIGHT_TABLE.
.
.
.
SAP AG
REPORT RSAAA06Q.
.
* Ausgabe
.
LOOP AT FLIGHT_TABLE.
WRITE: / FLIGHT_TABLE-CARRID,
FLIGHT_TABLE-CONNID.
LOOP AT FLIGHT_TABLE-CREW INTO CREW_WA.
SKIP.
WRITE: /20 CREW_WA-PERSNR,
35 CREW_WA-FUNKTION,
45 CREW_WA-FLIGHTS.
ENDLOOP.
ENDLOOP.
SAP AG
SAP AG
Exercises Chapter 6: Internal Tables
Task:
Determine for each airline carrier the 5 table entries with the
highest prices (as a ranked list).
To do this, read the table SFLIGHT and store the airline carrier
ID (CARRID), the flight connection code (CONNID), the flight
date (FLDATE) and the price (PRICE) in an internal table.
Then, output the internal table.
Allow the user to specify just one airline carrier which can be
entered as a parameter on the selection screen.
2. Name of your report: ZBCA##F2
##: Group number
Development class: $TMP (local)
Task:
Create a list of the maximum number of available seats for
each airline carrier.
To do this, read the table SFLIGHT and store the airline carrier
ID (CARRID) and the maximum number of seats (SEATSMAX)
in an internal table. Determine the total number of seats for
each airline carrier when filling the internal table.
When declaring the internal table, use the Dictionary structure
you defined in Chapter 3 (ZCA26#).
Then, display the internal table, but limit the output to records
where the total number of available seats is greater than
5,000.
3. Name of your report: ZBCA##F3
##: Group number
Development class: $TMP (local)
Task:
Read the table SFLIGHT into an internal table and then output
the internal table with the fields CARRID, CONNID, FLDATE
and PRICE.
Delete all the internal table entries where the airline carrier
(CARRID) is not equal to LH.
Read the internal table entry with the key LH 0402, multiply the
price by 3 and write the modified entry back to the internal
table.
Then, output the internal table.
Solutions Chapter 6: Internal Tables
1. REPORT RSAAA061.
PARAMETERS: FLUGG LIKE SFLIGHT-CARRID DEFAULT ´LH´
TABLES: SFLIGHT.
END OF FLIGHTINFO_LIST.
ENDSELECT.
LOOP AT FLIGHTINFO_LIST.
20 FLIGHTINFO_LIST-CONNID,
30 FLIGHTINFO_LIST-FLDATE,
40 FLEIGHTINFO_LIST-PRICE.
ENDLOOP.
2. REPORT RSAAA062.
TABLES: SFLIGHT.
COLLECT FLIGHTINFO_LIST.
ENDSELECT.
WRITE: / FLIGHTINFO_LIST-CARRID,
FLIGHTINFO_LIST-SEATSMAX.
ENDLOOP.
3. REPORT RSAAA063.
TABLES: SFLIGHT.
END OF FLIGHTINFO_LIST.
APPEND FLIGHTINFO_LIST.
ENDSELECT.
LOOP AT FLIGHTINFO_LIST.
WRITE: / 10 FLIGHTINFO_LIST-CARRID,
20 FLIGHTINFO_LIST-CONNID,
30 FLIGHTINFO_LIST-FLDATE,
40 FLIGHTINFO_LIST-PRICE.
ENDLOOP.
READ TABLE FLIGHTINFO_LIST WITH KEY ´LH 0402´
IF SY-SUBRC NE 0.
WRITE: / TEXT-001.
ELSE.
FLIGHTINFO_LIST-PRICE = FLIGHTINFO_LIST-PRCIE * 3.
ENDIF.
NEW-PAGE.
SKIP.
ULINE.
LOOP AT FLIGHTINFO_LIST.
WRITE: / 10 FLIGHTINFO_LIST-CARRID,
20 FLIGHTINFO_LIST-CONNID,
30 FLIGHTINFO_LIST-FLDATE,
40 FLIGHTINFO_LIST-PRICE.
ENDLOOP.