Friday, March 28, 2008

SAP ABAP Internal Table Basics

SAP ABAP Internal Table Basics

The TYPES statement can be used to create a new local internal table in your program. The syntax is as follows.

TYPES <t> TYPE|LIKE <tabkind> OF <linetype> [WITH <key>]
          [INITIAL SIZE <n>].

TYPE or LIKE can be used, the type constructor defines the table type <TABKIND>, the <linetype> and the <key> of the table. With the INITIAL SIZE the size of the table can be defined.


Table types can be as follows.

ANY TABLE                          
INDEXED TABLE                 
HASHED TABLE                    
STANDARD TABLE             
SORTED TABLE                  

The minimum size of an internal table is 256 bytes.

ANY TABLE
Has GENERIC TYPES

INDEXED TABLE
Has GENERIC TYPES, INDEX ACCESS

HASHED TABLE
FULLY SPECIFIED TYPES, NO INDEX ACCESS, KEY ACCESS. Basically a HASHED TABLE does not have a linear Index and can be accessed by using its key. The response time is constant and does not depend on the number of records. While defining a HASHED TABLE the key that has been defined should be UNIQUE. This table is searched using a HASHED algorithm.

STANDARD TABLE (INDEXED TABLE)
FULLY SPECIFIED TYPES, INDEX ACCESS, KEY ACCESS. These tables have a linear INDEX. These tables can be accessed using the INDEX or the KEY. The response time here does depend on the number of records in the table. While defining the INDEX TABLE the KEY has always to be NON UNIQUE.  UNIQUE key is not allowed.

SORTED TABLE (INDEXED TABLE)        
FULLY SPECIFIED TYPES. INDEX ACCESS, KEY ACCESS. They have an internal INDEX and are always SORTED by the KEY while saving. While doing a KEY access the response time does depend on the number of records in the internal Table. Here the system uses a binary search. In sorted tables the KEY can be UNIQUE as well as NON UNIQUE

Examples of Defining Tables.

TYPES: BEGIN OF SalesOrd,
VBELN LIKE VBAK-VBELN,
KUNNR LIKE KNA1-KUNNR,
MATNR LIKE MARA-MATNR,
END OF SalesOrd.

STANDARD TABLE
TYPES ITAB_SalesORD TYPE STANDARD TABLE OF SalesOrd WITH DEFAULT KEY.

SORTED TABLE
TYPES ITAB_SORD_St TYPE SORTED TABLE OF SalesOrd WITH UNIQUE KEY VBELN.

HASHED TABLE
TYPES ITAB_SalesOrd TYPE HASHED TABLE OF SalesOrd WITH UNIQUE KEY VBELN.

Appending lines to an Internal Table.

Appending a Single Line

To add a line to an index table, use the statement:

APPEND <line> TO <itab>.

Sorting Internal Tables.

Internal Tables are sorted as follows.

SORT <itab> [ASCENDING|DESCENDING] [AS TEXT] [STABLE].

Attributes of an Internal Table.

You can use the following statement to find the attributes of an Internal Table at Runtime.

DESCRIBE TABLE <itab> [LINES <l>] [OCCURS <n>] [KIND <k>].


LINES parameter will fill the number of lines into the variable <lin>.
OCCURS will return the initial size of the table to the variable <n>.
KIND parameter, will return the table type to the variable  <k>: ‘T’ for standard table, ‘S’ for sorted table, and ‘H’ for hashed table.

Internal Tables Related Interview Questions

True or False

1) Hashed Tables can have UNIQUE key                                                                                                TRUE
2) SORTED Tables can have only NON UNIQUE KEY                                                                            FALSE
3) STANDARD Tables can have UNIQUE as well as NON UNIQUE KEY                                             FALSE
4) HASHED Tables Do not have an INDEX ACCESS                                                                              TRUE
5) The Access time in a Hashed Table depends on the Number of records in the table               FALSE


No comments:

Post a Comment