November 17, 2015

Internal tables in ABAP. Part 1

There are two kinds of tables in SAP, these are:
  • Transparent tables
  • Internal tables
Transparent tables are the database tables and they are referenced by the same name. The content of these tables are accessed by OPEN SQL which is a convention defined by SAP to map the data from these tables. These tables are created and modified in transaction 'SE11'.

Internal tables are data repositories that can have a defined structure and reside in the session memory. The structure of these tables can be one defined by our own structure, a transparent table structure or the combination of both. These can be created or defined in 'SE11' and/or as part of the source code of a program. 

Most of the time in ABAP we are going to face the use of the latter when handling large amount of data that could finally be stored in a database or especifically in transparent tables. To access the rows of the internal tables we use special structures known as work areas and field symbols. We will cover more on field symbols in next posts.

There are 3 types of internal tables:
  1. Standard tables
  2. Sorted tables
  3. Hashed tables
  4. Any tables
  5. Index tables

Standard tables

Characteristics:
  • Is the most basic table structure and is used by the majority of (if not all) the function modules and everywhere in ABAP.
  • You can address data using an index and it is also possible to make a binary search which is logarithmically proportional to size of the table.
  • In order to make use a binary search it's necessary to sort the table first with key field.
  • No restrictions on duplicated keys.
  • Use APPEND to add rows to the table.
Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
DATA: it_data TYPE STANDARD TABLE OF ty_data, " It can also be defined as 'it_data TYPE TABLE OF ty_data' without the word 'STANDARD'
   wa_data TYPE ty_data. " Or alternatively: DATA wa_data LIKE LINE OF it_data.

DO 100 TIMES.
 wa_data-field1 = sy-tabix.
 CONCATENATE 'Data' sy-tabix INTO wa_data-field2.
 GET TIME FIELD wa_data-time.
 APPEND wa_data TO it_data.
ENDDO.

READ TABLE it_data INTO WA_DATA WITH KEY field1 = 50.

IF SY-SUBRC EQ 0.
 WRITE: / 'Read table using field: ',  wa_data.
ENDIF.

SORT it_data BY field1.
READ TABLE it_data INTO WA_DATA index 10.

IF SY-SUBRC EQ 0.
 WRITE: / 'Read using index: ', wa_data.
ENDIF.


Sorted table

Characteristics:
  • A key must be defined.
  • Can be sorted or non-sorted by specified key[s].
  • Always in ascending order.
  • It can't be used 'BINARY SEARCH', however internally it uses binary search. The search is logarithmically proportional to size of the table.
  • Can't be used the command 'SORT' on them.
  • Use INSERT <work area> INTO <sorted table> to add rows to the table. The new rows will internally be added according the key defined.
Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
DATA: it_sorted TYPE SORTED TABLE OF ty_data WITH UNIQUE KEY field1,
      it_non_sorted TYPE SORTED TABLE OF ty_data WITH NON-UNIQUE KEY field1.

DO 100 TIMES.
 wa_data-field1 = 100 - sy-tabix. " Number in descendent order
 CONCATENATE 'Data' sy-tabix INTO wa_data-field2.
 GET TIME FIELD wa_data-time.
 INSERT wa_data INTO it_data.
ENDDO.

READ TABLE it_data INTO WA_DATA WITH KEY field1 = 50.

IF SY-SUBRC EQ 0.
 WRITE: / 'Read using key field',  wa_data.
ENDIF.


Hashed table
  • Managed by a hash algorithm.
  • A key must be defined.
  • Main operation is key access.
  • Access time is constant.
  • It is recommended to use with big datasets.
  • Can't be used index to access data.
  • Use INSERT to add rows to the table.
Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
DATA it_hashed TYPE HASHED TABLE OF ty_data WITH UNIQUE KEY field1 field2.

DO 100 TIMES.
 wa_data-field1 = 100 - sy-tabix. " Number in descendant order
 CONCATENATE 'Data' sy-tabix INTO wa_data-field2.
 GET TIME FIELD wa_data-time.
 INSERT wa_data TO it_data.
ENDDO.

READ TABLE it_data INTO WA_DATA WITH KEY field1 = 50.

IF SY-SUBRC EQ 0.
 WRITE: / 'Read using key field',   wa_data.
ENDIF.


Index and Any table

These are generic tables and we are going to show their use in next posts. However they are defined like this.

1
2
3
FIELD-SYMBOLS <it_index> TYPE INDEX TABLE.

FIELD-SYMBOLS <it_any> TYPE ANY TABLE.

See you in the next.

Hope it helps.

Please Share it! :)

No comments :

Post a Comment