[ABAP] REDUCE: Kosten (Wertetabelle) summieren und neue Summentabelle erzeugen

TYPES: BEGIN OF ty_s_kosten,
         k1 TYPE decfloat16,
         k2 TYPE decfloat16,
       END OF ty_s_kosten.

TYPES: ty_it_kosten TYPE STANDARD TABLE OF ty_s_kosten WITH EMPTY KEY.
TYPES: ty_it_sum TYPE STANDARD TABLE OF decfloat16 WITH EMPTY KEY.

DATA(it_kosten) = VALUE ty_it_kosten( ( k1 = '0.59' k2 = '1.48' )
                                      ( k1 = '1.28' k2 = '2.31' )
                                      ( k1 = '4.07' k2 = '2.35' ) ).

* Summen ausrechnen, neue Tabelle mit den Summen erstellen
DATA(it_sum) = REDUCE ty_it_sum(
* Initialwerte (leere Tabelle) für die Rückgabe (Typen bei REDUCE und VALUE müssen identisch sein)
                                 INIT sum = VALUE ty_it_sum( )
* alle Datensätze aus it_kosten
                                 FOR <k> IN it_kosten
* neue Elemente an it_sum anfügen, auf Basis der alten "sum"
                                 NEXT sum = VALUE #( BASE sum
                                                     ( <k>-k1 + <k>-k2 )
                                                   )
                               ).

* Ausgabe
cl_demo_output=>write_data( it_kosten ).
cl_demo_output=>write_data( it_sum ).
cl_demo_output=>display( ).

[ABAP] Hilfsvariablen mit LET-Ausdruck definieren

LET

  • Verwendung innerhalb von Operatoren NEW, VALUE, CONV, COND, SWITCH, REDUCE, FOR …
  • es konnen inplace Variablen oder Feldsymbole deklariert werden
  • Variablen, die im LET-Ausdruck definiert wurden k?nnen nur innerhalb des jeweiligen Scopes verwendet werden

Variante 1 (Variablendefinition, CONV)

* Carrier
PARAMETERS: p_carrid TYPE scarr-carrid DEFAULT 'LH'.

START-OF-SELECTION.

* Daten holen
  SELECT * FROM scarr INTO TABLE @DATA(it_carr).

  TRY.
* String zusammenbauen
      DATA(lv_carr_name) = CONV string(
* Hilfsvariablen per LET definieren
* ersten Carrier mit Carrid holen, kann Exception CX_SY_ITAB_LINE_NOT_FOUND werfen
                                        LET <c> = it_carr[ carrid = p_carrid ]
                                            dp  = |: |
* Hilfsvariablen verwenden
                                        IN
                                        |{ <c>-carrname }{ dp }{ <c>-url }| ).

      WRITE: / lv_carr_name.

* Exceptions abfangen
    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

Variante 2 (Variablendefinition, COND)

DATA(lv_lang) = COND string(
* Hilfsvariablen per LET definieren
                             LET lang     = cl_abap_syst=>get_language( )
                                 lang_iso = cl_i18n_languages=>sap1_to_sap2( im_lang_sap1 = lang )
* Hilfsvariablen verwenden
                             IN
                             WHEN lang_iso = 'DE' THEN 'Deutsch'
                             WHEN lang_iso = 'EN' THEN 'Englisch'
                             ELSE 'andere Sprache'
                           ).

WRITE: / lv_lang.

Variante 3 (Tabelleneinträge, VALUE)

TYPES: BEGIN OF ty_s_adr,
         id   TYPE i,
         city TYPE string,
       END OF ty_s_adr.

TYPES: ty_it_adr TYPE STANDARD TABLE OF ty_s_adr WITH DEFAULT KEY.

TYPES: BEGIN OF ty_s_person,
         city_id  TYPE i,
         forename TYPE string,
         surename TYPE string,
         age      TYPE i,
       END OF ty_s_person.

TYPES: ty_it_persons TYPE STANDARD TABLE OF ty_s_person WITH DEFAULT KEY.

* Städte
DATA(it_city) = VALUE ty_it_adr(
                                 ( id = 100 city = 'Berlin' )
                                 ( id = 101 city = 'Hamburg' )
                                 ( id = 102 city = 'M?nchen' )
                               ).

* Tabelle mit Personen
DATA(it_persons) = VALUE ty_it_persons(
                                        ( city_id = 100 forename = 'Udo'   surename = 'Lehmann' age = 35 )
                                        ( city_id = 101 forename = 'Erika' surename = 'Müller'  age = 63 )
                                        ( city_id = 102 forename = 'Heinz' surename = 'Schulze' age = 65 )
                                ).

DATA(it_csv) = VALUE stringtab(
* alle Namen durchgehen
                                 FOR <p> IN it_persons INDEX INTO idx
* Hilfsvariablen zusammensetzen
                                   LET name       = |{ <p>-surename }, { <p>-forename }|
                                       city       = it_city[ id = <p>-city_id ]-city
                                       age_string = COND string( WHEN <p>-age > 63 THEN 'Rentner' ELSE 'kein Rentner' )
                                       output     = |{ idx };{ name };{ city };{ age_string }|
* Hilfsvariablen verwenden
                                   IN
* neue Zeile zu Tabelle hinzufügen
                                   ( output )
                               ).

* Anzeige
cl_demo_output=>display( it_csv ).

Variante 4 (CONV)

DATA(lv_i) = CONV i( LET x = 1 IN x ).
WRITE: / lv_i.

Variante 5 (NEW)

DATA(lv_i) = NEW i( LET x = 1 IN x ).
WRITE: / lv_i->*.

[ABAP] OpenSQL: WHERE mit Werteliste

DATA: it_usr TYPE STANDARD TABLE OF usr02 WITH DEFAULT KEY.

SELECT * FROM usr02 INTO TABLE it_usr WHERE bname IN ('XYZ', 'ZYX').

IF sy-subrc = 0.
  SORT: it_usr BY trdat DESCENDING bname ASCENDING.

  WRITE: / |USER         \| DATE       \| TIME     \| CREATED|.
  WRITE: / |-------------------------------------------------|.

  LOOP AT it_usr ASSIGNING FIELD-SYMBOL(<usr>).
    WRITE: / <usr>-bname, '|', <usr>-trdat, '|', <usr>-ltime, '|', <usr>-erdat.
  ENDLOOP.
ENDIF.

[ABAP] Datensätze einer internen Tabelle mit FOR … WHERE kopieren

TYPES: ty_spfli TYPE STANDARD TABLE OF spfli WITH DEFAULT KEY.

DATA: it_spfli TYPE ty_spfli.

SELECT * FROM spfli INTO TABLE @it_spfli.

* Daten mit carrid = LH in neue iTab kopieren
* es werden nur die Daten in carrid, connid, cityfrom, cityto übertragen
DATA(it_for) = VALUE ty_spfli( FOR l IN it_spfli WHERE ( carrid = 'LH' )
                               ( carrid = l-carrid
                                 connid = l-connid
                                 cityfrom = l-cityfrom
                                 cityto = l-cityto ) ).