Sie sind auf Seite 1von 9

Internal tables

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.

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.

3. On the dialog box, mark the radio button Internal table.

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.

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

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.

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.

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.

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

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.

Internal table declaration - various ABAP


code to implement an internal table

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 .

* Table declaration (old method)


DATA: BEGIN OF tab_ekpo OCCURS 0, "itab with header line
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
END OF tab_ekpo.

*Table declaration (new method) "USE THIS WAY!!!


TYPES: BEGIN OF t_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
END OF t_ekpo.
DATA: it_ekpo TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0, "itab
wa_ekpo TYPE t_ekpo. "work area (header line)

* 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)

SELECT command - Example ABAP code to


demonstrate the SELECT command

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.

*Code to demonstrate select command


*Code to demonstrate select into internal table command
TYPES: BEGIN OF t_bkpf,
* include structure bkpf.
bukrs LIKE bkpf-bukrs,
belnr LIKE bkpf-belnr,
gjahr LIKE bkpf-gjahr,
bldat LIKE bkpf-bldat,
monat LIKE bkpf-monat,
budat LIKE bkpf-budat,
xblnr LIKE bkpf-xblnr,
awtyp LIKE bkpf-awtyp,
awkey LIKE bkpf-awkey,
END OF t_bkpf.
DATA: it_bkpf TYPE STANDARD TABLE OF t_bkpf INITIAL SIZE 0,
wa_bkpf TYPE t_bkpf.

TYPES: BEGIN OF t_bseg,


*include structure bseg.
bukrs LIKE bseg-bukrs,
belnr LIKE bseg-belnr,
gjahr LIKE bseg-gjahr,
buzei LIKE bseg-buzei,
mwskz LIKE bseg-mwskz, "Tax code
umsks LIKE bseg-umsks, "Special G/L transaction type
prctr LIKE bseg-prctr, "Profit Centre
hkont LIKE bseg-hkont, "G/L account
xauto LIKE bseg-xauto,
koart LIKE bseg-koart,
dmbtr LIKE bseg-dmbtr,
mwart LIKE bseg-mwart,
hwbas LIKE bseg-hwbas,
aufnr LIKE bseg-aufnr,
projk LIKE bseg-projk,
shkzg LIKE bseg-shkzg,
kokrs LIKE bseg-kokrs,
END OF t_bseg.
DATA: it_bseg TYPE STANDARD TABLE OF t_bseg INITIAL SIZE 0,
wa_bseg TYPE t_bseg.

*Select directly into an 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.

* Select directly into an internal table where fields are in a


* different order or not all fields are specified
SELECT bukrs belnr gjahr buzei mwskz umsks prctr hkont xauto koart
dmbtr mwart hwbas aufnr projk shkzg kokrs
FROM bseg
INTO CORRESPONDING FIELDS OF TABLE it_bseg.

*Select... endselect command


SELECT bukrs belnr gjahr buzei mwskz umsks prctr hkont xauto koart
dmbtr mwart hwbas aufnr projk shkzg kokrs
FROM bseg
INTO wa_bseg.

APPEND wa_bseg TO it_bseg.


ENDSELECT.

*Select FOR ALL ENTRIES command


SELECT bukrs belnr gjahr bldat monat budat xblnr awtyp awkey
UP TO 100 ROWS
FROM bkpf
INTO TABLE it_bkpf.

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.

Das könnte Ihnen auch gefallen