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

Reading Hashtable Record

Former Member
0 Likes
438

Hello,

How read 10th records of Hash internal table? Please guide me with syntax.

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
406

Hello Anil

You cannot read a hashed table using an index. If you loop over a hashed table sy-tabix is undefined. Hashed tables are basically accessed using their<b> key fields</b>.

The following sample report shows how to realize your requirement which, however, is not reasonable using hashed tables.

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_HASHED_TABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_hashed_table.

TABLES: kna1.
TABLES: knb1.

TYPES: BEGIN OF ty_s_customer.
TYPES:   bukrs    TYPE bukrs.
TYPES:   kunnr    TYPE kunnr.
TYPES:   anred    TYPE anred.
TYPES:   name1    TYPE name1_gp.
TYPES: END OF ty_s_customer.
TYPES: ty_t_customer  TYPE HASHED TABLE OF ty_s_customer
                      WITH UNIQUE KEY bukrs kunnr.

DATA:
  gd_idx         TYPE i,
  gs_customer    TYPE ty_s_customer,
  gt_customer    TYPE ty_t_customer.  " hashed table


PARAMETERS:
  p_bukrs    TYPE bukrs  DEFAULT '1000'.


START-OF-SELECTION.


  SELECT * FROM  vf_debi INTO CORRESPONDING FIELDS OF TABLE gt_customer
         WHERE  bukrs  = p_bukrs.


  "  READ TABLE gt_customer INTO gs_customer INDEX 10.  " not allowed

  gd_idx = 0.
  LOOP AT gt_customer INTO gs_customer.
    "   NOTE: sy-tabix is undefined

    ADD 1 TO gd_idx.
    IF ( gd_idx = 10 ).
      WRITE: / gd_idx, gs_customer-kunnr, gs_customer-name1.
      EXIT.
    ENDIF.
  ENDLOOP.

END-OF-SELECTION.

Regards

Uwe

2 REPLIES 2
Read only

uwe_schieferstein
Active Contributor
0 Likes
407

Hello Anil

You cannot read a hashed table using an index. If you loop over a hashed table sy-tabix is undefined. Hashed tables are basically accessed using their<b> key fields</b>.

The following sample report shows how to realize your requirement which, however, is not reasonable using hashed tables.

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_HASHED_TABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_hashed_table.

TABLES: kna1.
TABLES: knb1.

TYPES: BEGIN OF ty_s_customer.
TYPES:   bukrs    TYPE bukrs.
TYPES:   kunnr    TYPE kunnr.
TYPES:   anred    TYPE anred.
TYPES:   name1    TYPE name1_gp.
TYPES: END OF ty_s_customer.
TYPES: ty_t_customer  TYPE HASHED TABLE OF ty_s_customer
                      WITH UNIQUE KEY bukrs kunnr.

DATA:
  gd_idx         TYPE i,
  gs_customer    TYPE ty_s_customer,
  gt_customer    TYPE ty_t_customer.  " hashed table


PARAMETERS:
  p_bukrs    TYPE bukrs  DEFAULT '1000'.


START-OF-SELECTION.


  SELECT * FROM  vf_debi INTO CORRESPONDING FIELDS OF TABLE gt_customer
         WHERE  bukrs  = p_bukrs.


  "  READ TABLE gt_customer INTO gs_customer INDEX 10.  " not allowed

  gd_idx = 0.
  LOOP AT gt_customer INTO gs_customer.
    "   NOTE: sy-tabix is undefined

    ADD 1 TO gd_idx.
    IF ( gd_idx = 10 ).
      WRITE: / gd_idx, gs_customer-kunnr, gs_customer-name1.
      EXIT.
    ENDIF.
  ENDLOOP.

END-OF-SELECTION.

Regards

Uwe

Read only

Former Member
0 Likes
406

Check with below Link :

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb35f8358411d1829f0000e829fbfe/frameset.htm

report zxyz.

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 12 tIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

READ TABLE ITAB into LINE index 10. -> <b>you can not use explicit or implicit index operation with Hash table</b> so you get syntax error

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

SKIP.

WRITE: / LINE-COL1, LINE-COL2.

Thanks

Seshu