Create, Read, Update & Delete – CRUD Operations in ABAP Report

by | Jun 4, 2018 | ABAP Beginner, ABAP Programs

Preface – This post is part of the ABAP Programs series.

In ABAP, if you know basic SQL operation to perform CRUD Operations in ABAP report, then this article will teach you multiple ways to perform the same.

Introduction

ABAP report has more often a requirement to perform any of the following operation:

  • Create : It means, you add data into a table
  • Read : It means, you read data from a table
  • Update : It means, you update any data of a table
  • Delete : It means, you delete a record from a table

Everything in programming are directly or indirectly performing CRUD operations only. Let’s take example of Facebook.

Whenever anyone create a profile on Facebook, the data of new Facebook ID is added into a table.

Create Operation - CRUD operations Image illustration

Create Operation in Facebook

If you want to see your profile, data is read from the same table.

Read Operation in Facebook - CRUD operations Image illustration

Read Operation in Facebook

If you want to update your profile, data is updated in the same table.

Update Operation in Facebook

Update Operation in Facebook

And if, you plan to delete your account permanently, data is deleted from the same table.

Delete Operation in Facebook

Delete Operation in Facebook

You see, here we have performed CRUD operations only.

ABAP Program to perform CRUD Operations

Program Requirement: Create an ABAP report to provide four radio buttons with name Create, Update, Read and Delete. Employee ID input box will be always visible. If Create button is clicked, Input boxes will be available for Employee Details Input. On Save, it will save data in table. If Update button is clicked, Input boxes will be visible with pre-loaded data for the Employee ID. On Update, it will update data in the table. If Read is clicked, the data for the Employee ID will be visible. If delete radio button is clicked, on click of Delete button, data will be deleted.

Some Tables you will need:

Employee Basic Table

Employee Basic Table

 

Employee Salary Table

Employee Salary Table

 

Employee Address Table

Employee Address Table

 

"Program for Reading , Updating , Inserting & Deleting data from a table


TABLES: zBarry_emp. "Employee Table
DATA: lt_itab   TYPE TABLE OF zBarry_emp,  "Internal table for employee basic details table
      lwa_emp   TYPE zBarry_emp, "Workarea for employee basic details table
      lt_itab1  TYPE TABLE OF zBarry_add, "Internal table for employee address details table
      lwa_emp1  TYPE zBarry_add, "Workarea for employee address details table
      lt_itab2  TYPE TABLE OF zBarry_sal,  "Internal table for employee salary details table
      lwa_emp2  TYPE zBarry_sal, "Workarea for employee salary details table
      total_sal TYPE zBarry_salary, "total salary
      tax       TYPE zBarry_salary, "total tax
      c         TYPE zBarry_salary, "tax percentage
      inhand    TYPE zBarry_salary. "inhand salary

PARAMETER: p_empid TYPE char8.

SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-101. "selection screen block

PARAMETERS:p_rad1 RADIOBUTTON GROUP rg1 USER-COMMAND flag DEFAULT 'X',
           p_rad2 RADIOBUTTON GROUP rg1,
           p_rad3 RADIOBUTTON GROUP rg1,
           p_rad4 RADIOBUTTON GROUP rg1.

SELECTION-SCREEN END OF BLOCK blk1.




PARAMETERS:
  p_fname TYPE char20 MODIF ID pa1, "parameter for first name
  p_lname TYPE char20 MODIF ID pa2, "parameter for last name
  p_city  TYPE char20 MODIF ID pa3. "parameter for city name






AT SELECTION-SCREEN OUTPUT.

  LOOP AT SCREEN.
    CASE screen-group1. "selecion of parameters on the basis of radiobutton selection

      WHEN 'PA1'.
        IF p_rad1 = 'X'.
          screen-active = '0'.
        ENDIF.

        IF p_rad2 = 'X'.
          screen-active = '1'.
        ENDIF.

        IF p_rad3 = 'X'.
          screen-active = '1'.
        ENDIF.
*
        IF p_rad4 = 'X'.
          screen-active = '0'.
        ENDIF.

        MODIFY SCREEN.

      WHEN 'PA2'.
        IF p_rad1 = 'X'.
          screen-active = '0'.
        ENDIF.

        IF p_rad2 = 'X'.
          screen-active = '1'.
        ENDIF.
        IF p_rad3 = 'X'.
          screen-active = '1'.
        ENDIF.
        IF p_rad4 = 'X'.
          screen-active = '0'.
        ENDIF.
        MODIFY SCREEN.

      WHEN 'PA3'.
        IF p_rad1 = 'X'.
          screen-active = '0'.
        ENDIF.

        IF p_rad2 = 'X'.
          screen-active = '1'.
        ENDIF.
        IF p_rad3 = 'X'.
          screen-active = '1'.
        ENDIF.
        IF p_rad4 = 'X'.
          screen-active = '0'.
        ENDIF.
        MODIFY SCREEN.
    ENDCASE.
  ENDLOOP.


AT SELECTION-SCREEN ON RADIOBUTTON GROUP rg1. "Auto-Popullating data in parameters

  IF p_rad3 EQ 'X'. "if update radio button is selected

    SELECT emp_fname emp_lname
         FROM zBarry_emp
         INTO CORRESPONDING FIELDS OF TABLE lt_itab
       WHERE empid = p_empid.
    IF sy-subrc = 0.

      LOOP AT lt_itab INTO lwa_emp.

        p_fname = lwa_emp-emp_fname.
        p_lname = lwa_emp-emp_lname.

      ENDLOOP.

    ENDIF.
  ENDIF.

