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

hashed table

Former Member
0 Likes
1,020

hi,

can some one help me how to use hashed table

i have tried with this code..but its giving runtime error A row with the same key already exists

DATA: BEGIN OF sflight_wa,

carrid LIKE sflight-carrid,

connid LIKE sflight-connid,

END OF sflight_wa.

DATA: sflight_wa1 LIKE HASHED TABLE OF sflight_wa

WITH UNIQUE KEY carrid connid.

SELECT carrid connid FROM sflight INTO TABLE sflight_wa1.

LOOP AT sflight_wa1 INTO sflight_wa.

WRITE: / sflight_wa-carrid.

ENDLOOP.

thanku.

dhaya.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
913

Selected value MUST be unique to add to the HASHED table


SELECT DISTINCT carrid connid FROM sflight INTO TABLE sflight_wa1.

6 REPLIES 6
Read only

Former Member
0 Likes
914

Selected value MUST be unique to add to the HASHED table


SELECT DISTINCT carrid connid FROM sflight INTO TABLE sflight_wa1.

Read only

Former Member
0 Likes
913

Hi,

It is giving error because the table SFLIGHT contains Multiple Entries for Same combination of CARRID and CONNID and according to the definition of Internal Table the Carrid Connid fields are UNIQUE, hence an error is thrown

so use the below code to rectify the error:

DATA: BEGIN OF sflight_wa,
carrid LIKE sflight-carrid,
connid LIKE sflight-connid,
END OF sflight_wa.

DATA: sflight_wa1 LIKE HASHED TABLE OF sflight_wa
WITH UNIQUE KEY carrid connid.

SELECT DIstinct carrid connid FROM sflight INTO TABLE sflight_wa1.

LOOP AT sflight_wa1 INTO sflight_wa.
WRITE: / sflight_wa-carrid.
ENDLOOP.

Regards,

Sunil

Read only

Former Member
0 Likes
913

The key of table SFLIGHT is CARRID CONNID and FLDATE hence it is possible to have multiple entries in that table with the same CARRID and CONNID.

Since you specified the unique key for your hashed table to be CARRID CONNID only you will get the error because you can't have the same key twice in your hashed table.

Either do a select distinct or extend the primary key of your hashed table to include fldate.

Regards,

Michael

Read only

vinod_vemuru2
Active Contributor
0 Likes
913

Hi Dhaya,

U have defined ur internal table with the user defined key

carrid connid. This will exactly work as primary key of data base table. As u can't have more that one record in database table with same primary key here also u can't have 2 records having same value of the comabination of carrid connid.

Modify ur definition like below.

DATA: sflight_wa1 LIKE HASHED TABLE OF sflight_wa

WITH NON-UNIQUE KEY carrid connid.

Thanks,

Vinod.

Read only

0 Likes
913

I don't believe you can use NON-UNIQUE KEY in a hashed table

This is from SAP Doc

HASHED TABLE

Effect

Defines the table as one that is managed with an internal hash procedure. You can imagine a hashed table as a set, whose elements

you can address using their unique key. Unlike standard and sorted tables, you cannot access hash tables using an index.

All entries in the table must have a unique key.

Access time using the key is constant, regardless of the number of table entries.

You can only access a hashed table using the generic key operations or other generic operations ( SORT, LOOP, and so on). Explicit

or implicit index operations (such as LOOP ... FROM oe INSERT itab within a LOOP) are not allowed.

Read only

Former Member
0 Likes
913

hi experts,

thanku for ur replies...now my probled has solved..