Sie sind auf Seite 1von 7

Internal Tables Complete Doc

Internal tables provide a means of taking data from a fixed structure and storing it in
working memory in ABAP.
The data is stored line by line in memory, and each line has the same structure.
In ABAP, internal tables fulfill the function of arrays. Since they are dynamic data objects,
they save the programmer the task of dynamic memory management in his or her
programs.
You should use internal tables whenever you want to process a dataset with a fixed
structure within a program.
A particularly important use for internal tables is for storing and formatting data from a
database table within a program.
They are also a good way of including very complicated data structures in an ABAP
program.
Data Type of an Internal Table
The data type of an internal table is fully specified by its line type, key, and table type.
Line Type
The line type of an internal table can be any data type. The data type of an internal table
is normally a structure.
Each component of the structure is a column in the internal table. However, the line type
may also be elementary or another internal table.
Key
The key identifies table rows. There are two kinds of key for internal tables - the standard
key and a user-defined key. You can specify whether the key should be UNIQUE or NONUNIQUE. Internal tables with a unique key cannot contain duplicate entries. The
uniqueness depends on the table access method.
At tables with structured row type, the standard key is formed from all character-type
columns of the internal table. If a table has an elementary line type, the default key is
the entire line. The default key of an internal table whose line type is an internal table,
the default key is empty. At tables with non-structured row type, the standard key
consists of the entire row. If the row type is also a table, an empty key is defined.
The user-defined key can contain any columns of the internal table that are no internal
table themselves, and do not contain internal tables. References are allowed as table
keys. Internal tables with a user-defined key are called key tables. When you define the
key, the sequence of the key fields is significant. You should remember this, for example,
if you intend to sort the table according to the key.
Table type
The table type determines how ABAP will access individual table entries. Internal tables
can be divided into three types:
Standard tables have an internal linear index. From a particular size upwards, the
indexes of internal tables are administered as trees. In this case, the index
administration overhead increases in logarithmic and not linear relation to the number of
lines. The system can access records either by using the table index or the key. The
response time for key access is proportional to the number of entries in the table. The
key of a standard table is always non-unique. You cannot specify a unique key. This
means that standard tables can always be filled very quickly, since the system does not
have to check whether there are already existing entries.
Sorted tables are always saved sorted by the key. They also have an internal index. The
system can access records either by using the table index or the key. The response time
for key access is logarithmically proportional to the number of table entries, since the
system uses a binary search. The key of a sorted table can be either unique or nonunique. When you define the table, you must specify whether the key is to be UNIQUE

or NON-UNIQUE. Standard tables and sorted tables are known generically as index
tables.
Hashed tables have no linear index. You can only access a hashed table using its key.
The response time is independent of the number of table entries, and is constant, since
the system accesses the table entries using a hash algorithm. The key of a hashed table
must be unique. When you define the table, you must specify the key as UNIQUE.
Generic Internal Tables
Unlike other local data types in programs, you do not have to specify the data type of an
internal table fully. Instead, you can specify a generic construction, that is, the key or key
and line type of an internal table data type may remain unspecified. You can use generic
internal tables to specify the types of field symbols and the interface parameters of
procedures . You cannot use them to declare data objects.
Internal Tables as Dynamic Data Objects

Internal tables are always completely specified regarding row type, key and access type.
However, the number of lines is not fixed. Thus internal tables are dynamic data objects,
since they can contain any number of lines of a particular type. The only restriction on
the number of lines an internal table may contain are the limits of your system
installation. The maximum memory that can be occupied by an internal table (including
its internal administration) is 2 gigabytes. A more realistic figure is up to 500 megabytes.
An additional restriction for hashed tables is that they may not contain more than 2
million entries. The line types of internal tables can be any ABAP data types elementary, structured, or internal tables. The individual lines of an internal table are
called table lines or table entries. Each component of a structured line is called a column
in the internal table.
Choosing a Table Type
The table type (and particularly the access method) that you will use depends on how the typical
internal table operations will be most frequently executed.
Standard tables
This is the most appropriate type if you are going to address the individual table entries using
the index. Index access is the quickest possible access. You should fill a standard table by
appending lines (ABAP APPEND statement), and read, modify and delete entries by specifying
the index (INDEX option with the relevant ABAP command). The access time for a standard table
increases in a linear relationship with the number of table entries. If you need key access,
standard tables are particularly useful if you can fill and process the table in separate steps. For
example, you could fill the table by appending entries, and then sort it. If you use the binary
search option (BINARY) with key access, the response time is logarithmically proportional to the
number of table entries.
Sorted tables
This is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted
tables using the INSERT statement. Entries are inserted according to the sort sequence defined
through the table key. Any illegal entries are recognized as soon as you try to add them to the
table. The response time for key access is logarithmically proportional to the number of table
entries, since the system always uses a binary search. Sorted tables are particularly useful for

partially sequential processing in a LOOP if you specify the beginning of the table key in the
WHERE condition.
Hashed tables
This is the most appropriate type for any table where the main operation is key access. You
cannot access a hashed table using its index. The response time for key access remains
constant, regardless of the number of table entries. Like database tables, hashed tables always
have a unique key. Hashed tables are useful if you want to construct and use an internal table
which resembles a database table or for processing large amounts of data.

Creating Internal Tables


You define internal tables first as an abstract data type in the program or ABAP
Dictionary, and then as a data object based on that, or they are declared directly as a
fully specified data object. When you create an internal table as a data object, you
should ensure that only the administration entry which belongs to an internal table is
declared statically. The size of table headers for initial tables is currently 8 bytes. This
should be heeded whenever internal tables occur as components of complex data
objects. Also, empty tables can use up a relatively high amount of storage space as
components of tables. The size of the entire storage space required for an internal table
is not defined in the declaration as is the case for data objects of the type string or
xstring. Table rows are added to and deleted from the table dynamically at runtime by
the various statements for adding and deleting records.
Table Types
Internal Tables
Special Aspects of Standard Tables

Table types
This section describes how to define internal tables locally in a program. You can also define
internal tables globally as data types in the ABAP Dictionary.
Like all local data types in programs , you define internal tables using the TYPES statement. If
you do not refer to an existing table type using the TYPE or LIKE addition, you can use the
TYPES statement to construct a new local internal table in your program.
TYPES type TYPE|LIKE tabkind OF linetype [WITH key]
[INITIAL SIZE n].

After TYPE or LIKE, there is no reference to an existing data type. Instead, the type constructor
occurs:

tabkind OF linetype [WITH key]


The type constructor defines the table type tabkind, the line type linetype, and the key key of
the internal table type.
You can, if you wish, allocate an initial amount of memory to the internal table using the INITIAL
SIZE addition.
Table type
You can specify the table type tabkind as follows:

Generic table types


INDEX TABLE

For creating a generic table type with index access.


ANY TABLE

For creating a fully-generic table type.


Data types defined using generic types can currently only be used for field symbols and for
interface parameters in procedures . The generic type INDEX TABLEincludes standard tables
and sorted tables. These are the two table types for which index access is allowed. You cannot
pass hashed tables to field symbols or interface parameters defined in this way. The generic type
ANY TABLE can represent any table. You can pass tables of all three types to field symbols and
interface parameters defined in this way. However, these field symbols and parameters will then
only allow operations that are possible for all tables, that is, index operations are not allowed.

Fully-Specified Table Types


STANDARD TABLE or TABLE

For creating standard tables.


SORTED TABLE

For creating sorted tables.


HASHED TABLE

For creating hashed tables.


Fully-specified table types determine how the system will access the entries in the table in key
operations. It uses a linear search for standard tables, a binary search for sorted tables, and a
search using a hash algorithm for hashed tables.
Line Type
For the line type linetype, you can specify:

Any data type if you are using the TYPE addition. This can be a predefined ABAP type, a local type in the
program, or a data type from the ABAP Dictionary. If you specify any of the generic elementary types c, n, p
and x , any attributes that you fail to specify (field length, number of decimal places) are automatically filled
with the default values. You cannot specify any other generic types.
Any data object recognized within the program at that point if you are using the LIKE addition. The line type
adopts the fully-specified data type of the data object to which you refer. Except for within classes, you can still
use the LIKE addition to refer to database tables and structures in the ABAP Dictionary (for compatibility
reasons).
All of the lines in the internal table have the fully-specified technical attributes of the specified
data type.
Key
You can specify the key key of an internal table as follows:
[UNIQUE|NON-UNIQUE] KEY col1 ... coln

