Sunteți pe pagina 1din 31

Chapter 06 Internal Tables

Field strings
Internal tables with header line
Internal tables without header line

SAP AG
Chapter 06 Objectives

W orking with field strings, internal tables with a


header line and internal tables without a header line
Getting to know complex structures

SAP AG
Field Strings / Internal Tables (with Header Line)

Field string

FLIGHT DATA
Carrier Connection Date Price

Internal tables

FLIGHT DATA LIST


Carrier Connection Date Price
header line

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.

DATA: AMOUNT(5) TYPE P VALUE '563.12',


BEGIN OF FLIGHTINFO,
FLAG TYPE C,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID,
FLDATE LIKE SFLIGHT-FLDATE,
PRICE LIKE SFLIGHT-PRICE,
END OF FLIGHTINFO.
.
.
.
MOVE 'X' TO FLIGHTINFO-FLAG.
ADD AMOUNT TO FLIGHTINFO-PRICE.
.
.
.
WRITE: / FLIGHTINFO-FLAG, FLIGHTINFO-CARRID,
FLIGHTINFO-CONNID,FLIGHTINFO-FLDATE.
SAP AG

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:

<field string>-<field name>.


You can, of course, address a field string in its entirety (e.g. WRITE: <field string>.). However, it is
then treated as a type C field, i.e. no type conversions are performed. In the above example, the field
string contains a packed field (FLIGHTINFO-PRICE). In this case, a statement such as WRITE
FLIGHTINFO would not return any meaningful output.
Transporting Values Field by Field
MOVE-CORRESPONDING <f1> TO <f2>.
REPORT RSAAA06B.
TABLES: SFLIGHT.
DATA: BEGIN OF FLIGHTINFO,
FLAG TYPE C,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID,
FLDATE LIKE SFLIGHT-FLDATE,
PRICE LIKE SFLIGHT-PRICE,
END OF FLIGHTINFO.
SELECT * FROM SFLIGHT WHERE...
MOVE-CORRESPONDING SFLIGHT TO FLIGHTINFO.
ENDSELECT.
SFLIGHT
CARRID CONNID FLDATE
LH 2407 19951029
MOVE-CORRESPONDING SFLIGHT TO FLIGHTINFO.
FLIGHTINFO
FLAG CARRID CO NNID FLDA TE
LH 2407 19951029

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:

MOVE <f1>-<field name> TO <f2>-<field name>.

The other fields remain unchanged.


If the data structures f1 and f2 are the same and all the sub-fields match in terms of sequence and
attributes, you can also use the MOVE <f1> TO <f2> statement which does not require as much
runtime.
The CLEAR <f> statement assigns the correct initial values to all the sub-fields of f, depending on
their type.
Declaring Internal Tables (with Header Line)
DATA: BEGIN OF <tab> OCCURS <n>, ...,
END OF <tab>.
REPORT RSAAA06C.
TABLES: SFLIGHT.
DATA: AMOUNT(5) TYPE P,
BEGIN OF FLIGHTINFO_LIST OCCURS 5,
FLAG TYPE C,
CARRID LIKE SFLIGHT-CARRID,
CONNID LIKE SFLIGHT-CONNID,
FLDATE LIKE SFLIGHT-FLDATE,
PRICE LIKE SFLIGHT-PRICE,
END OF FLIGHTINFO_LIST.
.
.

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.

Table structure SFLIG HT

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

APPEND <itab>. APPEND <itab> SORTED BY <field>.

APPEND FLIGHTINFO _LIST. APPEND FLIGHTINFO _LIST SORTED BY PRICE.

CA RRID CONN ID PRICE CA RRID CONN ID PRICE

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.

SELECT * FROM SFLIGHT.


MOVE-CORRESPONDING SFLIGHT TO
FLIGHTINFO_LIST.
APPEND 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)

COLLECT <itab>. REPORT RSAAA06G.

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>.

This statement reads the i-th table entry.


If you want to specify single fields explicitly as a search argument, you can use the following syntax:

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:

This statement deletes the current line.


Deleting Internal Tables (with Header Line)

CLEAR <tab>.

Initialize header line

REFRESH <tab>.

Delete all table entries


M emory space remains occupied

FREE <tab>.

Delete all table entries


M emory space is released

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

DESCRIBE TABLE <itab>... .

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.
.
.
.

