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

How to insert value in an hashed internal table?

Former Member
0 Likes
4,962

Hi,

In my scenario i m having one structure assigned to an internal table which is defined as HASHED with unique key, now i want to insert the entries in the internal table where the primary key values are same but having different secondary values

so my question is what key word i have to use for entering values in this internal table. INSERT, MODIFY, APPEND, COLLECT?

Say like same consumer no with different sales order, quotation,I want all the three rows in the internal table with same consumer no with different data .

Regards

Gaurav

Hi,

In my scenario i m having one structure assigned to an internal table which is defined as HASHED with unique key, now i want to insert the entries in the internal table where the primary key values are same but having different secondary values

so my question is what key word i have to use for entering values in this internal table. INSERT, MODIFY, APPEND, COLLECT?

Say like same consumer no with different sales order, quotation,I want all the three rows in the internal table with same consumer no with different data .

Regards

Gaurav

4 REPLIES 4
Read only

matt
Active Contributor
0 Likes
2,439

If the keys are the same you cannot enter them into a hashed table. Hashed table entries must have unique keys. You should change your table definition so that the key is unique - e.g. include sales order number / quotation in it.

You will use INSERT INTO TABLE if the entry doesn't exist. Or MODIFY TABLE if it does.

matt

Read only

Former Member
0 Likes
2,439

see the uniqueness is there with say some of the key cani change the uniq defination as other entries are different in that case

The code :

DATA:i_final TYPE HASHED TABLE OF zbi_csm_data

WITH UNIQUE KEY ztariff zbpnumber zmonth ,

LOOP AT i_eabl1 ASSIGNING <fs_eabl1>.

LOOP AT i_eabl ASSIGNING <fs_eabl> WHERE anlage = <fs_eabl1>-anlage

AND gernr = <fs_eabl1>-gernr

AND adat = <fs_eabl1>-adat.

CLEAR : wa_final.

ASSIGN wa_final TO <fs_final>.

<fs_final>-zaedat = sy-datum.

LOOP AT i_eanlh ASSIGNING <fs_eanlh>

WHERE anlage = <fs_eabl>-anlage

AND bis >= <fs_eabl>-adat

AND ab <= <fs_eabl>-adat.

EXIT.

ENDLOOP.

CHECK sy-subrc EQ 0 .

READ TABLE i_final ASSIGNING <fs_final>

WITH KEY ztariff = <fs_eanlh>-tariftyp

zbpnumber = <fs_eanlh>-gpart

zmonth = <fs_eabl>-adat

zadat = <fs_eabl>-adat

zmeterno = <fs_eabl>-gernr.

IF sy-subrc NE 0.

<fs_final>-ztariff = <fs_eanlh>-tariftyp.

<fs_final>-zbpnumber = <fs_eanlh>-gpart.

<fs_final>-zinstall = <fs_eanlh>-anlage.

<fs_final>-zmonth = <fs_eabl>-adat.

<fs_final>-zadat = <fs_eabl>-adat.

<fs_final>-zmeterno = <fs_eabl>-gernr.

INSERT <fs_final> INTO TABLE i_final. u201C HERE A NEW LINE GET INSERT when thereu2019s change in ADAT or METERNO

ENDIF.

lv_tabix = sy-tabix.

*****KWH+KWH1 TOD Reading*******************

IF <fs_eabl>-massread = 'KWH' AND

<fs_eabl>-zwnummer = '1' .

<fs_final>-zadat = <fs_eabl>-adat.

<fs_final>-zkwhread = <fs_eabl>-v_zwstand + <fs_eabl>-n_zwstand.

flag = 'X'.

ENDIF.

IF <fs_eabl>-massread = 'KWH' AND

<fs_eabl>-zwnummer = '4'.

<fs_final>-zkwhtod1 = <fs_eabl>-v_zwstand + <fs_eabl>-n_zwstand.

flag = 'X1'.

ENDIF.

IF <fs_eabl>-massread = 'KWH' AND

<fs_eabl>-zwnummer = '5'.

<fs_final>-zkwhtod2 = <fs_eabl>-v_zwstand + <fs_eabl>-n_zwstand.

flag = 'X2'.

ENDIF.

IF <fs_eabl>-massread = 'KWH' AND

<fs_eabl>-zwnummer = '6'.

