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 vbeln, matkl,
WHEN ‘L001’ THEN matnr
WHEN ‘L004’ THEN arktx
ELSE ‘ ‘
END AS material_id
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 Goods – Description 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!