In tables with a structured line type, all of the components col1 coln belong to the key as long as they are not
internal tables or references, and do not contain internal tables or references. Key fields can be nested structures.
The substructures are expanded component by component when you access the table using the key. The system
follows the sequence of the key fields.
[UNIQUE|NON-UNIQUE] KEY table_line

If a table has an elementary line type (c, d, f, i, n, p, t, x), you can define the entire line as the key. If you try
this for a table whose line type is itself a table, a syntax error occurs. If a table has a structured line type, it is
possible to specify the entire line as the key. However, you should remember that this is often not suitable.
[UNIQUE|NON-UNIQUE] DEFAULT KEY

This declares the fields of the default key as the key fields. If you have a structured line type, the standard key is
build from all columns of the internal table that have a character-type type (c, d, t, n, x, string, xstring).
Internal tables with a nested row structure have a standard key built by linearization of the row structure. At
elementary row types, the standard key is the row itself. Elementary tables with an internal table as row type
have an empty standard key.
Specifying a key is optional. If you do not specify a key, the system defines a table type with an
arbitrary key. You can only use this to define the types of field symbols and the interface
parameters of procedures . For exceptions, refer to Special Features of Standard Tables).
The optional additions UNIQUE or NON-UNIQUE determine whether the key is to be unique or
non-unique, that is, whether the table can accept duplicate entries. If you do not specify UNIQUE
or NON-UNIQUE for the key, the table type is generic in this respect. As such, it can only be
used for specifying types. When you specify the table type simultaneously, you must note the
following restrictions:

You cannot use the UNIQUE addition for standard tables. The system always generates the NON-UNIQUE
addition automatically.
You must always specify the UNIQUE option when you create a hashed table.
Initial Memory Requirement
You can specify the initial amount of main memory assigned to an internal table object when you
define the data type using the following addition:
INITIAL SIZE n
. This size does not belong to the data type of the internal table, and does not affect the type
check. You can use the above addition to reserve memory space for n table lines when you
declare the table object.
When this initial area is full, the system makes twice as much extra space available up to a limit
of 8KB. Further memory areas of 12KB each are then allocated.
You can usually leave it to the system to work out the initial memory requirement. The first time
you fill the table, little memory is used. The space occupied, depending on the line width, is 16
<= n<= 100.
It only makes sense to specify a concrete value of nif you can specify a precise number of table
entries when you create the table and need to allocate exactly that amount of memory
(exception: Appending table lines to ranked lists). This can be particularly important for deepstructured internal tables where the inner table only has a few entries (less than 5, for example).

To avoid excessive memory requests, large values for nare treated as follows: The possible
maximum value for n results from 8 kilobyte divided by the row length. If you specify a larger
value of n, the system calculates a new value so that ntimes the line width is around 12KB.

TYPES: BEGIN OF line,


column1 TYPE i,
column2 TYPE i,
column3 TYPE i,
END OF LINE.
TYPES itab TYPE SORTED TABLE OF line WITH UNIQUE KEY column1.
The program defines a table type itab. It is a sorted table, with line type of the structure line and a unique key of
the component column1.

TYPES vector TYPE HASHED TABLE OF i WITH UNIQUE KEY table_line.


TYPES: BEGIN OF line,
column1 TYPE i,
column2 TYPE i,
column3 TYPE i,
END OF line.
TYPES itab TYPE SORTED TABLE OF line WITH UNIQUE KEY column1.
TYPES: BEGIN OF deepline,
field TYPE c,
table1 TYPE vector,
table2 TYPE itab,
END OF deepline.
TYPES deeptable TYPE STANDARD TABLE OF deepline
WITH DEFAULT KEY.
The program defines a table type vector with type hashed table, the elementary line type i and a unique key of
the entire table line. The second table type is the same as in the previous example. The structure deepline
contains the internal table as a component. The table type deeptable has the row type deepline. Therefore, the
elements of this internal table are themselves internal tables. The key is the default key - in this case the column
field. The key is non-unique, since the table is a standard table.

Das könnte Ihnen auch gefallen