[ABAP] Daten aus einer internen Tabelle löschen

Typen & Daten

* Typen
TYPES : BEGIN OF ty_s_sflight,
          carrid   TYPE sflight-carrid,
          connid   TYPE sflight-connid,
          seatsmax TYPE sflight-seatsmax,
          flag     TYPE abap_bool,
        END OF ty_s_sflight.

TYPES: ty_it_sflight TYPE STANDARD TABLE OF ty_s_sflight WITH DEFAULT KEY.

* Daten
DATA(it_sflight) = VALUE ty_it_sflight( ).

* Select
SELECT carrid,
       connid,
       seatsmax,
       CASE WHEN seatsmax > 200 THEN @abap_true " CASE-Anweisung zum Setzen der Spalte "flag"
       END AS flag
  INTO TABLE @it_sflight
  FROM sflight.

Variante 1 (INDEX)

* Eintrag mit Index 1
DELETE it_sflight INDEX 1.

cl_demo_output=>display( it_sflight ).

Variante 2 (Struktur)

* Genau einen Eintrag entsprechend der Struktur
DELETE TABLE it_sflight FROM VALUE #( carrid = 'LH' connid = '400' seatsmax = 280 flag = abap_true ).

cl_demo_output=>display( it_sflight ).

Variante 3 (TABLE KEY)

* Einträge mit Primärschlüssel aus Tabelle löschen
DELETE TABLE it_sflight WITH TABLE KEY carrid = 'LH' connid = '400' flag = abap_true.

cl_demo_output=>display( it_sflight ).

Variante 4 (WHERE)

* Alle Einträge mit leerem flag löschen
DELETE it_sflight WHERE flag IS INITIAL.
* Alle Einträge innerhalb eines Bereiches löschen
DELETE it_sflight WHERE seatsmax > 200 AND seatsmax < 350.
* Alle Einträge die nicht der Bedingung entsprechen
DELETE it_sflight WHERE NOT carrid = 'AA'.

cl_demo_output=>display( it_sflight ).

Variante 5 (RANGE)

* alle Carrier, die nicht 'AA' sind aus der iTab löschen
DELETE it_sflight WHERE NOT carrid IN VALUE rseloption( ( sign   = 'I'
                                                          option = 'EQ'
                                                          low    = 'AA'
                                                          high   = '' ) ).

cl_demo_output=>display( it_sflight ).

Variante 6 (RANGE mit Pattern)

* Alle Einträge beginnend mit 'A' und 'L' löschen
DELETE it_sflight WHERE carrid IN VALUE rseloption( ( sign   = 'I'
                                                      option = 'CP'
                                                      low    = 'A*'
                                                      high   = '' )
                                                    ( sign   = 'I'
                                                      option = 'CP'
                                                      low    = 'L*'
                                                      high   = '' ) ).

cl_demo_output=>display( it_sflight ).

Variante 7 (RANGE leer)

* Achtung: löscht alle Einträge in it_sflight!
DELETE it_sflight WHERE carrid IN VALUE rseloption( ).

cl_demo_output=>display( it_sflight ).

Variante 8 (Pattern CP)

* alle Einträge, deren carrid mit 'L' beginnen
DELETE it_sflight WHERE carrid CP 'L*'.

cl_demo_output=>display( it_sflight ).

Variante 9 (dynamische WHERE-Condition)

* Stringtab mit WHERE-Conditions -> Leerzeichen beachten!
DATA(it_where) = VALUE stringtab(
                                  ( |testfehler = 'AA'| )
                                  ( |carrid EQ 'AA' AND connid CP '001*' | )
                                  ( |seatsmax > 300| )
                                ).

LOOP AT it_where ASSIGNING FIELD-SYMBOL(<w>).

  WRITE: / <w>.

  TRY.
* dynamische WHERE-Condition ausführen
* fehlerhafte WHERE-Conditions werfen eine Exception
      DELETE it_sflight WHERE (<w>).
    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

ENDLOOP.

cl_demo_output=>display( it_sflight ).

Variante 10 (Pseudokomponente TABLE_LINE)

* Die Tabellenzeile mit der Struktur ty_s_sflight wird gelöscht
DELETE it_sflight WHERE table_line = VALUE ty_s_sflight( carrid = 'LH' connid = '400' seatsmax = 280 flag = abap_true ).

cl_demo_output=>display( it_sflight ).

Variante 11 (CO [Contains Only])

* Carrier darf nur Zeichen 'A' und/oder 'Z' und/oder ' ' enthalten
* -> löscht Einträge für 'AA ', 'AZ '
DELETE it_sflight WHERE carrid CO 'AZ '.

cl_demo_output=>display( it_sflight ).

[ABAP] Daten von interner Tabelle lesen (Index, Key, Feldsymbol)

