Application Development and Automation 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: 
Read only

Difference Between Sorted Internal Table & Hashed Internal Table

Former Member
0 Likes
11,890

Hi Friends,

Can anyone explain the difference between Sorted Internal Table and Hashed Table?

When and where we use them?

One code example of each like declaring, fetching data into these tables and how Insert and Append Statement behaves in these tables?

Regards,

Pradeep

Note : Points will be rewarded for complete and good answers.

5 REPLIES 5
Read only

Former Member
0 Likes
3,685

Sorted tables

This is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted tables using the INSERTstatement. Entries are inserted according to the sort sequence defined through the table key. Any illegal entries are recognized as soon as you try to add them to the table. The response time for key access is logarithmically proportional to the number of table entries, since the system always uses a binary search. Sorted tables are particularly useful for partially sequential processing in a LOOP if you specify the beginning of the table key in the WHEREcondition.

Hashed tables

This is the most appropriate type for any table where the main operation is key access. You cannot access a hashed table using its index. The response time for key access remains constant, regardless of the number of table entries. Like database tables, hashed tables always have a unique key. Hashed tables are useful if you want to construct and use an internal table which resembles a database table or for processing large amounts of data.

If the target table itab has the type SORTED TABLE, it must be correctly sorted after you have finished inserting lines, otherwise a runtime error is triggered.

If the target table itab is a SORTED TABLE, it must be correctly sorted after the append, otherwise a runtime error occurs.

SORTED TABLE:

The new entry is added in its appropriate place, determined by the table key. The key values are taken from the specified work area wa or from the header line of the table. If the key is NON-UNIQUE, the entry is placed at the top of the list of duplicates. The insertion point is determined internally using a binary search. This makes the relationship between the runtime required and the number of table entries logarithmic.

HASHED TABLE:

The new entry is placed in the internal hash administration of the table according to the table key. The key values are taken from the specifiedwork area wa or the header line of the table. The runtime required remains constant, since it does not depend on the number of table entries. The key must be unique.

Example

Construct a table sorted by name and age:

TYPES: BEGIN OF PERSON,

NAME(10) TYPE C,

AGE TYPE I,

END OF PERSON.

DATA: P TYPE PERSON,

PTAB TYPE SORTED TABLE OF PERSON

WITH UNIQUE KEY NAME AGE.

P-NAME = 'Steve'. P-AGE = 20. INSERT P INTO TABLE PTAB.

P-NAME = 'Andy'. P-AGE = 20. INSERT P INTO TABLE PTAB.

P-NAME = 'Steve'. P-AGE = 17. INSERT P INTO TABLE PTAB.

P-NAME = 'Andy'. P-AGE = 20. INSERT P INTO TABLE PTAB.

Die Tabelle enthält nun die folgenden Einträge:

Andy 20

Steve 17

Steve 20

SY-SUBRC is set to 4.

Variant 3

INSERT LINES OF itab1 INTO itab2 .

Effect

Inserts the internal table itab1 or an extract of it into the internal table itab2. This operation is the same as using a loop at the source area and inserting the entries into the target table line-by-line.

In INDEX idx3, like in variant 1, you specify the table index position before which you want to insert the entry in itab2. itab2 cannot have the type HASHED or ANY TABLE, since these tables have no defined index operations.

If you are using a LOOP you can omit the INDEX idx3 specification. The entry is then inserted in the target table before the current LOOP line.

If the target table itab2 has the type SORTED TABLE, it must be correctly sorted when you have finished adding entries. Otherwise, a runtime error occurs.

If the target table is defined with a UNIQUE KEY, you must ensure that this characteristic is preserved when you add more entries to it. If you try to add duplicate records, a runtime error occurs.

You can restrict the number of lines taken from the source table itab1 by using FROM idx1 and TO idx2. If you omit FROM idx1, the range begins at the first line of itab1. If you omit TO idx2, the range ends at the last line of itab1. This means that if you omit both FROM and TO, the whole table is inserted into the target table. If itab1 has the table type HASHED or ANY TABLE, you may not use the FROM or TO additions, since these table types have no defined index operations.

check out this link:

reward if helpful

raam

Edited by: Kodandarami Reddy.S on Mar 27, 2008 8:34 AM

Read only

Former Member
0 Likes
3,685

Hi,

SORTED INTERNAL TABLE:

Resorting techniques are not allowed. Data sorted at population of data.

By default it is sorted in ascending order where as in standard table sorting is based on user. Searching is binary type. It is faster than linear search in standard internal tables. Display is very faster in sorted than standard internal tables & structured.

