Sie sind auf Seite 1von 16

Creating Internal Tables

Like other elements in the ABAP type concept, you can declare internal tables as abstract data types in programs or in the ABAP Dictionary, and then use them to define data objects. Alternatively, you can define them directly as data objects. 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 minimum size of an internal table is 256 bytes. This is important if an internal table occurs as a component of an aggregated data object, since even empty internal tables within tables can lead to high memory usage. (In the next functional release, the size of the table header for an initial table will be reduced to 8 bytes). Unlike all other ABAP data objects, you do not have to specify the memory required for an internal table. Table rows are added to and deleted from the table dynamically at runtime by the various statements for adding and deleting records. An internal table is a collection of a variable number of records. They are used for temporary storage and exist or live only during the runtime of an ABAP/4 program. Internal tables and database tables work together. The contents of a database table can be copied to an internal table at runtime, so that the internal table is a snapshot of a database table and can then be used as a working copy of the data within the program. Internal tables are used as containers for volatile data in a program. TYPES: BEGIN OF struct1, col1 TYPE i, BEGIN OF struct2, col1 TYPE i, col2 TYPE i, END OF struct2, END OF struct1. TYPES mytype TYPE struct1-struct2-col2. The example shows how you can construct a nested structure type struct1 with a complex component struct2 by nesting TYPES BEGIN OF ... TYPES END OF blocks, and how you can address the inner components.

* Local types in program * referring to predefined ABAP types: TYPES: surname(20) TYPE c, street(30) TYPE c, zip_code(10) TYPE n, city(30) TYPE c, phone(20) TYPE n, date LIKE sy-datum. * Local structure in program * referring to the above types TYPES: BEGIN of address, name TYPE surname, code TYPE zip_code, town TYPE city,

str TYPE street, END OF address. * Local nested structure in program * referring to the above types TYPES: BEGIN of phone_list, adr TYPE address, tel TYPE phone, END OF phone_list. This example shows how to create complex data types from simple type definitions. After a set of simple data types are created with ABAP predefined types, a structured type address is defined using the data types defined earlier. Finally, a nested structure type, phone_list , is created, whose first component has the type address. TYPES: BEGIN OF struct1, col1 TYPE i, BEGIN OF struct2, col1 TYPE i, col2 TYPE i, END OF struct2, END OF struct1. TYPES mytype TYPE struct1-struct2-col2. The example shows how you can construct a nested structure type struct1 with a complex component struct2 by nesting TYPES BEGIN OF ... TYPES END OF blocks, and how you can address the inner components.

Comparison of Transparent, Pool and Cluster tables


By YSP, Defiance technologies Transparent Contain a single table. Used to store master data Pool Cluster They are used to hold a They are used to hold data large number of very small from a few number of large tables(stores customizing tables.(stores system data) data or system data) It has a one-to-one It has a many-to-one It has a many-to-one relationship with a table relationship with a table in relationship with table in in the database the database the database For each transparent It is stored with other Many cluster tables are table there is one pooled tables in a single stored in a single table in associated table in the table called table pool in the database called a table database the database cluster The database table has The database table has The database table has the same name, same different name, different different name, different number of fields and the number of fields and fields number of fields and fields fields have the same have different names have different names names There is only a single Table pools contain more Contains less tables than table tables than table clusters table pools Single table can have Primary key of each table Primary key of each table one or more primary key does not begin with same begins with same fields or fields or fields fields

Secondary indexes can be created They can be accessed using open and native SQL USE: They are used to hold master data e.g. Table vendors or table of customers. Example of transaction data is orders placed by customers

Secondary indexes cannot Secondary indexes cannot be created be created They can be accessed They can be accessed using open SQL only using open SQL only USE: They reduce the amount of database resources needed when many small tables have to be opened at the same time USE: They would be used when the tables have primary key in common and data in these tables are all accesses simultaneously

An internal table is a sequence of lines with the same type. If you want to define a table type, follow the line type declaration with the addition OCCURS 0. You can also write OCCURS <n> instead of OCCURS 0, where <n> is any integer.

