How to use CASE in 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!

SAP developer and integration consultant since 2016. Involved in several international projects as ABAP developer, AIF developer and integration consultant. Skilled at a variety of tools and development techniques including OOP in ABAP, Process Observer, Application Interface Framework (AIF), Process Orchestration, BRFplus, Alerts, Proxy, Smartforms, Adobe Forms, SAPscripts, BAdIs, Enhancement Framework, IDocs, ALE and SAP eDocument Framework.Up-to-date and advanced ABAP programming knowledge. Experienced with SAP AIF. Very well organized, focused on the customer and business. Creative thinker, quick to get results.

Leave a Reply

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


ul. Bednarska 1
60-571 Poznan, Poland



Write to us