ERPClimb logoERPClimb

Code examples · Modern ABAP · Beginner

ABAP SQL: create a table, then insert, read, update and delete rows

The problem

A small booking table must hold a booking number, a customer, an amount and a currency.

How we solve it

  1. 1TableZBOOKING
  2. 2InsertINSERT
  3. 3ReadSELECT
  4. 4ChangeUPDATE, DELETE

The result

Two rows are inserted, one is read and raised, one is deleted and one row remains.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A small booking table must hold a booking number, a customer, an amount and a currency. A test program must add two bookings, read one back, raise its amount, delete the other and report how many rows are left.

2. How we design it

First the table, defined as source code in ADT with a client field and a key. Then one class that walks through the four database operations in the order a real program would use them. After each operation we look at the system fields: SY-SUBRC tells whether the statement found or changed something, SY-DBCNT how many rows it touched.

Why we decided it this way

  1. 1Client field as first key fieldThe client field separates the data of each client. ABAP SQL fills it for you, so you never write it in a statement.
  2. 2Amount with a currency keyAn amount without its currency is meaningless. The annotation links the two fields so SAP knows how many decimals the currency has.
  3. 3Internal table for the insertOne INSERT of a whole table is one database round trip instead of one per row.
  4. 4Always a WHERE on UPDATE and DELETEWithout WHERE the statement touches every row of the table.

Before you start

  • ADT and a package such as $TMP or your own Z package. Use a sandbox or trial system, because the program deletes all rows of the demo table first.

3. The code, step by step

Step 1 of 4: Define the table

A transparent table with a client, a key and four fields.

CDS · Database table · ZBOOKING
// Table properties and key
@EndUserText.label : 'Demo booking'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zbooking {
  key client     : abap.clnt not null;
  key booking_id : abap.numc(6) not null;
  // Customer, amount and currency
  customer       : abap.char(40);
  @Semantics.amount.currencyCode : 'zbooking.currency'
  amount         : abap.curr(15,2);
  currency       : abap.cuky;
}
Part by part (plain text)
  1. Table properties and key. The annotations describe the table: its label, that it is a normal transparent table, delivery class A (application data) and that data is maintained by programs rather than by a generic maintenance screen. CLIENT is the first key field, then the booking number.
  2. Customer, amount and currency. The amount field points to the currency field through the Semantics annotation. ABAP.CURR(15,2) is an amount with two decimals and ABAP.CUKY is the currency key, for example EUR.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

4. The final outcome

Two rows are inserted, one is read and raised, one is deleted and one row remains.

What you should see

Inserted 2 row(s) Acme GmbH pays 100.00 New amount of booking 000001: 120.00 Deleted 1 row(s) Rows left: 1

Try it and check the result

  • Activate the table first, then the class, and run the class with F9. The console shows the five lines of the outcome.
  • Open the table in ADT and press F8 (data preview): one row, 000001, with amount 120.00.
  • Run the class twice: the result is the same, because the first statement empties the table.

Common mistakes

  • UPDATE or DELETE without WHERE changes every row of the table. In real programs read the WHERE twice.
  • Check SY-SUBRC straight after the statement. Any other statement in between can overwrite it.
  • The decision when data becomes permanent (the commit) belongs to the transaction as a whole. Do not scatter COMMIT WORK through helper methods.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: ABAP 7.54+ / S/4HANA on-premise 2020+ and ABAP Environment on BTP. Check on your release.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.