SAP ABAP Practice Exercises

Introduction

Get ready to enhance your SAP ABAP skills with our comprehensive collection of SAP ABAP practice exercises. Whether you’re a beginner or an experienced developer, our exercises will help you master the intricacies of the ABAP programming language and build robust, scalable and efficient applications. From basic exercises like creating an ABAP report and working with variables, to advanced exercises like creating an OData service and working with BAPI’s, we’ve got something for everyone. Our step-by-step instructions and detailed explanations make it easy for you to follow along and improve your understanding of SAP ABAP. With our practice exercises, you will be able to build professional, high-quality applications and advance your career as an SAP ABAP developer. Optimize your skills, improve your knowledge and get ready to take on any ABAP challenge with our SAP ABAP practice exercises.

SAP ABAP Basic Exercise

Here are ten practice exercises for SAP ABAP (ABAP stands for “Advanced Business Application Programming”):

1. Create a program that displays the current system date and time when run.

REPORT ZDISPLAY_DATETIME.

DATA: current_date TYPE sy-datum,
      current_time TYPE sy-uzeit.

current_date = sy-datum.
current_time = sy-uzeit.

WRITE: / 'Current Date:', current_date,
       / 'Current Time:', current_time.

 

2. Create a program that prompts the user for a number, then calculates and displays the square of that number.

REPORT ZSQUARE_NUMBER.

DATA: num TYPE i,
      result TYPE i.

PARAMETERS: p_num TYPE i.

result = p_num * p_num.

WRITE: / 'The square of', p_num, 'is', result.

 

3. Create a program that reads data from a CSV file and displays it in a table on the screen.

REPORT ZREAD_CSV.

DATA: it_csv_data TYPE TABLE OF string,
      lv_file_name TYPE string.

PARAMETERS: p_file TYPE string.

lv_file_name = p_file.

CALL METHOD cl_gui_frontend_services=>gui_upload
  EXPORTING
    filename = lv_file_name
  IMPORTING
    filelength = lv_file_len
  CHANGING
    data_tab = it_csv_data.

LOOP AT it_csv_data.
  WRITE: / it_csv_data.
ENDLOOP.

 

4. Create a program that prompts the user for a customer number and displays the customer’s name, address, and contact information from the SAP database.

REPORT ZCUSTOMER_DETAILS.

DATA: lv_customer TYPE KNA1-KUNNR,
      lv_name TYPE KNA1-NAME1,
      lv_address TYPE KNA1-STRAS,
      lv_contact TYPE KNA1-TELF1.

PARAMETERS: p_customer TYPE KNA1-KUNNR.

SELECT SINGLE NAME1 STRAS TELF1
  INTO (lv_name, lv_address, lv_contact)
  FROM KNA1
  WHERE KUNNR = p_customer.

WRITE: / 'Customer Name:', lv_name,
       / 'Address:', lv_address,
       / 'Contact:', lv_contact.

 

5. Create a program that generates an invoice for a customer using data from the SAP database.

REPORT ZINVOICE_GENERATION.

DATA: lv_customer TYPE KNA1-KUNNR,
      lv_invoice TYPE BSEG-BELNR,
      lv_amount TYPE BSEG-DMBTR.

PARAMETERS: p_customer TYPE KNA1-KUNNR.

SELECT SINGLE BELNR DMBTR
  INTO (lv_invoice, lv_amount)
  FROM BSEG
  WHERE KUNNR = p_customer.

WRITE: / 'Invoice:', lv_invoice,
       / 'Amount:', lv_amount.

 

6. Create a program that generates a report showing the sales data for a specific product over a specified time period.

REPORT ZSALES_DATA.

DATA: lv_product TYPE VBAP-MATNR,
      lv_sales TYPE VBAP-NETWR.

PARAMETERS: p_product TYPE VBAP-MATNR.

SELECT SUM(NETWR)
  INTO lv_sales
  FROM VBAP
  WHERE MATNR = p_product.

WRITE: / 'Total Sales for Product:', p_product, 'is', lv_sales.

 

7. Create a program that allows the user to update the prices of a specific product in the SAP database.

REPORT ZUPDATE_PRICE.

DATA: lv_product TYPE MARA-MATNR,
      lv_price TYPE MARA-PRDAT.

PARAMETERS: p_product TYPE MARA-MATNR,
            p_price TYPE MARA-PRDAT.

UPDATE MARA SET PRDAT = p_price
  WHERE MATNR = p_product.

WRITE: / 'Price updated for product:', p_product.

 

8. Create a program that generates a report showing the inventory levels of all products in the SAP database.

REPORT ZINVENTORY_LEVELS.

DATA: lv_product TYPE MARD-MATNR,
      lv_stock TYPE MARD-LABST.

SELECT MATNR, LABST
  INTO (lv_product, lv_stock)
  FROM MARD
  WHERE MATNR IS NOT NULL.