CARRID CONNID FLDATE PRICE

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)

APPEND <wa> TO <itab>.

APPEND LIST_W A TO FLIGHT_LIST.

CARRID CONNID PRICE

AA 0064 89000 work area

without header AA 0064 12300


line !!! AA 0064 66667
DL 1699 66667
LH 0400 100000
LH 0402 100000
UA 0007 100000

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)

READ TABLE <itab> INTO <wa> W ITH KEY... .

LOOP AT <itab> INTO <wa>.....ENDLOOP.

CA RRID CONN ID PRICE

LH 0402 100000 work area

without header AA 0064 12300


line !!! AA 0064 66667
DL 1699 66667
LH 0400 100000
LH 0402 100000
UA 0007 100000

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

PERSNR FUNCTION FLIGHTS

459015 Pilot 1902


900056 Copilot 1177
LH 0400
600412 Navigator 1204
300912 Stewardess 2078
... ....... .....
... ....... .....
AA 0623
... ....... .....
... ....... .....

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

Here, the TYPE statement defines the structure type PERS_INFO.


FLIGHT_TABLE is an internal table with a header line and its third component is an internal table
without a header line. The structure of the latter internal table is the same as the structure type
PERS_INFO and is addressed via FLIGHT_TABLE-CREW. The work area for this table is
CREW_WA. When defining this work area, you refer to the same structure type and thus ensure that it
has the same structure as the internal table.
Complex Structures III

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

To output these complex structures, you use nested LOOP statements:


The LOOP AT FLIGHT_TABLE.... ENDLOOP. statement places the individual entries of the table
FLIGHT_TABLE in the header line of this table.
The fields CARRID and CONNID are single fields and can therefore be output directly.
The third component of FLIGHT_TABLE is an internal table output by a LOOP. However, since this
table has no header line, it is output in the work area CREW_WA.
Chapter 06 Summary

Internal tables are important data structures in the R/3


System. They are used to store data required during the
runtime of a program.
ABAP/4 has a wide range of statements for processing
internal tables.
Internal tables allow you to reproduce very complex data
structures in ABAP/4 programs.

SAP AG
Exercises Chapter 6: Internal Tables

1. Name of your report: ZBCA##F1


##: Group number
Development class: $TMP (local)

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.

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 FLIGHTINFO_LIST.

SELECT * FROM SFLIGTHT WHERE CARRID = FLUGG.

MOVE-CORRESPONDING SFLIGHT TO FLIGHTINFO_LIST.

APPEND FLIGHTINFO_LIST SORTED BY PRICE.

ENDSELECT.

LOOP AT FLIGHTINFO_LIST.

WRITE: /10 FLIGHTINFO_LIST-CARRID,

20 FLIGHTINFO_LIST-CONNID,

30 FLIGHTINFO_LIST-FLDATE,

40 FLEIGHTINFO_LIST-PRICE.

ENDLOOP.
2. REPORT RSAAA062.
TABLES: SFLIGHT.

DATA: BEGIN OF FLIGHTINFO_LIST OCCURS 10,

INCLUDE STRUCTURE BCAXX.

DATA: END OF FLIGHTINFO_LIST.

SELECT * FROM SFLIGHT.

MOVE-CORRESPONDING SFLIGHT TO FLIGHTINFO_LIST.

COLLECT FLIGHTINFO_LIST.

ENDSELECT.

LOOP AT FLIGHTINFO_LIST WHERE SEATSMAX GT 5000.

WRITE: / FLIGHTINFO_LIST-CARRID,

FLIGHTINFO_LIST-SEATSMAX.

ENDLOOP.
3. REPORT RSAAA063.
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 FLIGHTINFO_LIST.

SELECT * FROM SFLIGTHT.

MOVE-CORRESPONDING SFLIGHT TO 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.

MODIFY FLIGHTINFO_LIST INDEX SY-TABIX.

ENDIF.

NEW-PAGE.

SKIP.

WRITE: / TEXT 002.

ULINE.

LOOP AT FLIGHTINFO_LIST.

WRITE: / 10 FLIGHTINFO_LIST-CARRID,

20 FLIGHTINFO_LIST-CONNID,

30 FLIGHTINFO_LIST-FLDATE,

40 FLIGHTINFO_LIST-PRICE.

ENDLOOP.

S-ar putea să vă placă și