Sunday, March 4, 2018

Table Expressions



Internal table of Invoices.

Now you want to build a range table of materials from above invoice table.

traditional

loop at lt_invoice into ls_invoice.
  ls_matnr_r-sign = 'I'.
  ls_matnr_r-low= 'EQ'.
  ls_matnr_r-low = ls_invoice-matnr.
 append ls_matnr
endloop.


https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/index.htm



    DATA(out) = cl_demo_output=>new( ).

    DATA(itab1) = VALUE itab1(
      FOR j = 41 THEN j - 10 UNTIL j < 10
      ( col1 = j col2 = j + 1 col3 = j + 2 col4 = j + 3 ) ).
    out->write( itab1 ).

    DATA(itab2) = VALUE itab2(
      FOR wa IN itab1 WHERE ( col1 < 30 )
        ( wa ) ).
    out->write( itab2 ).

    DATA(itab3) = VALUE itab3(
      FOR wa IN itab1 INDEX INTO idx WHERE ( col1 = 21 ) ##PRIMKEY[key]
        ( LINES OF itab1 from idx ) ).
    out->write( itab3 ).

    DATA(itab4) = VALUE itab4(
       FOR wa IN itab1 FROM 2 TO 3
         ( col1 = wa-col2 col2 = wa-col3 ) ).
    out->write( itab4 ).

    DATA(base)  = VALUE itab5( ( 1 ) ( 2 ) ( 3 ) ).
    DATA(itab5) = VALUE itab5(
       BASE base
       FOR wa IN itab1 USING KEY key
          ( wa-col1 ) ).
    out->write( itab5 ).

    out->display( ).



From an internal table itab1, the content of new internal tables is created in constructor expressions with one FOR expression each:
  • itab2 has the same row type as itab1. All columns of all rows are assigned that meet a WHERE condition.
  • itab3 has the same row type as itab1. All rows are assigned that meet a WHERE condition from a row.
  • itab4 has fewer columns than itab1. Only certain columns of a restricted number of rows are assigned.
  • itab5 has only one column. The addition BASE is used to first assign the table base and then the first column of itab1. This demonstrates the effect of specifying a sorted secondary key for this column.


No comments:

Post a Comment