Simplify Your Queries with SELECT and FIELDS: Using CASE in Modern ABAP
When working with SAP tables, it’s common to combine data from multiple tables and derive new fields dynamically based on conditions. One of the best features in modern ABAP SQL is the ability to use FIELDS and CASE in a SELECT statement. It allows you to calculate fields directly in the database layer, making your queries cleaner and more efficient.
In this article, let’s break down a practical use case involving the QMEL (Notification Table) and JEST (Status Table) tables, showcasing how to use CASE to assign meaningful user statuses to notification data.
Scenario
You need to fetch a list of quality notifications (QMEL) and their current user statuses (JEST). Each status (STAT) in JEST has a specific meaning:
Additionally:
Query Breakdown
Here’s the query using FIELDS and CASE:
SELECT
FROM qmel
INNER JOIN jest AS jest
ON jest~objnr = qmel~objnr
AND jest~inact <> 'X'
AND jest~stat LIKE 'E%'
FIELDS
qmel~qmnum AS notification_number,
CASE
WHEN jest~stat = 'E0001' THEN 'CNCL'
WHEN jest~stat = 'E0003' THEN 'ALYS'
WHEN jest~stat = 'E0004' THEN 'CLSD'
WHEN jest~stat = 'E0005' THEN 'WAIT'
ELSE 'CREA'
END AS userstatus
WHERE qmel~qmart = 'LU'
AND jest~stat IN @userstatus[]
INTO TABLE @DATA(result).
How It Works
Recommended by LinkedIn
Advantages of This Approach
Example Output
Let’s assume the following data:
After running the query, the result will look like this:
Inactive records (like 1000003) are excluded, and statuses are mapped according to the CASE logic.
When to Use This Approach
Limitations