Thursday, November 23, 2017

FOR Table Iteration

Today I used simplest form of table iteration using FOR keyword.

My use case was that I have list of values in an unstructured internal table, say integers 1, 2 and 3. I need to build a range table of those values so that IN operator of SELECT query can be used.

If I were building a range table using fixed variable values or hard coded literals, it would have been something like this. VALUE operator is something I know how to use in simple cases.

DATA range TYPE RANGE OF i.

range = VALUE #(

    ( sign = 'I' option = 'EQ' low = '1' )

    ( sign = 'I' option = 'EQ' low = '2' )

    ( sign = 'I' option = 'EQ' low = '3' )

  ).


Since I am having the values in internal tables, I would need to loop through it.

DATA integers TYPE TABLE OF i.

integers = VALUE #( ( 1 ) ( 2 ) ( 3 ) ).

range = VALUE #( FOR integer IN integers ( sign = 'I' option = 'EQ' low = integer ) ).



Before knowing FOR keyword, I would have done it like this.

DATA range TYPE RANGE OF i.

DATA integers TYPE TABLE OF i.

integers = VALUE #( ( 1 ) ( 2 ) ( 3 ) ).

LOOP AT integers INTO DATA(integer).

  APPEND INITIAL LINE TO range ASSIGNING FIELD-SYMBOL(<range_line>).

  <range_line> = VALUE #( sign = 'I' option = 'EQ' low = integer ).

ENDLOOP.



End result is identical.

No comments: