ABAP Guide: ABAP SQL on a Local Database

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Checked on 2026-11-13 against open-abap 2.14.30 (the abaplint transpiler and runtime, with open-abap-core a9acc41), abaplint 2.120.103 and Node.js 24.21.

What you build

You write one report named zsql_claims that works on a database table of expense claims. It inserts five rows, reads them back in several ways (all rows, filtered rows, sorted rows, the two largest, a count, a single row) and then changes the table with INSERT, UPDATE, MODIFY and DELETE. After each statement you read the result that the program prints, and at the end you make a deliberate spelling mistake and read the error message.

The table is zexp_claim, the table that the ABAP lab already holds in src/zexp_claim.tabl.xml. The lab table is a reduced version of the full claim table used in the reference pages: it has only three columns, claim_id (the key, ten characters), status (one character: A approved, S submitted, R rejected) and amount (a whole number). Behind it the lab keeps a small in-memory SQLite database. Nobody has to install or configure it: the lab creates it, empty, every time a program starts.

The playground is open-abap, a community open-source project, not SAP software. It runs only the part of ABAP SQL that it implements, so this guide uses only statements that were run in it. Results on a real SAP system follow the same rules, but check a program there before you rely on it. Output is written with WRITE, the classic list statement, because that is what the playground offers.

Terminal with the command npm run run:sql and the end of its output: Found C000000003 A 80, No row for C999999999, the sy-subrc lines of INSERT, UPDATE, MODIFY and DELETE, and the table After the changes with six rows

Before you start

  • No account is needed: Node.js, the abaplint tools and open-abap install from the public npm registry without signing in. The licences you accept by installing (MIT for the abaplint packages) are listed in the README of the ABAP lab.

  • Set up the lab first, with the guide Set Up a Local ABAP Playground: the folder abap-lab with npm install done, and an editor such as Visual Studio Code open on it. Do the guide Your First ABAP Program too, so that a report, a WRITE statement and the terminal commands are familiar.

  • The lab already holds the finished file of this guide, modules/ROOT/examples/abap-lab/src/zsql_claims.prog.abap. Type it yourself, block by block, rather than copying it.

  • Read ABAP SQL: Reading Data and ABAP SQL: Writing Data if you want the reasons behind each statement. This guide only runs them.

  • The figures are drawings of the editor and the terminal, not screenshots. A highlighted box with a number marks the part of the figure that belongs to the step with the same number.

Steps

Two ideas help from the first line. An ABAP SQL statement is a normal ABAP statement that talks to the database, such as SELECT. A variable that you use inside it is written with a leading @, for example @DATA(all_claims): the @ tells ABAP that the name is a variable of your program and not a column of the table. After every statement the system field sy-subrc tells whether it worked: 0 means success, 4 means that nothing matched or the row could not be written.

Write the report and load the table

  1. In the src folder create src/zsql_claims.prog.abap. Start with the REPORT line, a table type for rows of zexp_claim, and a small local class lcl_output whose method show prints a title and then one line per row. You will call it after every read, so the guide does not repeat the loop.

    REPORT zsql_claims.
    
    TYPES ty_claims TYPE STANDARD TABLE OF zexp_claim WITH EMPTY KEY.
    
    CLASS lcl_output DEFINITION.
      PUBLIC SECTION.
        CLASS-METHODS show IMPORTING title TYPE string rows TYPE ty_claims.
    ENDCLASS.
    
    CLASS lcl_output IMPLEMENTATION.
      METHOD show.
        WRITE / title.
        LOOP AT rows INTO DATA(row).
          WRITE / |  { row-claim_id } { row-status } { row-amount }|.
        ENDLOOP.
      ENDMETHOD.
    ENDCLASS.
  2. Add the program’s start block. INSERT zexp_claim FROM TABLE writes all the rows of an internal table in one statement, and the @( VALUE ty_claims( ... ) ) expression builds that table from five lines. The type name must be written out here (ty_claims, not #). SELECT * FROM zexp_claim INTO TABLE then reads every row into the new table all_claims, and show prints it.

    START-OF-SELECTION.
      INSERT zexp_claim FROM TABLE @( VALUE ty_claims(
        ( claim_id = 'C000000001' status = 'A' amount = 120 )
        ( claim_id = 'C000000002' status = 'S' amount = 35 )
        ( claim_id = 'C000000003' status = 'A' amount = 80 )
        ( claim_id = 'C000000004' status = 'R' amount = 10 )
        ( claim_id = 'C000000005' status = 'S' amount = 64 ) ) ).
      WRITE / |Inserted five rows, sy-subrc = { sy-subrc }|.
    
      SELECT * FROM zexp_claim INTO TABLE @DATA(all_claims).
      lcl_output=>show( title = `All claims` rows = all_claims ).
  3. Create the attribute file src/zsql_claims.prog.xml next to it, the same kind of file as for the first program:

    <?xml version="1.0" encoding="utf-8"?>
    <abapGit version="v1.0.0" serializer="LCL_OBJECT_PROG" serializer_version="v1.0.0">
     <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
       <PROGDIR>
        <NAME>ZSQL_CLAIMS</NAME>
        <SUBC>1</SUBC>
        <RLOAD>E</RLOAD>
        <UCCHECK>X</UCCHECK>
       </PROGDIR>
      </asx:values>
     </asx:abap>
    </abapGit>
    Visual Studio Code with the abap-lab project: the file src/zsql_claims.prog.abap is open and shows the REPORT line, the table type and the local class lcl_output with its show method, then START-OF-SELECTION with an INSERT of five rows and a SELECT into the table all_claims; callout 1 marks the helper class, callout 2 marks the INSERT and SELECT block and callout 3 marks the companion XML file in the explorer
  4. Add a script to the scripts section of package.json that transpiles and then runs the report. The && makes the run stop when the transpile fails, so you never run an old build by mistake (the lab’s own package.json already has it):

    "run:sql": "npm run transpile && node output/project/zsql_claims.prog.mjs"

    Then run it in the terminal, in the abap-lab folder:

    npm run run:sql
  5. Read the output. The transpiler first prints a few lines about the files it found; the figures leave them out. Then come the lines of your program: sy-subrc = 0 after the INSERT, the title All claims and the five rows. Each row line shows the claim id, the status and the amount.

    Inserted five rows, sy-subrc = 0
    All claims
      C000000001 A 120
      C000000002 S 35
      C000000003 A 80
      C000000004 R 10
      C000000005 S 64
    Terminal after npm run run:sql: the program prints Inserted five rows with sy-subrc 0 and then All claims with the five rows C000000001 A 120, C000000002 S 35, C000000003 A 80, C000000004 R 10 and C000000005 S 64; callout 4 marks the command and callout 5 marks the output

Read rows with SELECT

Every block below goes at the end of the file, after the previous one, inside the start block (two spaces of indentation, as in the figures). The program reads the same five rows again and again; only the condition changes.

  1. Filter with WHERE. The first read keeps the rows whose status is in a list (IN), the second the rows whose amount lies between two limits (BETWEEN, which includes both limits), the third the rows whose id starts with a given text (LIKE, where % stands for any characters) and whose status is S.

      SELECT * FROM zexp_claim
        WHERE status IN ( 'A', 'S' )
        INTO TABLE @DATA(status_a_or_s).
      lcl_output=>show( title = `Status A or S` rows = status_a_or_s ).
    
      SELECT * FROM zexp_claim
        WHERE amount BETWEEN 30 AND 100
        INTO TABLE @DATA(mid_range).
      lcl_output=>show( title = `Amount 30 to 100` rows = mid_range ).
    
      SELECT * FROM zexp_claim
        WHERE claim_id LIKE 'C00000000%' AND status = 'S'
        INTO TABLE @DATA(like_rows).
      lcl_output=>show( title = `Id like C00000000% and status S` rows = like_rows ).
  2. Sort and limit. ORDER BY amount DESCENDING puts the largest amount first, UP TO 2 ROWS stops after two rows (so the two largest), and SELECT DISTINCT returns each different status once. lines( ) is a built-in function that returns the number of rows of a table.

      SELECT * FROM zexp_claim
        ORDER BY amount DESCENDING
        INTO TABLE @DATA(largest_first).
      lcl_output=>show( title = `Largest first` rows = largest_first ).
    
      SELECT * FROM zexp_claim
        ORDER BY amount DESCENDING
        INTO TABLE @DATA(top_two)
        UP TO 2 ROWS.
      lcl_output=>show( title = `Top two` rows = top_two ).
    
      SELECT DISTINCT status FROM zexp_claim
        ORDER BY status
        INTO TABLE @DATA(statuses).
      WRITE / |Different statuses: { lines( statuses ) }|.
  3. Count rows with COUNT( * ), once for the whole table and once with a WHERE condition. The result is one number, read into an inline-declared variable.

      SELECT COUNT( * ) FROM zexp_claim INTO @DATA(total).
      WRITE / |Rows in table: { total }|.
    
      SELECT COUNT( * ) FROM zexp_claim WHERE status = 'S' INTO @DATA(submitted).
      WRITE / |Submitted claims: { submitted }|.
  4. Read one row with SELECT SINGLE. It fills a structure instead of a table. The first read finds claim C000000003, the second asks for a claim that does not exist, so sy-subrc is 4 and the structure keeps its old content.

      SELECT SINGLE * FROM zexp_claim WHERE claim_id = 'C000000003' INTO @DATA(claim).
      WRITE / |Found { claim-claim_id } { claim-status } { claim-amount }, subrc { sy-subrc }|.
    
      SELECT SINGLE * FROM zexp_claim WHERE claim_id = 'C999999999' INTO @claim.
      WRITE / |No row for C999999999, sy-subrc = { sy-subrc }|.
  5. Run npm run run:sql again and read the new lines after the first listing. Every group of lines belongs to one step: the three WHERE reads, the sorted reads and the number of different statuses, the two counts, and the single-row reads.

    Terminal after npm run run:sql with the read statements added: the output groups Status A or S, Amount 30 to 100, Id like C00000000% and status S, Largest first, Top two, Different statuses 3, Rows in table 5, Submitted claims 2, then Found C000000003 A 80 with subrc 0 and No row for C999999999 with sy-subrc 4; callouts 6 to 9 mark the groups of steps 6 to 9 and callout 10 marks the command

    The figure leaves out the first lines of the output, which you saw in step 5. Look at the results: status A or S leaves four rows (the rejected claim is gone), amounts from 30 to 100 leave three rows, the id pattern together with status S leaves two, the largest amount is 120, the two largest are 120 and 80, the table holds three different statuses, five rows in all and two submitted claims.

Change rows with INSERT, UPDATE, MODIFY and DELETE

The next four blocks change the table. They run in this order, and each one reports sy-subrc.

  1. INSERT adds one row. The row is built with VALUE #( ... ), and the # is allowed here because the target table fixes the type.

  2. UPDATE changes rows that match a WHERE condition. The first statement sets the status of claim C000000002 to A and raises its amount by 5 (amount = amount + 5, so 35 becomes 40). The second asks for a claim that does not exist: nothing changes and sy-subrc is 4.

  3. MODIFY writes a row whether or not it exists: a row with an existing key is replaced, a row with a new key is inserted. The first MODIFY replaces claim C000000004 (its status changes from R to S), the second inserts a new claim C000000007 with status R.

  4. DELETE FROM removes the rows that match its condition. Only claim C000000007 still has status R (the MODIFY changed claim 4), so only it disappears. A last SELECT lists the table.

      INSERT zexp_claim FROM @( VALUE #( claim_id = 'C000000006'
                                         status   = 'S'
                                         amount   = 42 ) ).
      WRITE / |INSERT, sy-subrc = { sy-subrc }|.
      UPDATE zexp_claim
        SET status = 'A', amount = amount + 5
        WHERE claim_id = 'C000000002'.
      WRITE / |UPDATE, sy-subrc = { sy-subrc }|.
    
      UPDATE zexp_claim SET status = 'A' WHERE claim_id = 'C999999999'.
      WRITE / |UPDATE without a match, sy-subrc = { sy-subrc }|.
      MODIFY zexp_claim FROM @( VALUE #( claim_id = 'C000000004'
                                         status   = 'S'
                                         amount   = 10 ) ).
      MODIFY zexp_claim FROM @( VALUE #( claim_id = 'C000000007'
                                         status   = 'R'
                                         amount   = 7 ) ).
      WRITE / |MODIFY twice, sy-subrc = { sy-subrc }|.
      DELETE FROM zexp_claim WHERE status = 'R'.
      WRITE / |DELETE, sy-subrc = { sy-subrc }|.
    
      SELECT * FROM zexp_claim INTO TABLE @DATA(after_changes).
      lcl_output=>show( title = `After the changes` rows = after_changes ).

    Source: INSERT, UPDATE, MODIFY and DELETE (ABAP Keyword Documentation, writable_obj)

    Visual Studio Code with the file src/zsql_claims.prog.abap open at the end of the report: INSERT of claim C000000006, UPDATE of claim C000000002 and of a claim that does not exist, two MODIFY statements, DELETE FROM with status R and a final SELECT that lists the table; callouts 11 to 14 mark the INSERT, the UPDATE, the MODIFY and the DELETE blocks

    The figure shows these four blocks only, the end of the file.

  5. Run npm run run:sql. The last lines of the output show sy-subrc for each change (0 for every statement that found its row, 4 for the update without a match) and the table after the changes: six rows, claim 2 now A 40, claim 4 now S 10, the new claim 6, and no claim 7.

Read your first error

Errors are normal. You make one on purpose, where you know the fix. The complete file at this point has the same blocks as the lab file, without its tag comment lines.

  1. Open src/zsql_claims.prog.abap and, in the SELECT DISTINCT statement of step 7, change status in the field list (the word right after DISTINCT) to statuz. Save the file.

  2. Run npm run run:sql. The transpiler checks the program before it translates anything, stops at the first problem and prints the message; Node.js does not run. The message has four parts: check_syntax is the check that failed, checkFields, field STATUZ not found says that the table has no such column, and zsql_claims.prog.abap:57 is the file and the first line of the statement. A few more lines about the transpiler’s own code follow; ignore them.

    Terminal after npm run run:sql with a misspelled field in a SELECT: the transpiler stops with the line Error: check_syntax, checkFields, field STATUZ not found, zsql_claims.prog.abap:57 and the program does not run; callout 17 marks the error line
  3. Run npm run lint to ask abaplint, the static checker of the lab, about the same file. It finds the same problem. The finding reads file[line, column] - message (rule) [severity]: line 57, column 3 is where the statement starts, check_syntax is the rule, and [E] means error.

    npm run lint
    Terminal after npm run lint: abaplint prints one finding, src/zsql_claims.prog.abap at line 57 column 3, the message checkFields field STATUZ not found, the rule name check_syntax and the severity E, then 1 issue found in 12 files; callout 18 marks the finding
  4. Change statuz back to status, save and run npm run run:sql. The output returns, and npm run lint reports 0 issue(s) found.