START-OF-SELECTION.

  IF p_rad3 EQ 'X'. "Updating data

    SELECT *
         FROM zBarry_emp
         INTO TABLE lt_itab
         WHERE empid = p_empid.

    UPDATE zBarry_emp SET emp_fname = p_fname
        emp_lname = p_lname WHERE empid = p_empid.
    UPDATE zBarry_add SET city_name = p_city WHERE empid = p_empid.

    IF sy-subrc = 0.
      MESSAGE 'Employee Record Updated' TYPE 'I'.
    ELSEIF sy-subrc <> 0 .
      MESSAGE 'Wrong Employee ID3' TYPE 'E'.
      EXIT.
    ENDIF.

    EXIT.

  ENDIF.

  IF p_rad1 EQ 'X'. "Displaying Data
    SELECT *
      FROM zBarry_emp
      INTO TABLE lt_itab
      WHERE empid = p_empid.
    IF sy-subrc = 0 .

      LOOP AT lt_itab INTO lwa_emp.

        FORMAT COLOR 1 INTENSIFIED ON. "adding dark color to headings
        WRITE: /3 'Employee Id',
                20 'First Name',
                35 'Last Name',
                70  'Date Of Birth',
                90  'Gender',
                110  'Marital Status',
                130  'Created By',
                150  'Creation Date'.
        ULINE. " FOR UNDERLINE

        FORMAT COLOR 2 INTENSIFIED ON. "adding dark color to fields

        WRITE:/3 lwa_emp-empid, 20 lwa_emp-emp_fname,35 lwa_emp-emp_lname,70 lwa_emp-dob,90 lwa_emp-gender,110 lwa_emp-marital,
        130 lwa_emp-created_by,150 lwa_emp-created_date.
      ENDLOOP.
      ULINE.

      SELECT *
     FROM zBarry_add
     INTO TABLE lt_itab1
     WHERE empid = p_empid.

      LOOP AT lt_itab1 INTO lwa_emp1.

        FORMAT COLOR 1 INTENSIFIED ON. "adding dark color to headings
        WRITE: /3 'Employee Id',
                20 'Flat No.',
                35 'Street Name',
                70  'City Name',
                90  'State',
                110  'Pincode'.
        ULINE. " FOR UNDERLINE

        FORMAT COLOR 2 INTENSIFIED ON. "adding dark color to fields

        WRITE:/3 lwa_emp1-empid, 20 lwa_emp1-flat_no, 35 lwa_emp1-street_name,70 lwa_emp1-city_name,90 lwa_emp1-state,110 lwa_emp1-pincode.
      ENDLOOP.
      ULINE.

      SELECT *
      FROM zBarry_sal
      INTO TABLE lt_itab2
      WHERE empid = p_empid.

      LOOP AT lt_itab2 INTO lwa_emp2.

        FORMAT COLOR 1 INTENSIFIED ON. "adding dark color to headings
        WRITE: /3 'Employee Id',
                20 'Transaction ID',
                35 'Month',
                70  'Date Of Salary',
                90  'Basic Salary',
                110  'Food Card',
                130  'Transport',
                150  'Variable_M',
                170  'Variable_Q',
                190  'Medical',
                210  'Others'.
        ULINE. " FOR UNDERLINE

        FORMAT COLOR 2 INTENSIFIED ON. "adding dark color to fields


        WRITE:/3 lwa_emp2-empid,20 lwa_emp2-tid,35 lwa_emp2-mon,70 lwa_emp2-dos,90 lwa_emp2-bs,110 lwa_emp2-fc,130 lwa_emp2-transport,
        150 lwa_emp2-variable_m,170 lwa_emp2-variable_q,190 lwa_emp2-medical,210 lwa_emp2-others.
      ENDLOOP.
      ULINE.
      total_sal = lwa_emp2-bs + lwa_emp2-fc + lwa_emp2-transport + lwa_emp2-variable_m +
      lwa_emp2-variable_q + lwa_emp2-medical + lwa_emp2-others. "total salary calculated

      WRITE:/ 'Total Salary = ', total_sal.

      IF total_sal < 10000.
        WRITE: 'Voila! No Tax'.
      ELSEIF ( 10000 < total_sal AND total_sal <  35000 ).
        c = 5 / 100.
        tax = c * total_sal. "total tax calculated when 10000< total salary <35000
        WRITE : / 'Tax = ', tax.
      ELSEIF ( 30000 < total_sal AND total_sal <  50000 ).
        c = 10 / 100.
        tax = c * total_sal. "total tax calculated when 30000 < total salary <50000
        WRITE : / 'Tax = ', tax.
      ELSEIF total_sal > 50000.
        c = 15 / 100.
        tax = c * total_sal. "total tax calculated when total salary >50000
        WRITE : / 'Tax = ', tax.
      ENDIF.
      inhand = total_sal - tax. "inhand salary
      WRITE : / 'Inhand Salary' , inhand.

    ELSEIF sy-subrc <> 0 .
      MESSAGE 'Wrong Employee ID1' TYPE 'E'.
      EXIT.
    ENDIF.
  ENDIF.



  IF p_rad2 EQ 'X'. "Inserting Data

    lwa_emp-empid = p_empid.
    lwa_emp-emp_fname = p_fname.
    lwa_emp-emp_lname = p_lname.
    lwa_emp1-empid = p_empid.
    lwa_emp1-city_name = p_city.

    INSERT zBarry_emp FROM lwa_emp.
    INSERT zBarry_add FROM lwa_emp1.

    IF sy-subrc = 0.
      MESSAGE 'Employee Record Inserted' TYPE 'I'.
    ELSEIF sy-subrc <> 0 .
      MESSAGE 'Wrong Employee ID2' TYPE 'E'.
      EXIT.
    ENDIF.

    EXIT.

  ENDIF.



  IF p_rad4 EQ 'X'. "Deleting Data

    DELETE FROM zBarry_emp WHERE empid = p_empid.
    IF sy-subrc = 0.
      MESSAGE 'Employee Record Deleted' TYPE 'I'.
    ELSEIF sy-subrc <> 0 .
      MESSAGE 'Wrong Employee ID4' TYPE 'E'.
      EXIT.
    ENDIF.
  ENDIF.

Code Explanation

In the program mentioned above, we have discussed CRUD operation in ABAP Report. This program has comments wherever required. Still, we will be explaining key points of this program. This program utilizes ABAP Classical Events, do read it here, before proceeding.

  • Initially, we have defined Parameters to take input, variables to perform calculation and radio buttons to provide choice selection.
  •  AT SELECTION-SCREEN OUTPUT: Here we have written conditions such that few input boxes become hidden and other visible according to the condition.
  • AT SELECTION-SCREEN ON RADIOBUTTON GROUP rg1 to auto-populate data in parameters.
  • START-OF-SELECTION: Here we have written SQL codes to perform Create, Update, Read and Delete according to requirement and condition.

Output Screens

Dynamic Report Output

Dynamic Report Output

Dynamic Report Delete Option

Dynamic Report Delete Option

Dynamic Report Update Option

Dynamic Report Update Option

Dynamic Report Create Option

Dynamic Report Create Option

Dynamic Report Read Option

Dynamic Report Read Option

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.