Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Internal Table

Former Member
0 Kudos

Hi,

As there are three types of Internal tables Standard Table, Sorted table and Hashed table.

Please tell me when to use which internal table?

Thanks,

Sriram.

6 REPLIES 6

Former Member
0 Kudos

HI Sriram,

Refer to the following.

Internal table types

This section describes how to define internal tables locally in a program. You can also define internal tables globally as data types in the ABAP Dictionary.

Like all local data types in programs , you define internal tables using the TYPES statement. If you do not refer to an existing table type using the TYPE or LIKE addition, you can use the TYPES statement to construct a new local internal table in your program.

TYPES <t> TYPE|LIKE <tabkind> OF <linetype> [WITH <key>]

[INITIAL SIZE <n>].

After TYPE or LIKE, there is no reference to an existing data type. Instead, the type constructor occurs:

<tabkind> OF <linetype> [WITH <key>]

The type constructor defines the table type <tabkind>, the line type <linetype>, and the key <key> of the internal table <t>.

You can, if you wish, allocate an initial amount of memory to the internal table using the INITIAL SIZE addition.

Table type

You can specify the table type <tabkind> as follows:

Generic table types

INDEX TABLE

For creating a generic table type with index access.

ANY TABLE

For creating a fully-generic table type.

Data types defined using generic types can currently only be used for field symbols and for interface parameters in procedures . The generic type INDEX TABLE includes standard tables and sorted tables. These are the two table types for which index access is allowed. You cannot pass hashed tables to field symbols or interface parameters defined in this way. The generic type ANY TABLE can represent any table. You can pass tables of all three types to field symbols and interface parameters defined in this way. However, these field symbols and parameters will then only allow operations that are possible for all tables, that is, index operations are not allowed.

Fully-Specified Table Types

STANDARD TABLE or TABLE

For creating standard tables.

SORTED TABLE

For creating sorted tables.

HASHED TABLE

For creating hashed tables.

Fully-specified table types determine how the system will access the entries in the table in key operations. It uses a linear search for standard tables, a binary search for sorted tables, and a search using a hash algorithm for hashed tables.

Line type

For the line type <linetype>, you can specify:

Any data type if you are using the TYPE addition. This can be a predefined ABAP type, a local type in the program, or a data type from the ABAP Dictionary. If you specify any of the generic elementary types C, N, P, or X, any attributes that you fail to specify (field length, number of decimal places) are automatically filled with the default values. You cannot specify any other generic types.

Any data object recognized within the program at that point if you are using the LIKE addition. The line type adopts the fully-specified data type of the data object to which you refer. Except for within classes, you can still use the LIKE addition to refer to database tables and structures in the ABAP Dictionary (for compatibility reasons).

All of the lines in the internal table have the fully-specified technical attributes of the specified data type.

Key

You can specify the key <key> of an internal table as follows:

[UNIQUE|NON-UNIQUE] KEY <col1> ... <col n>

In tables with a structured line type, all of the components <coli> belong to the key as long as they are not internal tables or references, and do not contain internal tables or references. Key fields can be nested structures. The substructures are expanded component by component when you access the table using the key. The system follows the sequence of the key fields.

[UNIQUE|NON-UNIQUE] KEY TABLE LINE

If a table has an elementary line type (C, D, F, I, N, P, T, X), you can define the entire line as the key. If you try this for a table whose line type is itself a table, a syntax error occurs. If a table has a structured line type, it is possible to specify the entire line as the key. However, you should remember that this is often not suitable.

[UNIQUE|NON-UNIQUE] DEFAULT KEY

This declares the fields of the default key as the key fields. If the table has a structured line type, the default key contains all non-numeric columns of the internal table that are not and do not contain references or internal tables. If the table has an elementary line type, the default key is the entire line. The default key of an internal table whose line type is an internal table, the default key is empty.

Specifying a key is optional. If you do not specify a key, the system defines a table type with an arbitrary key. You can only use this to define the types of field symbols and the interface parameters of procedures . For exceptions, refer to Special Features of Standard Tables.

The optional additions UNIQUE or NON-UNIQUE determine whether the key is to be unique or non-unique, that is, whether the table can accept duplicate entries. If you do not specify UNIQUE or NON-UNIQUE for the key, the table type is generic in this respect. As such, it can only be used for specifying types. When you specify the table type simultaneously, you must note the following restrictions:

You cannot use the UNIQUE addition for standard tables. The system always generates the NON-UNIQUE addition automatically.

You must always specify the UNIQUE option when you create a hashed table.

Initial Memory Requirement

You can specify the initial amount of main memory assigned to an internal table object when you define the data type using the following addition:

INITIAL SIZE <n>

This size does not belong to the data type of the internal table, and does not affect the type check. You can use the above addition to reserve memory space for <n> table lines when you declare the table object.

