Skip to Content

VALUE and multiple FOR operators to create internal table

Hi,

I'm searching for a way to create an internal table using the VALUE #() operator with multiple FOR expressions inside.

While this works of course with a single FOR operator I can't get this working with more than one operator, right now. Maybe it's impossible at all, may be I'm still doing something wrong.

So far I tried:

DATA(lt_sel) = VALUE rsparams_tt(
 FOR <v2> IN p_reason
 ( 
 selname = 'P_AUGRU' 
 high = <v2>-high 
 low = <v2>-low 
 option = <v2>-option 
 sign = <v2>-sign 
 )
 FOR <v1> IN p_auart
 ( 
 selname = 'P_AUART' 
 high = <v1>-high 
 low = <v1>-low 
 option = <v1>-option 
 sign = <v1>-sign 
 )
 ).

to no avail.

These are non-nested FOR expressions but sequential ones that should create a single internal table from different sources.

So do you have an idea how to get this working?

Michael

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Best Answer
    Nov 24, 2016 at 01:48 PM

    Hello Michael,

    How about using auxiliary variables?

    DATA(lt_sel1)
     = VALUE rsparams_tt( 
          LET 
            sel1 = VALUE rsparams_tt( FOR <so1> IN s_carrid 
                   ( selname = 'S_CARRID' 
                     kind = 'S' 
                     sign = <so1>-sign 
                     option = <so1>-option 
                     low = <so1>-low 
                     high = <so1>-high ) )
            sel2 = VALUE rsparams_tt( FOR <so2> IN s_connid 
                   ( selname = 'S_CARRID' 
                     kind = 'S' 
                     sign = <so2>-sign 
                     option = <so2>-option 
                     low = <so2>-low 
                     high = <so2>-high ) )
          IN
          ( LINES OF sel1 )
          ( LINES OF sel2 )
        ).

    TBH i was not aware that multiple FORs result in a nested loop.

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 25, 2016 at 11:27 AM
    TYPES itab TYPE TABLE OF i WITH EMPTY KEY.
    DATA(itab) = VALUE itab(
                   BASE VALUE itab(
                          BASE VALUE itab(
                              FOR i = 10 UNTIL i > 13 ( i ) )
                              FOR i = 20 UNTIL i > 23 ( i ) )
                              FOR i = 30 UNTIL i > 33 ( i ) ).
    

    gives

    10
    11
    12
    13
    20
    21
    22
    23
    30
    31
    32
    33

    Each FOR expression adds 4 lines to itab, one package after the other.

    So, the very first example above might simply be rewritten as follows:

    DATA(lt_sel) = VALUE rsparams_tt(
     BASE VALUE rsparams_tt(
       FOR <v2> IN p_reason
       ( 
       selname = 'P_AUGRU' 
       high = <v2>-high 
       low = <v2>-low 
       option = <v2>-option 
       sign = <v2>-sign 
       )
     )
     FOR <v1> IN p_auart
     ( 
     selname = 'P_AUART' 
     high = <v1>-high 
     low = <v1>-low 
     option = <v1>-option 
     sign = <v1>-sign 
     )
     ).
    Add comment
    10|10000 characters needed characters exceeded

  • Nov 24, 2016 at 01:40 PM

    Hello,

    As per the ABAP Documentation, the syntax is:

    ... VALUE dtype|#( [let_exp] 
                       [BASE itab] 
                       [FOR for_exp1 
                        FOR for_exp2 
                        ... ] 
                       ( line_spec1 ) 
                       ( line_spec2 ) 
                         ... ) ... 
    

    Then, could you retry with the following code ?

    DATA(lt_sel) = VALUE rsparams_tt(
     FOR <v2> IN p_reason
     FOR <v1> IN p_auart
     ( 
       selname = 'P_AUGRU' 
       high = <v2>-high 
       low = <v2>-low 
       option = <v2>-option 
       sign = <v2>-sign 
     )
     ( 
       selname = 'P_AUART' 
       high = <v1>-high 
       low = <v1>-low 
       option = <v1>-option 
       sign = <v1>-sign 
     )
    ).

    Best regards, Nicolas

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Nicolas,

      TBH i was not aware that the Multiple FORs would result in a nested loop :(

      I read the SAP documentation and then wrote this code snippet to check the behaviour

      TABLES: sflight.
      SELECT-OPTIONS:
      s_carrid FOR sflight-carrid,
      s_connid FOR sflight-connid.
      DATA(lt_sel1)
       = VALUE rsparams_tt(
            LET
              sel1 = VALUE rsparams_tt( FOR <so1> IN s_carrid
                     ( selname = 'S_CARRID'
                       kind = 'S'
                       sign = <so1>-sign
                       option = <so1>-option
                       low = <so1>-low
                       high = <so1>-high ) )
              sel2 = VALUE rsparams_tt( FOR <so2> IN s_connid
                     ( selname = 'S_CARRID'
                       kind = 'S'
                       sign = <so2>-sign
                       option = <so2>-option
                       low = <so2>-low
                       high = <so2>-high ) )
            IN
            ( LINES OF sel1 )
            ( LINES OF sel2 )
          ).
      DATA(lt_sel2)
        = VALUE rsparams_tt(
            FOR <so1> IN s_carrid
            FOR <so2> IN s_connid
             ( selname = 'S_CARRID'
               kind = 'S'
               sign = <so1>-sign
               option = <so1>-option
               low = <so1>-low
               high = <so1>-high )
             ( selname = 'S_CARRID'
               kind = 'S'
               sign = <so2>-sign
               option = <so2>-option
               low = <so2>-low
               high = <so2>-high )
          ).
      cl_demo_output=>new(
      )->begin_section( |Auxilliary Variables|
      )->write_data( lt_sel1
      )->end_section(
      )->begin_section( |Multiple FORs|
      )->write_data( lt_sel2
      )->end_section(
      )->display( ).

      BR,

      Suhas

      PS - I don't use TABLES in productive code ;)

  • Nov 24, 2016 at 04:45 PM

    Use VALUE ... FOR behind BASE of VALUE?

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 24, 2016 at 05:20 PM

    My comment is off topic, but you could also use the CORRESPONDING constructor to reduce the size of the code.

    Add comment
    10|10000 characters needed characters exceeded