<fs_final>-zkwhtod3 = <fs_eabl>-v_zwstand + <fs_eabl>-n_zwstand.

flag = 'X3'.

ENDIF.

**************END KWH Reading*******************

***********KVA+KVA TOD Reading******************

IF <fs_eabl>-massread = 'KVA' AND

( <fs_eabl>-zwnummer = '13' OR

<fs_eabl>-zwnummer = '4' OR

<fs_eabl>-zwnummer = '7' ).

<fs_final>-zkvaread = <fs_eabl>-v_zwstand + <fs_eabl>-n_zwstand.

flag = 'Z'.

ENDIF.

IF <fs_eabl>-massread = 'KVA' AND

<fs_eabl>-zwnummer = '10'.

<fs_final>-zkvatod1 = <fs_eabl>-v_zwstand + <fs_eabl>-n_zwstand.

flag = 'Z1'.

ENDIF.

IF <fs_eabl>-massread = 'KVA' AND

<fs_eabl>-zwnummer = '11'.

<fs_final>-zkvatod2 = <fs_eabl>-v_zwstand + <fs_eabl>-n_zwstand.

flag = 'Z2'.

ENDIF.

IF <fs_eabl>-massread = 'KVA' AND

<fs_eabl>-zwnummer = '12'.

<fs_final>-zkvatod3 = <fs_eabl>-v_zwstand + <fs_eabl>-n_zwstand.

flag = 'Z3'.

ENDIF.

***********************KVARH Reading.

IF <fs_eabl>-massread = 'KRH' AND

<fs_eabl>-zwnummer = '2'.

<fs_final>-zkvarhread = <fs_eabl>-v_zwstand + <fs_eabl>-n_zwstand.

flag = 'R'.

ENDIF.

************************

MODIFY TABLE i_final FROM <fs_final> ."INDEX sy-tabix.u201D HERE A NEW LINE GET INSERTed when thereu2019s change in ADAT or METERNO

ENDLOOP. " for I_EABL

ENDLOOP."for I_EABL1

Edited by: Gaurav on Feb 13, 2009 6:24 AM

Edited by: Gaurav on Feb 13, 2009 7:19 AM

Read only

Former Member
0 Likes
2,439

Hi,

While entering data into hashed table unique ness of table needs to be maintained else it will result in runtime error.

Use the following:

Insert into table itab from WA.

Only Unique records will be entered.

If you use append or Insert without table clause .

Result will be short dump.

You can break the Unique ness of a table using modify and the key field of the table is defined as TABLE LINE.

EX:

Data:

Itab like hased table of spfli with unique key TABLE LINE.

Regards,

Gurpreet

Regards,

Gurpreet

Read only

Former Member
0 Likes
2,439

Hi Gaurav,

Please Find the Sample Code Below.

TYPES: BEGIN OF attr_line_ty,
         name TYPE attr_name,
         valu TYPE attr_value,
       END OF attr_line_ty.

TYPES: BEGIN OF attr_ty,
         docno    TYPE docno,
         t_attribs  TYPE STANDARD TABLE OF attr_line_ty WITH NON-UNIQUE KEY table_line,
       END OF attr_ty.

DATA: t_attr TYPE HASHED TABLE OF attr_ty WITH UNIQUE KEY docno.

DATA: ls_attr     TYPE attr_ty,
      ls_attrib   TYPE attr_line_ty. 

LOOP AT t_dbattr ASSIGNING <ls_dbattr>.
  ls_attrib-name = <ls_dbattr>-name.
  ls_attrib-valu = <ls_dbattr>-valu.
  APPEND ls_attrib TO ls_attr-t_attribs.
  AT END OF docno.
    ls_attr-docno = <ls_dbattr>-docno.
    INSERT ls_attr INTO TABLE t_attr.
    CLEAR ls_attr-attribs. " Probably not necessary
    CLEAR ls_attr.
  ENDAT.
ENDLOOP.

READ TABLE t_attr WITH TABLE KEY docno = docno ASSIGNING <ls_attr>.
IF sy-subrc IS INITIAL.
  LOOP AT <ls_attr>-t_attribs ASSIGNING <ls_attribs>.
    " <ls_attribs>-name, -valu have your attribute
    " now put them into new structure.
  ENDLOOP.
ENDIF.

Hope this will Help You

Thanks

kalyan