Check the result

npm run run:sql ends without an error. Its output begins with Inserted five rows, sy-subrc = 0 and the five rows, contains the read groups of step 10, and ends with After the changes and these six rows:

  C000000001 A 120
  C000000002 A 40
  C000000003 A 80
  C000000004 S 10
  C000000005 S 64
  C000000006 S 42

npm run lint prints abaplint: 0 issue(s) found, and npm test still passes with the lab’s own test class. Run npm run run:sql a second time: the output is the same, because the database starts empty on each run.

Variations

  • Change a condition and predict the rows before you run: WHERE amount > 60, WHERE status <> 'A' or ORDER BY claim_id DESCENDING.

  • Change UP TO 2 ROWS to another number, or remove ORDER BY from that read and notice that without it the database decides which rows come first.

  • Change the key of the first MODIFY to a claim that does not exist yet and read the table again: the row is inserted instead of replaced.

What can go wrong

Symptom Cause Fix

Error: check_syntax, checkFields, field STATUZ not found, zsql_claims.prog.abap:57 and no program output

A column name in the field list of a SELECT is misspelled; the table zexp_claim has claim_id, status and amount only.

Open the file and the line named at the end of the message and correct the name.

A read prints its title but no rows, and neither the transpiler nor npm run lint reports anything

A column name in the WHERE condition is misspelled (the test used amont for amount). The lab does not check names in WHERE, so the condition matches nothing.

When a list is empty although rows exist, read the condition and compare each name with the table.

INSERT, sy-subrc = 4 and the table has no new row

INSERT does not replace a row: the key already exists (the test inserted claim C000000001 again).

Use a new key, or use MODIFY when an existing row may be replaced. Check sy-subrc after every write.

Error: TypeNameOrInfer, type not found: #, ZSQL_CLAIMS, zsql_claims.prog.abap line 20 and no output

In the lab, VALUE #( ... ) written inside INSERT ... FROM TABLE @( ... ) has no type to infer, and npm run lint does not report it.

Write the type name: VALUE ty_claims( ... ).

The old output appears although you changed the code

You ran node output/project/zsql_claims.prog.mjs yourself after a failed transpile. Node.js ran the files of the last successful build.

Run npm run run:sql: it runs the program only when the transpile succeeded.

Where to go next