‎2008 Apr 25 3:11 PM
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.
‎2008 Apr 25 3:16 PM
Selected value MUST be unique to add to the HASHED table
SELECT DISTINCT carrid connid FROM sflight INTO TABLE sflight_wa1.
‎2008 Apr 25 3:16 PM
Selected value MUST be unique to add to the HASHED table
SELECT DISTINCT carrid connid FROM sflight INTO TABLE sflight_wa1.
‎2008 Apr 25 3:17 PM
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
‎2008 Apr 25 3:17 PM
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
‎2008 Apr 25 3:19 PM
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.
‎2008 Apr 25 3:24 PM
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.
‎2008 Apr 25 3:32 PM
hi experts,
thanku for ur replies...now my probled has solved..