Creating Internal Table Data Types To create an internal table data type, you use the TYPES statement as follows: Syntax TYPES <t> <type> OCCURS <n>. This creates an internal table data type <t> by using the OCCURS option of the TYPES statement. The lines of the internal table have the data type specified in <type>. To specify the data type of the lines, you can use either the TYPE or the LIKE parameter. By using the LIKE parameter to refer to an object defined in the ABAP/4 Dictionary, you can create internal tables which have the same line structure as objects stored in the Dictionary, and which reflect the structure of database tables. This is very important when reading and processing database tables <n> specifies an initial number of lines. Memory is reserved for the number of lines specified as soon as the first line is written to an internal table data object created with type <t>. If more lines are added to an internal table than specified by <n>, the reserved memory expands automatically. If there is not enough space in memory for an internal table, it is written to a buffer or to the disk (paging area).

TYPES VECTOR TYPE I OCCURS 10. This example creates an internal table data type VECTOR which has lines consisting of the elementary type I field. Creating Internal Tables by Referring to Another Table To create an internal table data object by referring to an existing internal table data type or data object, you use the DATA statement as follows:

Syntax

DATA <f> <type> [WITH HEADER LINE]. You can use the <type> option to refer to a table data type or table data object by using TYPE or LIKE . The data object <f> is declared as an internal table with the same structure. If you use the WITH HEADER LINE option, the internal table is created with a table work area <f>. If you want to create an internal table with a header line, the line type cannot directly be an internal table. However, it can be a structure which has internal tables as components. TYPES: BEGIN OF LINE, COLUMN1 TYPE I, COLUMN2 TYPE I, COLUMN3 TYPE I, END OF LINE. TYPES ITAB TYPE LINE OCCURS 10. DATA TAB1 TYPE ITAB. DATA TAB2 LIKE TAB1 WITH HEADER LINE. As shown in Creating Internal Table Data Types , this example creates a data type ITAB as an internal table. The data object TAB1 has the same structure as ITAB by referring to ITAB using the TYPE parameter of the DATA statement. The data object TAB2 has the same structure by referring to TAB1 using the LIKE parameter of the DATA statement. TAB2 is created with header line. Therefore, the table work area TAB2 can be addressed in the program by using TAB2-COLUMN1, TAB2-COLUMN2, and TAB2-COLUMN3. Creating Internal Tables by Referring to a Structure To create an internal table data object by referring to an existing line structure, you use the DATA statement as follows: Syntax DATA <f> <type> OCCURS <n> [WITH HEADER LINE]. This creates an internal table <f> by using the OCCURS option of the DATA statement. The lines of the internal table have the data type specified in <type>. To specify the data type, you can use either the TYPE or the LIKE parameter. By using the LIKE parameter to refer to an object defined in the ABAP/4 Dictionary, you can create internal tables which have the same line structure as objects stored in the Dictionary, and which reflect the structure of database tables. This is very important when reading and processing database tables <n> specifies an initial number of lines. Memory is reserved for the number of lines specified as soon as the first line is written to an internal table data object created with type <f>. If more lines are added to an internal table than specified by <n>, the reserved memory expands automatically. If there is not enough space in memory for an internal table, it is written to a buffer or to the disk (paging area). The features described above are the same as those for creating internal table data types with the TYPES statement.

As an additional feature, you can use the WITH HEADER LINE option with the DATA statement. This creates a table work area <f> with the same structure as the lines of the internal table <f>.

DATA FLIGHT_TAB LIKE SFLIGHT OCCURS 10. This example creates a data object FLIGHT_TAB which has the same structure as the database table SFLIGHT.

This example shows you how to create the same internal table using two different procedures. TYPES VECTOR_TYPE TYPE I OCCURS 10.DATA VECTOR TYPE VECTOR_TYPE WITH HEADER LINE. Here, an internal table data type VECTOR_TYPE is created with lines consisting of an elementary type I field is created first. Then, a data object VECTOR is created as an internal table by referring to VECTOR_TYPE. A table work area VECTOR is also created by using the WITH HEADER LINE option. In this case, the table work area consists of one type I field which can be addressed by the name VECTOR.

