Skip to Content
avatar image
Former Member

'loop where' on dynamic hashed table

hi everybody,

im having a problem reading selectively out of a dynamic hashed table.

i_th_ref_data is a hashed table delivered by a SAP UExit filled with data. i wish to loop over it (several entries required) selectively (i.e. WHERE) for performance reasons.

i've treid:

...

assign component '0COMPANY' of

structure <f_ref_data>

to <f_company>.

...

loop at i_th_ref_data assigning <f_ref_data>

where <f_company> = '4711'.

...

endloop.

but get the error "the row type of the table must be statically defined"

does anybody have an idea how to do a dynamic loop on a hashed table like this?

performance is critical: hence solutions with if-statements in the loop are not acceptable.

greetings,

martin

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

4 Answers

  • Sep 21, 2010 at 04:47 PM

    Loop statement supports dynamic clauses in higher versions only but read statement supports.

    May be you have to do something like this

    read table i_th_ref_data assigning <f_ref_data> with table key ('0COMPANY') = '4711'.
    

    You can also dynamically pass the fields to the read statement instead of harcoding it , but the problem is you can only get one line per read statement . So the possibility is go for if .. endif conditions

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      keshav,

      what do you mean "Loop statement supports dynamic clauses in higher versions"?

      which versions r u refering to?

      u r read proposal does not work since i need several entries.

      Moderator message - Please do not use textspeak

      Edited by: Rob Burbank on Sep 21, 2010 1:51 PM

  • Sep 21, 2010 at 06:02 PM

    Hi,

    I mean the syntax of loop with dynamic where clause is supported in higher releases of SAP. In your code the field seems to be dynamic in nature.

    In the link:[http://www.adfahrer.com/workplace/sem/2006-11-28-adsig-sbn-walldorfabap/horstkeller-cd200-sent-in.pdf] . Available after SAP NetWeaver 2004s under section preview of next release. I think so its available from SAP NetWeaver 7.0

    Add comment
    10|10000 characters needed characters exceeded

  • Sep 21, 2010 at 03:57 PM
    loop at i_th_ref_data assigning <f_ref_data>.
    if <f_company> = '4711'.
    ...
    endif.
    endloop.
    

    This will work

    Add comment
    10|10000 characters needed characters exceeded

    • >

      >

      > loop at i_th_ref_data assigning <f_ref_data>.
      > if <f_company> = '4711'.
      > ...
      > endif.
      > endloop.
      > 

      >

      > This will work

      Just a quick note about this. It won't work. Why? Because the <f_ref_data> (and it's component) are assigned before the loop. When you're in the loop, the <f_ref_data> points to a different area of memory. In these situations you must either put the ASSIGN COMPONENT inside the loop, or (more efficiently) use LOOP AT i_th_ref_data INTO <f_ref_data>.

      To the original question:

      Dynamically create a STANDARD table of the same line type as i_th_ref_data with a key of 'COMPANY' (double check the name of the key - in my experience infoobject 0COMPANY relates to field COMPANY).

      DATA: lr_data TYPE REF TO data.
      CREATE DATA lr_data LIKE LINE OF i_th_ref_data.
      ASSIGN lr_data->* TO <ls_ref_data>.
      
      CREATE DATA lr_data LIKE STANDARD TABLE OF <ls_ref_data>.
      ASSIGN lr_data->* TO <lt_ref_data>.
      
      <lt_ref_data> = i_th_ref_data.
      
      SORT <lt_ref_data> BY ('COMPANY').
      
      READ TABLE <lt_ref_data> WITH KEY ('COMPANY') eq '4711' TRANSPORTING NO FIELDS BINARY SEARCH.
      l_index = sy-tabix.
      ASSIGN COMPONENT 'COMPANY' OF STRUCTURE <ls_ref_data> TO <l_company>.
      LOOP AT <lt_ref_data> INTO <ls_ref_data> FROM l_index.
        IF <l_company> NE '4711'.
          EXIT.
        ENDIF.
      * Do stuff
      ENDLOOP.

      You might be able to dynamically create a SORTED table, which would be better. But I can't remember how to dynamically define the key. You probably can do it via RTTS.

      matt

  • Sep 21, 2010 at 04:35 PM

    For me this loop is not ok. Does <f_company> hold component name or its value? It should be the former:

    loop at i_th_ref_data assigning <f_ref_data>
    where ('COMPONENT_NAME') = '4711'.
    ...
    endloop.
    
    "not as in your case
    loop at i_th_ref_data assigning <f_ref_data>
    where some_value = '4711'. "<f_company> holds SOME_VALUE
    ...
    endloop.
    

    Regards

    Marcin

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      hello marcin,

      <f_company> hold the component name (not the value).

      unfortunately if i hardcode it with '0COMPANY' the result is the same.

      thanx for ur answer though.

      martin