When this initial area is full, the system makes twice as much extra space available up to a limit of 8KB. Further memory areas of 12KB each are then allocated.

You can usually leave it to the system to work out the initial memory requirement. The first time you fill the table, little memory is used. The space occupied, depending on the line width, is 16 <= <n> <= 100.

It only makes sense to specify a concrete value of <n> if you can specify a precise number of table entries when you create the table and need to allocate exactly that amount of memory (exception: Appending table lines to ranked lists). This can be particularly important for deep-structured internal tables where the inner table only has a few entries (less than 5, for example).

To avoid excessive requests for memory, large values of <n> are treated as follows: The largest possible value of <n> is 8KB divided by the length of the line. If you specify a larger value of <n>, the system calculates a new value so that n times the line width is around 12KB.

Examples

TYPES: BEGIN OF LINE,

COLUMN1 TYPE I,

COLUMN2 TYPE I,

COLUMN3 TYPE I,

END OF LINE.

TYPES ITAB TYPE SORTED TABLE OF LINE WITH UNIQUE KEY COLUMN1.

The program defines a table type ITAB. It is a sorted table, with line type of the structure LINE and a unique key of the component COLUMN1.

TYPES VECTOR TYPE HASHED TABLE OF I WITH UNIQUE KEY TABLE LINE.

TYPES: BEGIN OF LINE,

COLUMN1 TYPE I,

COLUMN2 TYPE I,

COLUMN3 TYPE I,

END OF LINE.

TYPES ITAB TYPE SORTED TABLE OF LINE WITH UNIQUE KEY COLUMN1.

TYPES: BEGIN OF DEEPLINE,

FIELD TYPE C,

TABLE1 TYPE VECTOR,

TABLE2 TYPE ITAB,

END OF DEEPLINE.

TYPES DEEPTABLE TYPE STANDARD TABLE OF DEEPLINE

WITH DEFAULT KEY.

The program defines a table type VECTOR with type hashed table, the elementary line type I and a unique key of the entire table line. The second table type is the same as in the previous example. The structure DEEPLINE contains the internal table as a component. The table type DEEPTABLE has the line type DEEPLINE. Therefore, the elements of this internal table are themselves internal tables. The key is the default key - in this case the column FIELD. The key is non-unique, since the table is a standard table.

Former Member
0 Kudos

read this from documentation...

Standard tables are managed system-internally by a logical index. New rows are either attached to the table or added at certain positions. The table key or the index identify individual rows.

Sorted tables are managed by a logical index (like standard tables). The entries are listed in ascending order according to table key.

Hashed tables are managed by a hash algorithm. There is no logical index. The entries are not ordered in the memory. The position of a row is calculated by specifying a key using a hash function.

rgds,

PJ

Former Member

former_member221770
Contributor
0 Kudos

Sriram,

Standard Table - the most commonly used type of table. You can sort and resort this table anytime. You can have duplicate records in them. Recommend that when accessing these tables, you sort them by a key and then READ via a BINARY SEARCH - better performance. When you write to this table the record is "appended" to the end of the table. In order to maintain the sort sequence (if any) you must use the SORT command again.

Sorted table - this table has a defined sort sequence. You cannot resort. You can have duplicates. When you READ these tables, SAP automatically tries to use a BINARY SEARCH. Because you use a Binary Search, the retrieval time isexpodential to the number of records. When you write a record to this table the table is resorted.

Hashed Table - this table has a defined key. When you write to the table it uses a hashing algorithm to identify where the record is stored (think if this as writing the location of a page in an index of a book). When you READ this table the system looks up this "index" and retrieves the record. Because of this the retrieval time is constant. Writing to this type of table takes the most time as it needs to calculate the hashing algorithm.

Personally I generally use Standard Tables as they are the most flexible table available. It lets me resort my data anyhow I like any time I like. I very rarely use a Sorted Table.

I use Hashed Tables as "buffer" tables. Let's say for example I want to retrieve HR data for a number of sales reps. To get HR data I need to use FM 'HR_READ_INFOTYPE'. This takes time. So what I do is read my hashed table to see if I have already got the data (very quick). If I don't have the data, I hit the FM and update my hashed table for next time. By using the hashed table in this way I reduce the number of FM calls I need to make.

Hope you find this helpful. Look up SAP Help (via F1) for more info.

Cheers,

Pat.

Former Member
0 Kudos

Which table type to choose it depends on your requirement.

PJ provided good diferences among them.

added to that.

The response time for an itab depends on the no.of records in case of standard & sort tables.

where as it's constant in case of Hash table type.

Hope this helps to u.

Kindly reward the helpful answers.

cheers,

RAJA.

Former Member
0 Kudos

Hi..

Just go thorugh this link

http://help.sap.com/saphelp_46c/helpdata/EN/fc/eb35de358411d1829f0000e829fbfe/frameset.htm

hope it will clarify your doubts

Regards

Asit