ABAP - The new Select
The customization of the ABAP language also has implications and improvements for the well-known and important Select. In this article we want to briefly describe what has changed and what you get for benefits.
Table of contents
As already described in an earlier article, the Select also changed a bit. There are now two variants in the execution and notation. The previous variant persists and does not change, thus keeping all old programs up to date.
Changes
So what has changed with the modern ABAP? Basically, this can be described with a few bullet points:
- Functions can now be transferred to the select or the results of these functions when called. Many functions were also completely new, only for the Select, implemented.
- Dynamic return tables / variables can be generated at runtime.
- The order of construction has changed a bit.
For you that means, there are some new features that you should definitely try with the Select.
Examples
Here are two examples to bring the new features a little bit closer to you. In the first example we focus on the creation of internal tables with dummy fields and how you can create them dynamically at runtime.
" Select-option creation
SELECT
'I' AS sign,
'EQ' AS option,
bukrs AS low,
' ' AS high
FROM t001
WHERE land1 <> 'DE'
INTO TABLE @DATA(lt_range).
As you have already guessed from the fields, we get as result a select option that is filled with the company codes, which are not in Germany.
- With the AS addition we give the fields new names, which was already possible with the "old" Select. The individual fields are now separated by commas.
- We give the fields a default value and assign it to the field. An empty value must contain a space in the literal, and an empty literal will be displayed as an error by the compiler.
- With the Escape character and DATA command we create a table at runtime. This table has exactly the fields that we specify in the Select.
- The INTO command has now moved to the end of the statement, which is as far as provided by SAP as a new point in the statement.
As a second example, we would like to draw your attention to the many new inline functions that were implemented especially for the Select.
" Create new variable
DATA(ld_waers) = 'eur'.
" Select with functions
SELECT
bukrs AS bukrs,
CAST( kokfi AS CHAR( 20 ) ) AS text,
CASE WHEN stceg = ' ' THEN '-'
ELSE stceg
END AS tax
FROM t001
WHERE bukrs LIKE 'A%'
AND waers = @( to_upper( ld_waers ) )
INTO TABLE @DATA(lt_bukrs).
In this example, the functions that pass the values to the select or make specific queries to the data of the select are of particular interest.
- The cast function converts the number field into a text field with 20 characters, so that the data type can be returned to the target format directly upon return.
- With a case statement, the tax key is checked, when it's not empty, it is taken over, but should it be empty, then we set a simple minus.
- In the where-clause, we still convert the currency field to uppercase with the UPPER command before passing it to Select. As always, an escape character is used to call the function.
To the point
The best feature is the dynamic generation of the target structure in Select. Thus only the determined fields are in the table, the table remains small in the memory and no special type needs to be defined beforehand and not even the target variable.
" Simple Select
SELECT bukrs, butxt, stceg, waers
FROM t001
WHERE land1 <> 'DE'
INTO TABLE @DATA(lt_bukrs).
It is always important to use the comma between the enumeration of the fields and the escape of the DATA statement to create the table.
Conclusion
Much has changed at the Select statement, also because the CDS Views are now in competition with these and are also very powerful. We hope we could make you curious? Then take a look at the new Open SQL functions in the SAP documentation.
Source:
SAP documentation - Open SQL