styleguides
styleguides copied to clipboard
Using `FIELDS` in SQL-Statements
I did not find anything what clean ABAP recommends regarding using FIELDS
in SQL-Statements.
SELECT FROM scarr
FIELDS carrid, carrname
ORDER BY carrid
INTO TABLE @DATA(result).
" anti-pattern
SELECT carrid, carrname
FROM scarr
ORDER BY carrid
INTO TABLE @DATA(result).
-> the select with FIELDS
is also better when using ABAP Development Tools for Eclipse.
With the FIELDS
the ABAP Element Info for carrid
and carrname
is working. With the 'standard' SELECT the element info is not displayed.
What are your thought on this?
For reference: https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapfields_clause.htm
Even more useful than the element info is the auto completion for the field names (both in ADT and SE80). As far as I know auto complete for the field selection only works with this syntax where the fields are listed after the data sources.
Personally, I do not see a great benefit to this recommendation. It is a neat pointer to a language feature that not many may be aware of but I would never go as far as to calling a SELECT
without FIELDS
an anti-pattern from a clean code perspective.
Admittedly, it is a nice trick to work around development environment shortcomings. As code is read more often than it is written one could also argue for the reverse to be preferable: Do not use the FIELDS
keyword as it breaks the natural flow of the English language.
Points can be made for both variants and even the argument above may be rebutted by examples with complex FROM
clauses and aliasing where one would really benefit from knowing about the selection source first.
Therefore my vote is to not take sides in the styleguide at all.
I see no value whatsoever in FIELDS. It's stating the obvious. I mean what else could those be, bananas? Auto-completion should react to SELECT, not FIELDS IMHO.
In general seems like bad trend of SAP moving farther and farther away from generic SQL syntax. This does not improve adoption and learning speed. I had no problem using SQL in ABAP 10+ years ago because it was basically the same commands I already knew from SQL Server. But it's a different picture these days...
I vote with @N2oB6n-SAP to take this out. I would not recommend using FIELDS but would not recommend against it either if someone really likes it.
Personally I've been using the FIELDs command to benefit from the auto-completion, but I do have to agree with the previous two comments.
If possible, I'd like to know from the ADT development team if they can make the auto-completion available for regular SELECTs without the FIELDS keyword - that'd be the best of both worlds.
I have opened a SAP Improvement request for this topic: Enable auto completion and ABAP Element Info for ‘standard’ SELECTs without 'FIELDS'