DATA VECTOR TYPE I OCCURS 10 WITH HEADER LINE. In this case, exactly the same data object VECTOR is created by using the OCCURS option directly in the DATA statement.

As you have already seen, internal tables (arrays or matrices in other terminology) are a set of lines with the same type(s). To define an internal table, simply use the addition OCCURS <n> when you declare a data object with the desired line type. The OCCURS addition turns the data object into an internal table with the same line type as that of the data object. It also determines the number of lines with which the internal table is created. Unlike the array concept of other programming languages, ABAP/4 can increase the number of lines in the table dynamically at runtime. If you do not know how big your internal table will be, set the OCCURS addition to 0. If you know that your internal table will be smaller than 8KB, specify the number of table lines using the OCCURS parameter. This ensures that only this amount of memory area is occupied. This is particularly important when you are working with nested structures. If the memory area is insufficient, further table lines are allocated.

Purpose of Internal Tables In ABAP/4, you work mainly with tables. Tables are the essential data structures in the R/3 System. Longlife data (also known as persistent data) is stored in relational database tables.

Besides database tables, you can create internal tables which exist only during the runtime of your program. ABAP/4 provides various operations for working with internal tables. You can, for example, search for, append, insert, or delete lines. The number of lines in an internal table is not fixed. Depending on requirements, the system increases the size of internal tables at runtime. If, for example, you want to read a database table into an internal table, you do not have to know the size of the database table in advance. This feature makes working with internal tables an easy task and also supports dynamic programming. You can use internal tables to perform table calculations on subsets of database tables. For example, you can read a certain part of a database table into an internal table. From the internal table, you can then calculate totals or generate a ranked list. Another use for internal tables is reorganizing the contents of database tables according to the needs of your program. For example, you can read data relevant for creating a telephone list from one or several large customer tables into an internal table. During the runtime of your program, you can then access this list directly without having to perform a time-consuming database query for each call. Besides using them when working with data from database tables, internal tables are an important feature in ABAP/4 for implementing very complex data structures in your program. Structure of Internal Tables In ABAP/4, you can distinguish between internal table data types, which define the structure of internal tables, and internal table data objects, which are the actual internal tables and can be filled with data. An internal table data type is an abstract definition of a data structure which can be used to declare data objects as internal tables. Data type An internal table is one of the two structured data types in ABAP/4. The other structured data type is the field string. An internal table consists of any number of lines which all have the same data type. The data type of the lines can be elementary or structured. This definition opens a variety of internal table structures which range from lines consisting of one field to lines consisting of field strings which have internal tables as components. You can define a data type as an internal table by using the TYPES statement with the OCCURS parameter. No memory is occupied when defining a data type. Data object A data object which has a data type defined as an internal table is the actual internal table you work with. It occupies memory and you can fill or read its lines. You create a data object as an internal table by using the DATA statement either with the OCCURS parameter or by referring to another internal table by using the TYPE or LIKE parameters.

Working with Internal Tables In contrast with the array concepts found in other programming languages, ABAP/4 does NOT use direct access to table entries. Instead, operations on table entries are carried out using a work area. This is a temporary holding area that contains the content of the current entry for almost ALL commands dealing with the table. This work space may be either a separately defined work area or a header line that is defined when the table is defined.

ABAP/4 recognizes essentially the following operations on internal tables: Command APPEND COLLECT INSERT MODIFY DELETE LOOP AT SORT Effect Appends the contents of the work area at the end of the internal table Inserts the cumulative contents of the work area into the internal table Inserts the contents of the work area into a particular table entry Overwrites a particular table entry with the content of the work area Deletes a specified entry from the internal table Places the entries of an internal table in a work area one at a time Sorts the internal table

READ TABLE Places a single internal table entry in a work area

