Blog > How to use CASE in new Open SQL

How to use CASE in new Open SQL

Mateusz Nowak SAP Integration Consultant, SAP Press Author
icon__calendar 2019-07-17

From this blog post you will learn:

  • How to use a CASE expression in the new Open SQL

In this short blog, I would like to show you how to use a CASE expression in the new Open SQL.

I have learned about it from my colleague, Michal Jakubowicz, who recently conducted a full 4-days workshop about ABAP Programming for SAP Hana.

The CASE expression in new Open SQL is available on ABAP 7.4 SP08. In this blog, we will focus on the scenario, in which we will select different columns in one select statement, based on the content in another column.

New Case expression in SELECT statement – example

Let’s consider, that you have to fill the internal table that contains three fields: VBELN, MATKL, and MATERIAL_ID. You have to fill VBELN and MATKL directly from VBAP table with Sales Document Number and Material Group. The field MATERIAL_ID you have to field based on the value in the Material Group:

If the Material Group is equal to Trading Materials (L001), then fill MATERIAL_ID with Material Number from VBELN (MATNR field)

If the Material Group is equal to Finished Goods (L004), then fill MATERIAL_ID with Description from VBELN (ARKTX field)

If Material Group is other than Trading Materials and Finished Goods – leave the MATERIAL_ID field empty.

In order to do it directly in the SELECT statement – you can use the following code snippet:

SELECT vbelnmatkl,
      WHEN ‘L001’ THEN matnr
      WHEN ‘L004’ THEN arktx
      ELSE ‘ ‘
    END AS material_id
   FROM vbap
   INTO TABLE @gt_outtab
   WHERE vbeln IN @so_vbeln[].

The entries in VBAP table look as follows:

And the result of the SELECT with CASE expressions is the following:

As you can see, for Sales Document 3 and 5, that are Finished GoodsDescription field instead of Material Number field was chosen. Thanks to CASE expression you don’t have to use two, separate SELECT statements or use additional LOOP statements to accomplish such task.

Please let us know if you like such content!

Mateusz Nowak SAP Integration Consultant, SAP Press Author
SAP Integration consultant since 2016. Mateusz is the author of SAP Press Book: Interface Monitoring and Error Handling with SAP AIF. He's also a trainer in the openSAP course Virtualize and Automate Your SAP Testing Using Int4 IFTT. Matt has been involved in multiple integrational projects as ABAP Developer, AIF lead and consultant. Skilled at a variety of tools and development techniques including ABAP, AIF, PO, BRFplus, IDocs, Adobe Forms, Smartforms, IDocs, ALE, SCPI.