WRITE: / 'Product:', lv_product, 'Stock:', lv_stock.
ENDLOOP.

 

9. Create a program that allows users to search for and display information about a specific purchase order.

REPORT ZPO_SEARCH.

DATA: lv_po TYPE EKKO-BELNR,
      lv_vendor TYPE EKKO-LIFNR,
      lv_date TYPE EKKO-BUDAT.

PARAMETERS: p_po TYPE EKKO-BELNR.

SELECT SINGLE BELNR LIFNR BUDAT
  INTO (lv_po, lv_vendor, lv_date)
  FROM EKKO
  WHERE BELNR = p_po.

WRITE: / 'PO:', lv_po,
       / 'Vendor:', lv_vendor,
       / 'Date:', lv_date.

 

10. Create a program that generates a report showing a specific sales area’s revenue over a specified period.

REPORT ZSALES_AREA_REVENUE.

DATA: lv_sales_area TYPE VBRP-VGBEL,
      lv_revenue TYPE VBRP-NETWR.

PARAMETERS: p_sales_area TYPE VBRP-VGBEL.

SELECT SUM(NETWR)
  INTO lv_revenue
  FROM VBRP
  WHERE VGBEL = p_sales_area.

WRITE: / 'Sales Area:', p_sales_area, 'Revenue:', lv_revenue.

 

Please note that these exercises are basic examples, and it’s recommended to practice on a non-production system, as some of them may require access to specific tables or functionalities unavailable on all systems.

SAP ABAP Advanced Exercise

Here is an advanced and complex SAP ABAP exercise:

Task: Create a program that reads data from a table and creates a report that displays the data in a specific format.

Sub Tasks:

1. Create a new ABAP program using the transaction code SE38.

2. Define the data structure for the table you will be reading from. This should include the fields you will be using in the report.

3. Use the SELECT statement to read the data from the table and store it in your defined data structure.

4. Create a report using the data structure and the data you read from the table. Use the LOOP statement to iterate through the data and display it in the report.

REPORT ZREPORT_FROM_TABLE.

TYPES: BEGIN OF ty_data,
         field1 TYPE string,
         field2 TYPE string,
         field3 TYPE i,
       END OF ty_data.

DATA: it_data TYPE TABLE OF ty_data,
      lv_field1 TYPE string,
      lv_field2 TYPE string,
      lv_field3 TYPE i.

SELECT field1, field2, field3
  INTO (lv_field1, lv_field2, lv_field3)
  FROM your_table
  WHERE your_conditions.

APPEND VALUE #( field1 = lv_field1
                field2 = lv_field2
                field3 = lv_field3 ) TO it_data.

LOOP AT it_data.
  WRITE: / it_data-field1, it_data-field2, it_data-field3.
ENDLOOP.

 

5. Use the SUM and COUNT statements to calculate and display the total number of records and the total value of a specific field in the report.

DATA: lv_total TYPE i,
      lv_count TYPE i.

SELECT SUM(field3), COUNT(*)
  INTO (lv_total, lv_count)
  FROM your_table
  WHERE your_conditions.

WRITE: / 'Total:', lv_total,
       / 'Count:', lv_count.

 

6. Use the AT NEW and AT END OF statements to create subtotals for specific fields in the report.

LOOP AT it_data.
  AT NEW field1.
    WRITE: / 'New Field1:', it_data-field1.
  ENDAT.

  AT END OF field1.
    WRITE: / 'End of Field1:', it_data-field1.
  ENDAT.
ENDLOOP.

 

7. Create an interactive ALV grid report using the CLASS CL_SALV_TABLE and the method SET_TABLE_FOR_FIRST_DISPLAY.

DATA: o_alv TYPE REF TO cl_salv_table.

CALL METHOD cl_salv_table=>factory
  IMPORTING
    r_salv_table = o_alv
  CHANGING
    t_table = it_data.

o_alv->display( ).

 

8. Implement a search functionality that allows users to filter the data based on specific criteria.

PARAMETERS: p_field TYPE string. SELECT * FROM your_table WHERE field1 = p_field INTO TABLE it_filtered_data.

 

9. Use the UPDATE and MODIFY statements to allow the user to update the data in the table directly from the report.

UPDATE your_table SET field1 = 'new_value' WHERE field2 = 'some_condition'.

 

10. Use the transaction code SE38 to test the program and ensure it works correctly.

To test, use transaction code SE38 to create and run the report. Make sure your conditions and table names are valid.

Note: This exercise is complex and assumes that you have a good understanding of ABAP and can use related tools and transactions.

Author

Comments

2 responses to “SAP ABAP Practice Exercises”

  1. Uclassic Avatar
    Uclassic

    Where is the answer to the above exercise?

    1. Rudramani Pandey Avatar

      Kindly check, it is updated.

Leave a Reply

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.