Technically possible but not good style in ABAP

Reading time:

3–5 minutes

There are different solutions for many ABAP tasks. Even if the result of each solution appears to be the same at first glance, making all solutions seem equivalent, they are ultimately not.

At least if you analyze the chosen solution with regard to CleanABAP and its guiding principles of readability, comprehensibility, and maintainability. Then a technically feasible solution according to CleanABAP can turn into one that should be ruled out or even not recommended.

The example

The following code, which I recently saw in a new development, falls into exactly this category.

SELECT SINGLE mandt INTO sy-mandt FROM t001 WHERE bukrs = lv_bukrs.

When reading this single line, reason and gut feeling can quickly send contradictory signals.

The mind wonders why the client of an entry in a client-specific database table is being read into a structure that is generally available, at least in on-premises systems, and which automatically provides this information anyway.

The gut feeling approaches the matter based on intuition. The assumption quickly arises that the fundamental idea behind this instruction is to check the existence of a specific record.

Unfortunately, intuition is a tricky thing. Above all, intuition often only develops after years of intensive study of a topic. Moreover, one still has to substantiate one’s suspicion.

Be that as it may, according to CleanABAP, this exact situation should be avoided when reading source code. It is a form of cognitive complexity (reading but not understanding) that is unnecessary, ties up every reader’s mind without providing any added value, and is also exhausting.

Your friend, static code analysis

Actually, you shouldn’t even read a line like that in 2025. At least, if you’re a developer running the ABAP Test Cockpit (ATC) and heeding its advice. The ATC warns that the SELECT statement shouldn’t write its result to SY-MANDT. If you take the ATC warning seriously, you’ll look for another solution.

Your other friend who never sleeps and always has time for you

For some time now, we’ve all had at least one AI colleague we can ask when needed. In my case, ChatGPT was sitting next to me virtually. I took advantage of the opportunity, and the conversation went something like this.

Me:

[..] Can I also misuse the SY-MANDT field to check the existence with a SELECT SINGLE mandt FROM t001?

ChatGPT:

Good question — technically yes, but it is not recommended to misuse the SY-MANDT field or the MANDT database field in this way, just to check the existence of a data record.
 
[..]

Why it is not recommended:
 
1. Semantic Misuse: The MANDT field is a technical control field. Using it for an existence check is a semantic “misuse” and can:

a) Reduce maintainability (other developers ask themselves: Why is MANDT being read?)

b) Make code comprehension more difficult

2. Risk of confusion: It may appear to other developers as if you actually need the tenant value, which leads to misunderstandings.

3. Future-proof: Even if ABAP can currently handle this, if the rules for multi-tenant processing change (e.g., with S/4HANA multi-tenant capabilities), this could have unintended side effects.

A surprisingly good answer from ChatGPT. Unfortunately, several more suggestions for alternatives to the line of code shown above followed, all of which were syntactically incorrect. Some of the suggested alternatives don’t even exist in ABAP syntax. Creative but useless. It quickly became clear that you were talking to an AI in 2025. At least if you have some experience with ABAP.

How to do it better

I demonstrated how to check the existence of a data record in a more understandable way in an SAP Community blog titled “the ‘X’ in the select statement” in 2019. The code above could therefore look something like this.

CONSTANTS entry_exists TYPE abap_bool VALUE abap_true.

SELECT SINGLE @entry_exists 
       FROM t001 AS company_codes
       WHERE bukrs = @company_code
       INTO @DATA(answer_from_db).

I would relocate this code to a functional method with a suitable name, such as “DOES_COMPANY_CODE_EXIST,” and a “RETURNING VALUE” of type “abap_bool.” This allows the code to be used in expressions.

Furthermore, a suitable Core Data Service (CDS) can certainly be found to make the database table name “T001” more understandable. This all depends, of course, on the capabilities of the respective system release.

Summary

From a technical perspective, there are multiple solutions to many problems. Even if they all lead to the same result, some are more readable and understandable upon closer inspection and are therefore preferable (see CleanABAP).

This pays off. Whether you or other developers are reading the code, you don’t have to resolve a conflict between reason and intuition, nor do you need years of experience to understand what the code is supposed to do.

In short: goal achieved, cognitive load kept to a minimum, and possibly a few minutes of work time saved for further training or a coffee (tea).


Thank you for reading. If you enjoyed the post, please share the article with your community. Thanks in advance.

Michael