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: 

Hashed Internal tables

Former Member

Hi

How to use Hashed Intarnal Tables.

Can u plz write some sample code?

Thanks

1 ACCEPTED SOLUTION

Former Member

Hi,

Hashed Internal 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.

sample code.

types: begin of typ_pernr,

pernr like pa0001-pernr,

ename like pa0001-ename,

end of typ_pernr.

data: ls_pernr type typ_pernr,

lt_pernr type hashed table of typ_pernr with unique key pernr.

...

select pernr ename into table lt_pernr from pa0001.

...

loop at itab.

read table lt_pernr with table key pernr = itab-pernr

into ls_pernr.

write: ls_pernr-ename, itab-data.

endloop.

5 REPLIES 5

Former Member
0 Kudos

Hi,

Refer below code:


DATA: ftab TYPE SORTED TABLE OF f 
           WITH NON-UNIQUE KEY table_line,
      itab TYPE HASHED TABLE OF i 
           WITH UNIQUE KEY table_line,
      fl   TYPE f.

  DO 3 TIMES.
  INSERT sy-index INTO TABLE itab.
  ENDDO.

ftab = itab.

LOOP AT ftab INTO fl.
  WRITE: / fl.
ENDLOOP.

Thanks,

Sriram Ponna.

Former Member

Hi,

Hashed Internal 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.

sample code.

types: begin of typ_pernr,

pernr like pa0001-pernr,

ename like pa0001-ename,

end of typ_pernr.

data: ls_pernr type typ_pernr,

lt_pernr type hashed table of typ_pernr with unique key pernr.

...

select pernr ename into table lt_pernr from pa0001.

...

loop at itab.

read table lt_pernr with table key pernr = itab-pernr

into ls_pernr.

write: ls_pernr-ename, itab-data.

endloop.

Former Member
0 Kudos

hi,

Check out the below related thread ... Hope it helps

Regards,

Santosh

Former Member
0 Kudos

Hi,

Check the sample codes

1.Insert command on HASHED tables on Key access.

Try with below code...

REPORT zhashtable_test .

TYPES :

BEGIN OF t_city,

city TYPE sgeocity-city,

country TYPE sgeocity-country ,

latitude TYPE sgeocity-latitude,

END OF t_city,

t_city_list TYPE HASHED TABLE OF t_city WITH UNIQUE KEY city country.

DATA : wa_itab TYPE t_city,

city_list TYPE t_city_list.

wa_itab-city = 'Coimbatore'.

wa_itab-country = 'INDIA'.

wa_itab-latitude = '150'.

INSERT wa_itab INTO TABLE city_list .

2nd scenario.

codeREPORT zforum34 LINE-SIZE 255.

PARAMETERS p_ktopl LIKE t001-ktopl.

PARAMETERS p_bukrs LIKE t001-bukrs.

PARAMETERS p_konto LIKE skb1-saknr.

DATA: t1 TYPE i,

t2 TYPE i,

tmin TYPE i.

DATA stab TYPE SORTED TABLE OF skb1 WITH UNIQUE KEY bukrs saknr.

DATA htab TYPE HASHED TABLE OF skb1 WITH UNIQUE KEY bukrs saknr.

DATA wa TYPE skb1.

SELECT * FROM skb1 INTO TABLE stab.

write: /'cnt:', sy-dbcnt.

uline.

htab = stab.

GET RUN TIME FIELD t1.

READ TABLE stab INTO wa WITH TABLE KEY bukrs = p_bukrs

saknr = p_konto.

GET RUN TIME FIELD t2.

tmin = t2 - t1.

WRITE:/ 'sorted table :', tmin, 'microseconds'.

ULINE.

CLEAR: t1, t2, tmin.

FREE stab.

GET RUN TIME FIELD t1.

READ TABLE htab INTO wa WITH TABLE KEY bukrs = p_bukrs

saknr = p_konto.

GET RUN TIME FIELD t2.

tmin = t2 - t1.

WRITE:/ 'hashed table :', tmin, 'microseconds'.[/code]

3.This is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted tables using the INSERT statement. 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 WHERE condition.

code

REPORT ZREPORT_SORTED1.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

CLEAR LINE.

READ TABLE ITAB WITH TABLE KEY COL1 = 3

INTO LINE TRANSPORTING COL2.

WRITE: 'SY-SUBRC =', SY-SUBRC,

/ 'SY-TABIX =', SY-TABIX.

SKIP.

WRITE: / LINE-COL1, LINE-COL2.[/code]

The output is:

SY-SUBRC = 0

SY-TABIX = 3

0 9

code

REPORT ZREPORT_SORTED2.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

READ TABLE ITAB WITH KEY COL2 = 16 TRANSPORTING NO FIELDS.

WRITE: 'SY-SUBRC =', SY-SUBRC,

/ 'SY-TABIX =', SY-TABIX.[/code]

The output is:

SY-SUBRC = 0

SY-TABIX = 4

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.

code

REPORT ZREPORT_HASHED1.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL1 = 2. LINE-COL2 = 3.

READ TABLE ITAB FROM LINE INTO LINE COMPARING COL2.

WRITE: 'SY-SUBRC =', SY-SUBRC.

SKIP.

WRITE: / LINE-COL1, LINE-COL2.[/code]

The output is:

SY-SUBRC = 2

2 4

code

REPORT ZREPORT_HASHED2.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

FIELD-SYMBOLS <FS> LIKE LINE OF ITAB.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

READ TABLE ITAB WITH TABLE KEY COL1 = 2 ASSIGNING <FS>.

<FS>-COL2 = 100.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2.

ENDLOOP.[/code]

The output is:

1 1

2 100

3 9

4 16

Regards,

Raj.

Former Member
0 Kudos

hi,

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.

Examples of Defining Tables.

HASHED TABLE

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

Regards,

vineela.