|
| 1 | +--- |
| 2 | +outline: [2, 4] |
| 3 | +--- |
| 4 | +# ABAP SQL |
| 5 | + |
| 6 | +ABAP SQL is the standard way to read and change data in the database directly from ABAP. In an abap2UI5 controller you can issue `SELECT`, `INSERT`, `UPDATE`, `DELETE`, and `MODIFY` statements the same way as in any ABAP program, and bind the result straight to a UI5 view. |
| 7 | + |
| 8 | +### Read Data |
| 9 | + |
| 10 | +The example below selects flights from the `sflight` table and shows them in a UI5 table: |
| 11 | +```abap |
| 12 | +CLASS z2ui5_cl_sample_sql DEFINITION PUBLIC. |
| 13 | +
|
| 14 | + PUBLIC SECTION. |
| 15 | + INTERFACES z2ui5_if_app. |
| 16 | + DATA mt_flights TYPE STANDARD TABLE OF sflight WITH EMPTY KEY. |
| 17 | +
|
| 18 | +ENDCLASS. |
| 19 | +
|
| 20 | +CLASS z2ui5_cl_sample_sql IMPLEMENTATION. |
| 21 | + METHOD z2ui5_if_app~main. |
| 22 | +
|
| 23 | + IF client->check_on_init( ). |
| 24 | +
|
| 25 | + SELECT FROM sflight |
| 26 | + FIELDS carrid, connid, fldate, price, currency |
| 27 | + ORDER BY carrid, connid, fldate |
| 28 | + INTO TABLE @mt_flights |
| 29 | + UP TO 50 ROWS. |
| 30 | +
|
| 31 | + DATA(view) = z2ui5_cl_xml_view=>factory( )->page( ). |
| 32 | + DATA(table) = view->table( client->_bind( mt_flights ) ). |
| 33 | +
|
| 34 | + table->columns( |
| 35 | + )->column( )->text( `Carrier` )->get_parent( |
| 36 | + )->column( )->text( `Connection` )->get_parent( |
| 37 | + )->column( )->text( `Date` )->get_parent( |
| 38 | + )->column( )->text( `Price` ). |
| 39 | +
|
| 40 | + table->items( )->column_list_item( )->cells( |
| 41 | + )->text( `{CARRID}` |
| 42 | + )->text( `{CONNID}` |
| 43 | + )->text( `{FLDATE}` |
| 44 | + )->text( `{PRICE} {CURRENCY}` ). |
| 45 | +
|
| 46 | + client->view_display( view->stringify( ) ). |
| 47 | +
|
| 48 | + ENDIF. |
| 49 | +
|
| 50 | + ENDMETHOD. |
| 51 | +ENDCLASS. |
| 52 | +``` |
| 53 | + |
| 54 | +### Filter with a Search Field |
| 55 | + |
| 56 | +Bind the search term with `_bind_edit( )` and re-run the `SELECT` on every `SEARCH` event: |
| 57 | +```abap |
| 58 | +CASE abap_true. |
| 59 | +
|
| 60 | + WHEN client->check_on_init( ). |
| 61 | + load_data( ). |
| 62 | + render( ). |
| 63 | +
|
| 64 | + WHEN client->check_on_event( `SEARCH` ). |
| 65 | + load_data( ). |
| 66 | + client->view_model_update( ). |
| 67 | +
|
| 68 | +ENDCASE. |
| 69 | +``` |
| 70 | + |
| 71 | +```abap |
| 72 | +METHOD load_data. |
| 73 | +
|
| 74 | + SELECT FROM scarr |
| 75 | + FIELDS carrid, carrname, url |
| 76 | + WHERE @mv_search = '' |
| 77 | + OR carrname LIKE @( |%{ mv_search }%| ) |
| 78 | + INTO TABLE @mt_carriers |
| 79 | + UP TO 100 ROWS. |
| 80 | +
|
| 81 | +ENDMETHOD. |
| 82 | +``` |
| 83 | + |
| 84 | +### Aggregations and Joins |
| 85 | + |
| 86 | +Aggregations and joins work like in any ABAP report — the result table is then bound to the view: |
| 87 | +```abap |
| 88 | +SELECT FROM sflight AS f |
| 89 | + INNER JOIN scarr AS c ON c~carrid = f~carrid |
| 90 | + FIELDS f~carrid, c~carrname, SUM( f~seatsocc ) AS total_seats |
| 91 | + GROUP BY f~carrid, c~carrname |
| 92 | + ORDER BY total_seats DESCENDING |
| 93 | + INTO TABLE @DATA(lt_stats) |
| 94 | + UP TO 20 ROWS. |
| 95 | +``` |
| 96 | + |
| 97 | +### Change Data |
| 98 | + |
| 99 | +`INSERT`, `UPDATE`, `DELETE`, and `MODIFY` are issued from event handlers — wrap them in a transaction and commit explicitly: |
| 100 | +```abap |
| 101 | +WHEN client->check_on_event( `SAVE` ). |
| 102 | +
|
| 103 | + MODIFY zorders FROM TABLE @mt_orders. |
| 104 | + IF sy-subrc = 0. |
| 105 | + COMMIT WORK. |
| 106 | + ELSE. |
| 107 | + ROLLBACK WORK. |
| 108 | + ENDIF. |
| 109 | +``` |
| 110 | + |
| 111 | +::: tip |
| 112 | +For data sourced from the Virtual Data Model, prefer reading from a [CDS view](./cds.md) instead of base tables — you get business semantics, associations, and authorization checks out of the box. |
| 113 | +::: |
0 commit comments