CLEAR REFRESH FREE

Deletes the work area or the internal table Deletes the internal table Releases space in memory previously occupied by table

Internal Tables With Header Line You access internal tables record by record. You must use a work area as an interface for transferring data to and from the table. When you read data from an internal table, the contents of a specified table record or line overwrites the contents of the work area. Then, you can reference the contents of the work area in your program. When you write data to an internal table, you must first enter the data in the work area from with the system can transfer the data to the internal table.

To avoid inconsistencies, it is beneficial if the work area has the same data type as the records or lines of the internal table. A safe procedure for creating work areas which are compatible with internal tables is to use the same data type for declaring both the internal table and the work area. In ABAP/4, you can distinguish between two kinds of internal tables: Internal tables WITH header lines Internal tables WITHOUT header lines A header line is similar to a work area for a database table. A work area is used as temporary storage for one entry of a database table. In a similar way, a header line is used to hold one line of an internal table. An internal table with header line is a tuple from a work area (header line) and the bulk of the table itself. Both are addressed using the same name, the interpretation of the name is context-sensitive. Hence it would stand for the header line in a MOVE statement, but would stand for the bulk of the table in a SEARCH statement. When you create an internal table WITH a header line, a work area is created automatically with the same data type as the rows of the internal table. The header line and the internal table have the same name. Then, the system uses this work area implicitly. In ABAP/4 statements for accessing internal tables, you can specify the work area to be used. For internal tables with header lines, you can leave out this specification. Then, the system uses the table work area implicitly.

Internal Tables Without Header Line If a table does not have a header line, you MUST provide a work area as a separate record to hold the content of the current entry for most commands used in processing tables. Internal tables WITHOUT a header line do NOT have a table work area declared which can be used implicitly. To access internal tables WITHOUT header lines, you must specify a work area explicitly in the corresponding ABAP/4 statements. It is a matter of your preference whether you use a header line or a work area. Remember that, for internal tables with header lines, the internal table itself and the table work areas have the same name. If you use the name in a statement, the system interprets it as the name of the table work area and NOT as the table itself. In some statement, however, you can enter square brackets after the name to address the internal table instead of the table work area as follows: <name> [ ].

Declaring Internal Tables with Header Line You define an internal table with a header line using the addition WITH HEADER LINE. A HEADER line is a work area where the currently active, individual record in the table resides.

Using Loops with an Internal Table You can process an internal table using the loop statement LOOP AT ... ENDLOOP. With each loop pass the system places the next table entry in the work area <wa> or the header line of the internal table <itab>. You can restrict the entries which are read using the WHERE addition in the same way as you do when using the SELECT command. At the beginning of each loop pass, SY-TABIX is set to the value of the current table entry. When the system leaves the LOOP, SY-TABIX has the same value as it had before the loop started.

Reading an Entry from an Internal Table You use the READ TABLE <itab> statement to read a single table entry. When the entry has been read, it is in the work area <wa> or the header line of <itab>. If the entry is successfully read, the value of SY-SUBRC is zero, otherwise it is not equal to zero. SYTABIX takes the value of the table entry read. READ TABLE <itab> INDEX <n>. The nth table entry is read. If you wish to specify individual fields as a search argument you can use the following syntax: READ TABLE <itab> WITH KEY <ki> = <v1> <k2> = <v2>...<kn> = <vn>. In this case, the first entry from <itab> is read which corresponds with the components specified in <k1>...<kn>. If the internal table is sorted by the search argument, you can use the BINARY SEARCH addition. The system then carries out a more performance-efficient binary search for the specified entry. The online documentation for the READ statement contains details of further additions.

Information about Internal Tables You can get information about an internal table using the DESCRIBE TABLE statement: The LINES addition returns the number of entries currently in the table. The OCCURS addition returns the number of OCCURS in the table definition.

You can display information about any data object using the DESCRIBE FIELD statement (see the online documentation for the DESCRIBE statement).

Das könnte Ihnen auch gefallen