Tuesday, November 18, 2008

Defining and Reading Internal Tables

Data types Type Description Byte
C Text (character) 1
N Numeric text 1
D Date (YYYYMMDD) 8
T Time (HHMMSS) 6
X Hexadecimal 1
I Integer 4
P Packed number 8
F Floating point no. 8


Defining and Reading Internal Tables

1. Types of internal tables

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.

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.

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.

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.

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 refer to the documentation.

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.

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

No comments: