7/25/2018 - 7:08 PM

Declare an internal table

You use the DATA statement as well for tables as for fields. We use Structured Data Objects (structures) in Database Tables & Internal Tables. Data can be copied between structures, even if they are not the same type. Key factors when making an internal table: what fields to use, memory usage (only create the internal table with the components needed). Organisational structure factors: sort order, logical structure, key fields, uniqueness Whether fields need to be unique determines whether to use standard, sorted or hashed internal tables. Also affects performance of program.

Process for creating Internal Tables:

  1. Define a Type for a table (in program or ABAP dictionary)
  2. Instantiate (create) 1 or more Internal Tables based on the Type

There are 3 types of Internal Tables:

  • STANDARD: similar to a DB table (accessible by row & keys)
  • SORTED: data is always sorted by the specified keys (accessible by row & keys)
  • HASHED: unique keys only (accessible by keys only, usually fastest way to access data, cannot read data line by line) Need to define start and end of table. itab is often used as internal table name. BEGIN OF means we are starting the definition. Then we specify what we are declaring. OCCURS tells SAP we are declaring an internal table with a line and that is going to store multiple lines, with n records and indicates an internal table with a header record. OCCURS 0 says we don't know how large table is going to be, so leave it up to the system to determine it. Table can expand as we fill it with data. Make sure your field names are equal to other field names or fields from ABAP dictionary, for MOVE-CORRESPONDING statement. Always declare the internal table in object-oriented style. Can reuse data objects and modularize better. Uses TYPES to declare new data type, i.e. structures of existing data types. First TYPES just defines the structure of one line. Does not yet define the internal table yet. First you define the table type with TYPES. The TYPE in this statement indicates you're going to base your table on a type that already exists. STANDARD TABLE OF indicates you're going to use a standard table type. Can add INITIAL SIZE, if you know the exact size of the table. After TYPE SORTED TABLE OF, specify a key. Tells system what you are going to sort the table by. Key should be part of line type. Types are just the blueprints to make up a table. You use DATA and TYPE to create internal table. If you want to use a header line you need to state WITH HEADER LINE. Cannot use header line style tables in ABAP objects. Work area is not part of internal table, but works together with table. Read data into internal table from work area or you can read data from internal table into work area to work with it. If there are multiple internal tables with the same structure, you can use one work area to read and write data to and from multiple tables (reuse code). Give work area wa_ & same name as internal table to make mental note that they go together.

STANDARD Internal Tables:

  • Similar to a normal DB table, no order to the records in table: like a DB table records are stored in sequential fashion as records as created
  • However, with a normal DB table you can define unique keys
  • System does keep row numbers by using indexes
  • Defining a key is optional, helps improve efficiency when you're reading data from the table: have to be NON-UNIQUE keys
  • Can not have UNIQUE keys, because by definition STANDARD tables allow duplicate keys

SORTED Internal Tables:

  • System automatically maintains the sort order based on a key, sort order is based on the sequence with which we create the keys
  • Therefore, a key must be defined (main difference with STANDARD tables)
  • The key can be unique or non-unique
  • You can specify multiple keys to speed up data access

HASHED Internal Tables:

  • Access to and order of fields is based on a system-managed hashing algorithm
  • You must have a key field defined
  • Key must be unique
  • No index is maintained by the system
*Internal Table with Header line
        surname LIKE zemployees-surname,
        dob     LIKE zemployees-dob,
      END OF itab01.
*New Way of defining Internal Tables
*Declare a Line Type     
TYPES: BEGIN OF line01_typ,
        surname LIKE zemployees-surname,
        dob     LIKE zemployees-dob,
       END OF line01_typ.
*Declare the 'Table Type' based on the Line Type / Structure.
TYPES itab02_typ TYPE STANDARD TABLE OF line01_typ (structure) [WITH NON-UNIQUE KEY List_of_fields].
*TYPES itab03_typ TYPE SORTED TABLE OF line01_typ 
*                                 WITH [UNIQUE | NON-UNIQUE] KEY surname dob.
*TYPES itab04_typ TYPE HASHED TABLE OF line01_typ 
*                                 WITH UNIQUE KEY List_of_fields.

*Declare/instantiate/create the table based on the 'Table Type'
DATA itab02 TYPE itab02_typ.

*Declare/instantiate/create the Work Area to use with our Internal Table
DATA wa_itab02 TYPE line01_typ.