[ABAP] OpenSQL: Summenbildung (SUM) mit Abhängigkeit (CASE)

Variante 1

* Summenbildung, abhängig von Spalte Soll/Haben
DATA: lv_bsid_sum type bsid-dmbtr.

SELECT SUM( CASE shkzg
* Haben
                WHEN 'H' THEN dmbtr
* Soll, negieren für Subtraktion
                WHEN 'S' THEN dmbtr * -1
            END )
  INTO @lv_bsid_sum
  FROM bsid
  WHERE bukrs = '0040'.

IF sy-subrc = 0.
* Summierten Wert ausgeben
  WRITE: / lv_bsid_sum.
ENDIF.

Variante 2

* Summenbildung, abhängig von Spalte Soll/Haben
DATA: lv_bsid_sum type bsid-dmbtr.

SELECT SUM( CASE
* Haben
                WHEN shkzg = 'H' THEN dmbtr
* Soll, negieren für Subtraktion
                WHEN shkzg = 'S' THEN dmbtr * -1
            END )
  INTO @lv_bsid_sum
  FROM bsid
  WHERE bukrs = '0040'.

IF sy-subrc = 0.
* Summierten Wert ausgeben
  WRITE: / lv_bsid_sum.
ENDIF.

[ABAP] Rückgabedaten eines SELECTs in einzelne Variablen übergeben

DATA: lv_name_first TYPE ad_namefir.
DATA: lv_name_last TYPE ad_namelas.
DATA: lv_smtp_addr TYPE ad_smtpadr.

SELECT SINGLE p~name_first, p~name_last, a~smtp_addr
  FROM usr21 AS u
  INNER JOIN adrp AS p ON p~persnumber = u~persnumber
  INNER JOIN adr6 AS a ON a~addrnumber = u~addrnumber AND a~persnumber = u~persnumber
  INTO (@lv_name_first, @lv_name_last, @lv_smtp_addr)
  WHERE u~bname = @sy-uname.

IF sy-subrc = 0.
  ...
ENDIF.

[ABAP] OpenSQL: Felder im SELECT mit konstanten Werten füllen

* Variante 1 (Konstante)
CONSTANTS: co_empty TYPE char7 VALUE '<empty>'.
* Konstante über Alias maktx in das korrespondierende Feld MAKTX einfügen
SELECT matnr, @co_empty AS maktx FROM mara INTO TABLE @DATA(it_mara) UP TO 10 ROWS.

cl_demo_output=>display( it_mara ).

* Variante 2 (Variable)
DATA: lv_empty TYPE char7 VALUE '<empty>'.
* Konstante über Alias maktx in das korrespondierende Feld MAKTX einfügen
SELECT matnr, @lv_empty AS maktx FROM mara INTO TABLE @DATA(it_mara) UP TO 10 ROWS.

cl_demo_output=>display( it_mara ).

[ABAP] Verknüpfte Abfrage mehrerer Tabellen, INNER JOIN, Alias

PARAMETERS: p_name TYPE TYPE string DEFAULT 'xyz'.

START-OF-SELECTION.

* SELECT durch Verknüpfung über mehrere Tabellen
  SELECT u~bname,
         u~persnumber,
         u~addrnumber,
         a~smtp_addr
    INTO CORRESPONDING FIELDS OF TABLE @DATA(it_usr)
    FROM usr21 AS u
    INNER JOIN adr6 AS a ON a~persnumber = u~persnumber AND a~addrnumber = u~addrnumber
    WHERE u~bname = @name.

  cl_demo_output=>display( it_usr ).