Tuesday, July 15, 2008

Defining and reading internal tables


1. Types of internal tables
2. Defining an internal table
3. Reading internal tables

1. Types of internal tables

1.1 STANDARD table

Key access to a standard table uses a linear search. This means that the time required for a search
is in linear relation to the number of table entries.

You should use index operations to access standard tables.

1.2 SORTED table

Defines the table as one that is always saved correctly sorted.

Key access to a sorted table uses a binary key. If the key is not unique, the system takes the entry
with the lowest index. The runtime required for key access is logarithmically related to the number
of table entries.


1.3 HASHED table


Defines the table as one that is managed with an internal hash procedure

You can only access a hashed table using the generic key operations or other generic operations (
SORT, LOOP, and so on). Explicit or implicit index operations (such as LOOP ... FROM oe INSERT itab
within a LOOP) are not allowed.

1.4 INDEX table

A table that can be accessed using an index.

Index table is only used to specify the type of generic parameters in a FORM or FUNCTION. That
means that you can't create a table of type INDEX.

Standard tables and sorted tables are index tables.


1.5 ANY table

Any table is only used to specify the type of generic parameters in a FORM or FUNCTION. That
means that you can't create a table of type ANY.

Standard, sorted and hashed tables belongs to ANY tables.

2. Defining an internal table

DATA itab TYPE table type of line type [WITH UNIQUE/NON-UNIQUE KEY ] [Iinitial size n]
[WITH HEADER LINE]

Note: There are also other ways to define an internal table. Please refere to the documentation.

2.1 The KEY option

KEY key1,,keyn :

key1..keyn are fields in the table. The sequence in which you specify the key is significant.

DEFAULT KEY :

The key fields are the standard keys. Note that you can only specify an empty key for tables with
access type STANDARD TABLE. The standard key basically comprises all tables fields with
character-like types (type ( C, STRING, D, T, N, X, XSTRING). In particular, components with a
numeric type (also refer to ABAP numeric types) and table components usually do not belong to the
standard key.

Example:

types:
begin of t_makt,
matnr like makt-matnr,
maktx like makt-maktx,
end of t_makt.

data:
* Define the table
gi_makt type sorted table of t_makt with unique key matnr.
* Define the work area for the table if necessary
gi_makt type t_makt.

3. Reading internal tables


READ TABLE itab WITH TABLE KEY k1 = v1 k2 = v2 [additions]

Note: In case of more than one match, it is the first match that is selected.


STANDARD TABLE: The system searches from the start of the table. The response time is in linear
relation to the number of table entries.

SORTED TABLE: The response time is in logarithmic relation to the number of table entries.

HASHED TABLE: The response time is constant

READ TABLE itab WITH KEY k1 = v1 k2 = v2 [BINARY SEARCH] [additions]

Note: In case of more than one match, it is the first match that is selected.


STANDARD TABLE: If you use the ... BINARY SEARCH addition, the system uses a binary search.
Otherwise, the search is sequential. This assumes that the internal table is sorted in ascending order
in the sequence of the specified key fields.

SORTED TABLE: If the specified key fields form a left-justified extract of the table key, the search is
binary, otherwise sequential.

HASHED TABLE: Sequential search.

READ TABLE itab INDEX i [additions]

Accessing the table entry with the index i.

Additions:


INTO wa - wa is used as output area

ASSIGNING - The field symbol is assigned to the entry. This saves the cost of copying the
contents in comparison to the first addition. However, this addition does involve table administration
costs, and it is therefore only worthwile for lines longer than around 300 bytes.

COMPARING f1...fn - If the system find an entry, the system compares the subfields f1, f2, ... with
the corresponding fields of the work area before they are transported into it.

COMPARING ALL FIELDS

TRANSPORTING f1 f2 - If the system finds an entry, it does not transfer all of the subfields (default)
into the work area, but only the specified fields f1 f2 ...; the other subfields remain unchanged.

TRANSPORTING NO FIELDS

Example:

loop at gi_mseg into g_mseg.
read table gi_makt
with table key matnr = g_mseg-matnr
into g_makt.
endloop.

No comments: