ABAP CRUD (Create, Read, Update, Delete) Tutorial

Today’s ABAP lesson is a continuation of our previous exercise on creating a custom Z-table in SAP. If you haven’t completed that lesson, please check it out first, as we will be using the table from that exercise. However, if you’re already familiar with creating custom tables in SAP, let’s jump straight into the tutorial.

Tutorial Objectives
1. Know how to declare an internal table for the transparent table
2. Learn how to CREATE new entry, READ entry, UPDATE and DELETE entry in the transparent table
Prerequisites
1. SAP System Access: SAP GUI, ECC.
2. Authorization: Developer role, TCODE: SE38, SE16N, SE11.

ABAP CRUD In Action

After creating your first custom table in SAP, it’s time to perform some basic database operations such as adding or creating entries, reading the records, updating them, and deleting records. These operations, commonly known as CRUD (Create, Read, Update, Delete), are essential for managing data in your custom table.

In the previous lesson we created an employees table called ZEMPLOYEE. Which consists of 4 fields ( EMP_ID, EMP_NM, BIRTHDATE, CITY ) with MANDT and EMP_ID as the primary keys as shown below.

Now let’s start the first database operation which creating new entries into the custom table.

How To Create New Entries To Your Custom ZTable in ABAP

A table consists of records, where each record represents a single row of data. To work with these records in ABAP, we need to declare two variables: a structure (or line type) to represent individual records and a table type to hold multiple records.

First let’s create a new custom ABAP Program, for this lesson I created ZABAPFILLTABLE.

Next, we need to declare it_temp with the TABLE TYPE OF ZEMPLOYEE. It_emp will serve as our internal table to store employee records. Additionally, we’ve defined wa_emp as the working area, which refers to the structure of ZEMPLOYEE and is used to handle individual record.


DATA: it_emp TYPE TABLE OF zemployee,
      wa_emp TYPE zemployee.

Now you’re ready to assign values for your first record.

TIPS: After typing wa_emp-, press CTRL+ SPACE to display all the available fields in your structure

Now add the codes below in your ABAP Program.

wa_emp-emp_id = 'EM1'.
wa_emp-emp_nm = 'Billy'.
wa_emp-birthdate = '20001010'.
wa_emp-city = 'Boston'.
INSERT INTO zemployee VALUES wa_emp.

wa_emp-emp_id = 'EM2'.
wa_emp-emp_nm = 'Steve'.
wa_emp-birthdate = '19791112'.
wa_emp-city = 'California'.
INSERT INTO zemployee VALUES wa_emp.

MESSAGE 'New Entries Created Successfully!' TYPE 'S'.

Save and Activate the program. You will see below message after you process the program (F8).

Now execute TCODE : SE16N to display the ZEMPLOYEE records.

Enter the Table’s name and click on the EXECUTE button.

Here’s the result. We have successfully created 2 new records in our custom z table.

How To Read Entries From Transparent Table using ABAP

Okay now we want to read or make a query from that table and display it on your ABAP program.

To make a query we need to do some SQL or in ABAP we called it Open SQL.

Here’s the basic syntax.

SELECT <FIELDS> INTO TABLE <INTERNAL_TABLE>
FROM <TRANSPARENT_TABLE>
WHERE <CONDITION>.

The INTO TABLE statement will add all the entries into an internal table.

After performing the selection query, the next step is to display the retrieved data on your screen. To do this, we loop through the entries using a LOOP statement and use the WRITE statement to output the results.

Here’s an example code snippet to demonstrate this process

"SELECT all fields, all records
"from custom table Zemployee and store it in IT_TEMP internal table
SELECT * INTO TABLE it_emp
  FROM zemployee.

"Loop through the internal table and pass each line into wa_emp structure / line type
LOOP AT it_emp INTO wa_emp.
  "display each record on your screen using the WRITE statement
  WRITE:/ 'Emp ID:', wa_emp-emp_id,
          'Emp Name:', wa_emp-emp_nm,
          'Birth Date:', wa_emp-birthdate,
          'City:', wa_emp-city.
ENDLOOP.

TIPS: To remark the previous code you can block the entire block, and then press CTRL + , (comma), to un-remark CTRL + . (dot).

Block The Code

Press CTRL + , (comma)

Save, Activate and click on the processing button (F8) and here’s the result.

How To Update Entries in Transparent Table using ABAP

Here’s the basic syntax for updating the entries in your transparent table.

UPDATE <transparent_table_name> 
  SET <field> = <new_value>
  WHERE <field> = <condition>.

Here’s an example of how to update the ZEMPLOYEE table to change the employee’s city from California to Sacramento for an employee with EMP_ID = ‘EM2’. To see the changes, select the entries again and display the output.

"Update the single record
UPDATE zemployee
  SET city = 'Sacramento'
  WHERE emp_id = 'EM2'.

"Select all entries
SELECT * INTO TABLE it_emp
  FROM zemployee.

"Display the new record, you will see the employee's city with EMP_ID = 'EM2'
"has been changed from Californio to Sacramento

LOOP AT it_emp INTO wa_emp.
  WRITE:/ 'Emp ID:', wa_emp-emp_id,
          'Emp Name:', wa_emp-emp_nm,
          'Birth Date:', wa_emp-birthdate,
          'City:', wa_emp-city.
ENDLOOP.

Here’s the result, and as you can see the city for Steve (EM2) has been changed from California to Sacramento.

How To Delete Entries From Transparent Table using ABAP

Now to delete the table’s entries we will add some codes that would display a pop up window confirming the deletion and if the user select yes, then the deletion process continues, if not then the deletion process is cancelled.

  DELETE FROM <Transparent_table_name>
    WHERE <field_name> = <field_value>.

To show the confirmation pop up window, we will use SAP ABAP built-in function module which is called POPUP_TO_CONFIRM and then get the user decision’s input to determine the deletion process.

You can insert the the POPUP_TO_CONFIRM function module in your ABAP Program by clicking the Pattern button on the toolbar.

Then Enter: popup_to_confirm in the CALL FUNCTION field and then PRESS ENTER

Now the function name and all the function’s import and export parameters automatically inserted into your ABAP Program.

Next enter the values as follow:

DATA lv_ans type c.

call function 'POPUP_TO_CONFIRM'
  exporting
    TITLEBAR                    = 'Delete Confirmation'
*   DIAGNOSE_OBJECT             = ' '
    text_question               = 'Are you sure want to delete this record?'
    TEXT_BUTTON_1               = 'Yes'
*   ICON_BUTTON_1               = ' '
    TEXT_BUTTON_2               = 'No'
*   ICON_BUTTON_2               = ' '
*   DEFAULT_BUTTON              = '1'
*   DISPLAY_CANCEL_BUTTON       = 'X'
*   USERDEFINED_F1_HELP         = ' '
*   START_COLUMN                = 25
*   START_ROW                   = 6
*   POPUP_TYPE                  =
*   IV_QUICKINFO_BUTTON_1       = ' '
*   IV_QUICKINFO_BUTTON_2       = ' '
  IMPORTING
    ANSWER                      = lv_ans
* TABLES
*   PARAMETER                   =
* EXCEPTIONS
*   TEXT_NOT_FOUND              = 1
*   OTHERS                      = 2
          .
if sy-subrc <> 0.
* Implement suitable error handling here
endif.

if lv_ans = '1'. "User selected Yes
  DELETE FROM zemployee
    WHERE emp_id = 'EM2'.
  MESSAGE 'Record has been deleted successfully!' TYPE 'S'.
else.
  MESSAGE 'Deletion is cancelled!' TYPE 'S' DISPLAY LIKE 'W'.
endif.

Save, Activate and Process the Program (F8).

You will be prompted with a pop up window confirming the deletion process.

If you choose Yes then the record will be deleted.

The employee with ID ‘EM2’ has been deleted from the table ZEMPLOYEE.

The MODIFY Statement For UPSERT process (Update, Insert)

This last statement probably the most useful syntax for updating and inserting records into your transparent table because the MODIFY statement will overwrite / update the existing record (with the same ID) and insert a new one if the record has a new ID.

In below codes, you will see that we’re using the same MODIFY statement for updating and inserting records then ABAP will automatically checks for the entries, if the key already existed then it will update the record BUT new ID’s will trigger the INSERT statement.

wa_emp-emp_id = 'EM1'.
wa_emp-emp_nm = 'Billy'.
wa_emp-birthdate = '20001010'.
wa_emp-city = 'San Francisco'.
"This will update the existing record
"Because EM1 already existed
MODIFY zemployee FROM wa_emp.

wa_emp-emp_id = 'EM2'.
wa_emp-emp_nm = 'Merry'.
wa_emp-birthdate = '19880920'.
wa_emp-city = 'Denver'.
"This will create new entry
"Because EM2 does not exist yet
MODIFY zemployee FROM wa_emp.

SELECT * INTO TABLE it_emp
  FROM zemployee.

"Loop through the internal table and pass each line into wa_emp structure / line type
LOOP AT it_emp INTO wa_emp.
  "display each record on your screen using the WRITE statement
  WRITE:/ 'Emp ID:', wa_emp-emp_id,
          'Emp Name:', wa_emp-emp_nm,
          'Birth Date:', wa_emp-birthdate,
          'City:', wa_emp-city.
ENDLOOP.

Now let’s check out the updated result.

Record with EMP ID: EM1, the city has been updated to San Francisco and record with a new EMP ID: EM2 has been created successfully also.

ABAP Source Code For This Lesson

Please download ABAP CRUD source code for today’s lesson. In the source code you’ll be using table from SAP’s mockup tables called FLIGHT data model.

Share this tutorial with Fellow Geeks!

Other ABAP Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *