1.31 SAP ABAP - Creating Internal Tables

«« Previous
Next »»

DATA statement is used to declare an internal table. The program must be told where the table begins and ends. So use the BEGIN OF statement and then declare the table name. After this, the OCCURS addition is used, followed by a number, here 0. OCCURS tells SAP that an internal table is being created, and the 0 states that it will not contain any records initially. It will then expand as it is filled with data.

Following is the syntax:

DATA: BEGIN OF <internal_tab> Occurs 0,

Let’s create the fields on a new line. For instance, create ‘name’ which is declared as LIKE ZCUSTOMERS1-name. Create another field called ‘dob’, LIKE ZCUSTOMERS1-dob. It is useful initially to give the field names in internal tables the same names as other fields that have been created elsewhere. Finally, declare the end of the internal table with “END OF <internal_tab>.” as shown in the following code −

DATA: BEGIN OF itab01 Occurs 0,
   name LIKE ZCUSTOMERS1-name,
   dob LIKE ZCUSTOMERS1-dob,
END OF itab01.

Here ‘itab01’ is commonly used shorthand when creating temporary tables in SAP. The OCCURS clause is used to define the body of an internal table by declaring the fields for the table. When the OCCURS clause is used, you can specify a numeric constant ‘n’ to determine additional default memory if required. The default size of memory that is used by the OCCUR 0 clause is 8 KB. The structure of the internal table is now created, and the code can be written to fill it with records.

An internal table can be created with or without using a header line. To create an internal table with a header line, use either the BEGIN OF clause before the OCCURS clause or the WITH HEADER LINE clause after the OCCURS clause in the definition of the internal table. To create an internal table without a header line, use the OCCURS clause without the BEGIN OF clause.

You can also create an internal table as a local data type (a data type used only in the context of the current program) by using the TYPES statement. This statement uses the TYPE or LIKE clause to refer to an existing table.

The syntax to create an internal table as a local data type is:

TYPES <internal_tab> TYPE|LIKE <internal_tab_type> OF
   <line_type_itab> WITH <key> INITIAL SIZE <size_number>.

Here the <internal_tab_type> specifies a table type for an internal table <internal_tab> and <line_type_itab> specifies the type for a line of an internal table. In TYPES statement, you can use the TYPE clause to specify the line type of an internal table as a data type and LIKE clause to specify the line type as a data object. Specifying a key for an internal table is optional and if the user does not specify a key, the SAP system defines a table type with an arbitrary key.

INITIAL SIZE <size_number> creates an internal table object by allocating an initial amount of memory to it. In the preceding syntax, the INITIAL SIZE clause reserves a memory space for size_number table lines. Whenever an internal table object is declared, the size of the table does not belong to the data type of the table.

Note − Much less memory is consumed when an internal table is populated for the first time.

Example


Step 1 − Open the ABAP Editor by executing the SE38 transaction code. The initial screen of ABAP Editor appears.

Step 2 − In the initial screen, enter a name for the program, select the Source code radio button and click the Create button to create a new program.

Step 3 − In the 'ABAP: Program Attributes' dialog box, enter a short description for the program in the Title field, select the 'Executable program' option from the Type drop-down menu in the Attributes group box. Click the Save button.

Step 4 − Write the following code in ABAP editor.

REPORT ZINTERNAL_DEMO.
TYPES: BEGIN OF CustomerLine,
Cust_ID TYPE C,
Cust_Name(20) TYPE C,
END OF CustomerLine.

TYPES mytable TYPE SORTED TABLE OF CustomerLine
WITH UNIQUE KEY Cust_ID.
WRITE:/'The mytable is an Internal Table'.

Step 5 − Save, activate and execute the program as usual.

In this example, mytable is an internal table and a unique key is defined on the Cust_ID field.

The above code produces the following output:

The mytable is an Internal Table.

«« Previous
Next »»

No comments:

Post a Comment