Alias Confusion

When I was learning ABAP 20 years ago, a typical example of reading data from the database looked like this:

SELECT a~vbeln … FROM vbak AS a JOIN vbap AS b… 

The “AS a” part is called an alias. Even more recent ABAP documentation still includes very similar examples, just with different tables like SCARR (pirates’ favorite!) and SFLIGHT. And there’s still zero explanation of the aliases.

At first, I naively assumed it was something we always had to do: just write AS, pick a random letter, and move on. Imagine my surprise when I saw code like this:

SELECT vbak~vbeln FROM vbak JOIN vbap…

Wait, what?! We can do this?! I spent the following years spreading the good word to other developers and getting rid of the confusing “AS a…” aliases.

These days, SAP doesn’t want us selecting anything from VBAK anymore (that would be dirty, dirty core), and this story might seem a bit “2000 and late.” But roughly half of SAP customers are still using ECC, and I hope this encourages everyone to explore plain SQL for better examples. So here is some ancient alias wisdom.

  • Aliases are optional. ‘AS’ is marked as such in the documentation, but nearly all the examples include it - which I suspect misleads many developers. If the table name is already short (like VBAK), not much is gained by assigning it a one-letter alias.

  • If the field name is unique, it doesn’t need the source table specified. In the examples above, the field VBELN exists in both tables, so we need to be more specific. But otherwise, it’s not required. However, I find it helpful to be clear about what’s being selected from where, especially for maintenance purposes.

  • Using an alias makes it possible to include the same table multiple times in a SELECT statement. This is rather unusual, but one potential example is joining the VBPA table multiple times to include different partner types.

While we’re on the subject, there are other kinds of aliases in ABAP. One interesting case is described in this blog post. There are also aliases in the class/interface context. Cool stuff. JP

Previous
Previous

Everything Wrong With AI In and For SAP

Next
Next

Consultant Obsolescence