In a sorted table, the system automatically stores the entries and inserts new entries sorted by the table key. The system uses a binary search on the table when you access it using the key. You can specify the key of a sorted table as unique. You will often use the key to access a sorted table, but it is also possible to use the index. Standard tables and sorted tables are generically known as index tables.

DATA: ITAB LIKE SORTED TABLE OF KNA1 WITH NON-UNIQUE KEY LAND1 WITH HEADER LINE INITIAL SIZE 1.

SELECT * FROM KNA1 INTO TABLE ITAB.

LOOP AT ITAB.

WRITE:/ ITAB-KUNNR,ITAB-NAME1,ITAB-ORT01.

ENDLOOP.

HASHED INTERNAL TABLE:

Default searching is linear. Random oreder reading is not possible. It is more efficient(display) than i=indexed internal tables.

When ever hashed internal tables are defined, table is defined with only two fields. First field is ‘field name’ and second field is ‘field value’. A row in this internal table reprents one field of table. Internal table indexing can be created only for rows which represents one entry. For this tables indexing not generated, internally called as non-index internal table.

Reward,if it is useful.

Thanks,

Chandu

Read only

Former Member
0 Likes
3,685

Hi,

Sorted Tables:

Sorted tables are always saved correctly sorted by key. They also have a linear key, and, like standard tables, you can access them using either the table index or the key. When you use the key, the response time is in logarithmic relationship to the number of table entries, since the system uses a binary search. The key of a sorted table can be either unique, or non-unique, and you must specify either UNIQUE or NON-UNIQUE in the table definition. Standard tables and sorted tables both belong to the generic group index tables.

This table type is particularly suitable if you want the table to be sorted while you are still adding entries to it. You fill the table using the (INSERT) statement, according to the sort sequence defined in the table key. Table entries that do not fit are recognised before they are inserted. The response time for access using the key is in logarithmic relation to the number of

table entries, since the system automatically uses a binary search. Sorted tables are appropriate for partially sequential processing in a LOOP, as long as the WHERE condition contains the beginning of the table key.

Hashed Tables:

Hashes tables have no internal linear index. You can only access hashed tables by specifying the key. The response time is constant, regardless of the number of table entries, since the search uses a hash algorithm. The key of a hashed table must be unique, and you must specify UNIQUE in the table definition.

This table type is particularly suitable if you want mainly to use key access for table entries. You cannot access hashed tables using the index. When you use key access, the response time remains constant, regardless of the number of table entries. As with database tables, the key of a hashed table is always unique. Hashed tables are therefore a useful way of constructing and

using internal tables that are similar to database tables.

Regards

Read only

Former Member
0 Likes
3,685

Hi,

Sorted tables are always saved sorted by the key. They also have an internal index. The system can access records either by using the table index or the key. The response time for key access is logarithmically proportional to the number of table entries, since the system uses a binary search. The key of a sorted table can be either unique or non-unique. When you define the table, you must specify whether the key is to be unique or not. Standard tables and sorted tables are

known generically as index tables.

Hashed tables have no linear index. You can only access a hashed table using its key. The response time is independent of the number of table entries, and is constant, since the system access the table entries using a hash algorithm. The key of a hashed table must be unique. When you define the table, you must specify the key as UNIQUE.

Examples

Sorted:

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.

Hashed:

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.

Regards,

Bhaskar

Read only

Former Member
0 Likes
3,685

Hi Pradeep Dhadwal,

here it goes .....

1. Which type of Internal Table to use will come into picture only when u know KEY & ACCESS.

If the requirement is like 'Key should be unique & Access should be Key access' , then you can go with Hashed Table.

If the requirement is 'Unique/Nonunique & Access is Key Access' then u can go with Sorted Table.

2. If you need the records always in sorted order ( instead of applying SORT ) then you can declare it as

SORTED table.

If you have large number of records and need to access them then use hashed table.

and further You cannot access a hashed table using its index. The response time for key access remains constant, regardless of the number of table entries.

3. Basically, to define which type of internal table I use, I analyze the following points:

Key: is it unique or non-unique?

Access: will I always seek the table by key, or will I need to seek by index?

Hashed tables don't allow non-unique keys, and they cannot be searched by index (for instance, READ TABLE ... INDEX 1). Also, they have a limitation on the number of records (I don't remember the exact number, but I believe it's something like 2 million records).

If there are non-unique keys or if the table needs to be accessed by index, I use sorted tables.

If I need to access the table by non-key fields, I use a standard internal table.

hope this helps you a bit better !

Reward if useful

Cheers

Kripa Rangachari.