Code examples · Modern ABAP · Beginner
Your first SAP data read: look up a customer in table KNA1
The problem
A colleague gives you a customer number and asks: what is the name and the city of this customer?
How we solve it
- 1Askwhich customer?
- 2ReadSELECT from KNA1
- 3Answerfound or not
- 4Listfirst five
The result
The program prints the customer, or a clear message when it does not exist, and then five customers from the table.
SAP objects in play
1. The requirement
A colleague gives you a customer number and asks: what is the name and the city of this customer? You must answer from the SAP system with a small program, and the program must say clearly when there is no such customer.
2. How we design it
SAP keeps every customer in a database table named KNA1, one row per customer. A table is like a spreadsheet: the columns are fields (KUNNR is the customer number, NAME1 the name, ORT01 the city) and each row is one record. We ask the database for one row with SELECT SINGLE, using the customer number as the search key. Afterwards we look at SY-SUBRC, a system field that says whether a row was found: 0 means found, 4 means not found.
Why we decided it this way
- 1Read only the fields we needAsking for three columns instead of the whole row is faster and shows exactly what the program uses.
- 2Always check SY-SUBRCIf the customer does not exist, the result is empty. A program that ignores this prints blanks and nobody knows why.
- 3A full ten-digit customer numberSAP stores customer numbers with leading zeros, so 1000 is stored as 0000001000. Searching for 1000 would find nothing.
Before you start
- A sandbox S/4HANA or ECC system with sample customers. The small ABAP trial images do not contain the SAP business tables.
- Look at the table first: open transaction SE16N, enter KNA1 and press Execute. You see the customers as rows. Copy one customer number from the column KUNNR into the program.
- ADT (ABAP Development Tools) with a package such as $TMP.
3. The code, step by step
Step 1 of 4: Create the class and name the customer
A program you can run, with the customer number you are looking for.
" Class you can run with F9
CLASS zcl_first_customer DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_first_customer IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
" The customer number to look for
DATA(customer_no) = CONV kunnr( '0000001000' ).
Part by part (plain text)
- Class you can run with F9. This frame is the same in all runnable examples. The interface IF_OO_ADT_CLASSRUN gives the class a method called MAIN. When you press F9 in ADT, SAP runs MAIN and shows everything written to OUT in the console.
- The customer number to look for. KUNNR is the data element of a customer number. CONV gives the text the right type. The number has ten digits with leading zeros, exactly as SAP stores it. Replace it with a customer that exists in your system.
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.
SAP tables
If it does not work
4. The final outcome
The program prints the customer, or a clear message when it does not exist, and then five customers from the table.
What you should see
Example output (your system has other data): 1000: Example Company, Berlin (DE) (or, when the number does not exist: Customer 0000099999 does not exist) First five customers: 1000 Example Company 1001 Sample Trading 1002 Demo Retail ...
Try it and check the result
- Activate with Ctrl+F3 and run with F9. The console shows the customer line and the list of five customers.
- Change the customer number to one that does not exist, for example 0000099999: the program prints that the customer does not exist.
- Open KNA1 in SE16N and compare: the five customers in the console are the first five rows ordered by KUNNR.
Common mistakes
- Searching for 1000 instead of 0000001000 finds nothing: customer numbers are stored with leading zeros.
- Reading a big table without UP TO n ROWS or a WHERE condition can take minutes in a real system.
- In a system built for ABAP Cloud, KNA1 is not released: the syntax check refuses it. There you read the released CDS view I_Customer instead.