Typen & Daten

* Zeilentyp der internen Tabelle
TYPES : BEGIN OF ty_s_persons,
          pernr TYPE i,
          name  TYPE string,
          age   TYPE i,
        END OF ty_s_persons.

* Typ: Interne Tabelle mit Primärschlüssel pernr und Sekundärschlüssel persons
TYPES: ty_it_persons TYPE STANDARD TABLE OF ty_s_persons WITH KEY pernr
                                                         WITH NON-UNIQUE SORTED KEY persons COMPONENTS name.

* Daten
DATA(it_persons) = VALUE ty_it_persons(
                                        ( pernr = 1 name = |Udo|    age = 20 )
                                        ( pernr = 2 name = |Heinz|  age = 30 )
                                        ( pernr = 3 name = |Jochen| age = 40 )
                                        ( pernr = 4 name = |Ede|    age = 50 )
                                        ( pernr = 5 name = |Ina|    age = 60 )
                                        ( pernr = 6 name = |Erika|  age = 70 )
                                        ( pernr = 7 name = |Erna|   age = 80 )
                                      ).

Datensatz mit Index 1 auslesen

* table expressions
IF line_exists( it_persons[ 1 ] ).
* Datenstruktur
  DATA(lv_per1) = it_persons[ 1 ].
  WRITE: / lv_per1-pernr, lv_per1-name, lv_per1-age.

* Feldsymbol
  ASSIGN it_persons[ 1 ] TO FIELD-SYMBOL(<p1>).
  IF <p1> IS ASSIGNED.
    WRITE: / <p1>-pernr, <p1>-name, <p1>-age.
  ENDIF.
ENDIF.

* READ TABLE
* Datenstruktur
READ TABLE it_persons INDEX 1 INTO DATA(lv_per2).
IF sy-subrc = 0.
  WRITE: / lv_per2-pernr, lv_per2-name, lv_per2-age.
ENDIF.
* Feldsymbol
READ TABLE it_persons INDEX 1 ASSIGNING FIELD-SYMBOL(<p2>).
IF sy-subrc = 0 AND <p2> IS ASSIGNED.
  WRITE: / <p2>-pernr, <p2>-name, <p2>-age.
ENDIF.

Datensatz mit Primärschlüssel auslesen

* table expressions
IF line_exists( it_persons[ pernr = 2 ] ).
* Datenstruktur
  DATA(lv_per3) = it_persons[ pernr = 2 ].
  WRITE: / lv_per3-pernr, lv_per3-name, lv_per3-age.

* Feldsymbol
  ASSIGN it_persons[ pernr = 2 ] TO FIELD-SYMBOL(<p3>).
  IF <p3> IS ASSIGNED.
    WRITE: / <p3>-pernr, <p3>-name, <p3>-age.
  ENDIF.
ENDIF.

* READ TABLE
* Datenstruktur
READ TABLE it_persons WITH TABLE KEY pernr = 2 INTO DATA(lv_per4).
IF sy-subrc = 0.
  WRITE: / lv_per4-pernr, lv_per4-name, lv_per4-age.
ENDIF.
* Feldsymbol
READ TABLE it_persons WITH TABLE KEY pernr = 2 ASSIGNING FIELD-SYMBOL(<p4>).
IF sy-subrc = 0 AND <p4> IS ASSIGNED.
  WRITE: / <p4>-pernr, <p4>-name, <p4>-age.
ENDIF.

Datensatz mit Sekundärschlüssel auslesen

* table expressions
IF line_exists( it_persons[ KEY persons COMPONENTS name = 'Ina' ] ).
* Datenstruktur
  DATA(lv_per5) = it_persons[ KEY persons COMPONENTS name = 'Ina' ].
  WRITE: / lv_per5-pernr, lv_per5-name, lv_per5-age.

* Feldsymbol
  ASSIGN it_persons[ KEY persons COMPONENTS name = 'Ina' ] TO FIELD-SYMBOL(<p5>).
  IF <p5> IS ASSIGNED.
    WRITE: / <p5>-pernr, <p5>-name, <p5>-age.
  ENDIF.
ENDIF.

* READ TABLE
* Datenstruktur
READ TABLE it_persons WITH KEY persons COMPONENTS name = 'Ina' INTO DATA(lv_per6).
IF sy-subrc = 0.
  WRITE: / lv_per6-pernr, lv_per6-name, lv_per6-age.
ENDIF.
* Feldsymbol
READ TABLE it_persons WITH KEY persons COMPONENTS name = 'Ina' ASSIGNING FIELD-SYMBOL(<p6>).
IF sy-subrc = 0 AND <p6> IS ASSIGNED.
  WRITE: / <p6>-pernr, <p6>-name, <p6>-age.
ENDIF.