Thou shalt not SELECT *

images/thumbnail.jpg - Thumbnail

Always try to select only the fields you’ll need. Selecting others is a waste of resources. Exception made for the use of FM *_SINGLE_READ because, even though these do select all fields, since they cache the data, they are still faster when used multiple times for the same key. If you just want to check if a record exists, select just one field, if possible the one you’re using as criteria to avoid declaring an extra variable. Example: SELECT KUNNR INTO V_KUNNR FROM KNA1 WHERE KUNNR = V_KUNNR. https://abapinho.com/en/2010/11/select-todos-os-campos/ (portuguese)

SELECT within SELECT

images/thumbnail.jpg - Thumbnail

ABAP programmers don’t explore the possibilities of SQL, probably for historical reasons. There are many who instead of using INNER JOINs still think it’s faster to do several SELECTs for internal tables and then process the data in ABAP. But the truth is that even if there are exceptions, the rule is: the lower the number of accesses to the database, the better the performance. And it makes sense because, after all, they were written explicitly for this; relational databases are much more adept at processing relational data than an ABAP program.

There are of course things that, due to their complexity, cannot be done with a simple INNER JOIN. Nevertheless, some of these things can be done in a single SELECT.

SELECT... ...ORDER BY PRIMARY KEY

images/thumbnail.jpg - Thumbnail

Although I have been using ABAP since 1998, I learn new features or options every week, and sometimes even new commands. It’s hard to believe.

Today, I’ll share a little SQL option that I recently discovered.

SELECT * FROM T001
INTO TABLE T_T001
ORDER BY PRIMARY KEY.

I have known SELECT, FROM, INTO, TABLE and even ORDER BY for a long time. I just didn’t know of PRIMARY KEY.

It orders the internal table by its key. Convenient.

SELECT comparing 2 fields of the same table

images/thumbnail.jpg - Thumbnail

This tip is simple and quick, but I bet 200.482 Portuguese escudos that few know it.

More RANGEs, less SELECTs

images/thumbnail.jpg - Thumbnail

The dictionary has the following entry for “style”: “set of formal aspects and expressive resources that characterise a text.”

It is preferable to read a text with style than one without. If ABAP were a language, a program in ABAP would be a text. When it comes to style, there are programs that seem to have been written by the feet, while there are others that one would swear the quill that wrote them was guided by the pure hand of an eighteenth century lady plagued by the troubles of love. Fortunately, I think it’s not possible to write programs in ABAP as tacky as the last sentence.

The “style” label is used here in Abapinho to identify articles that talk about just that: style. These articles try to find ways to improve the style of ABAP programs. This is one of Abapinho’s favourite labels, as you can see here. And what you are reading is another of those articles.

Moving on.