ABAP News for Release 7.50 – Environment Information in ABAP CDS
In the ABAP language you are used to system fields like sy-mandt, sy-uname, sy-langu for environment information. For date and time, you use the good old german ones, sy-uzeit and sy-datum, or you use time stamps (GET TIME STAMP and co.). Some of that convenience is available in ABAP CDS now too.
If a SAP HANA Database serves as the central database of an AS ABAP, you have access to the following three global session variables:
The ABAP runtime environment fills these database variables with values that correspond to the contents of the above mentioned system fields. You can access the session variables natively, that is in EXEC SQL, ADBC and AMDP, using the built-in function SESSION_CONTEXT. Example for AMDP:
BY DATABASE PROCEDURE FOR HDB
clnt := session_context(‘CLIENT’);
unam := session_context(‘APPLICATIONUSER’);
lang := session_context(‘LOCALE_SAP’);
That’s not new for ABAP 7.50.
New for ABAP 7.50:
You can access these session variables in an ABAP CDS View. And not only for a SAP HANA Database but for all supported databases! The syntax is
Please note, that for other databases than SAP HANA the contents of these variables is only defined when you access the CDS view with Open SQL.
Implicit Passing of Parameters
While session variables are a convenient way to access the information contained within they are – well – global variables. And you know the bad reputation of global variables. What’s the alternative? Passing the information from AS ABAP to appropriate parameters of CDS Views and CDS table functions. In order to facilitate that for you, a new ABAP annotation @Environment.systemField was introduced for CDS view and function parameters with ABAP 7.50. The possible values are the enumeration values:
If you access a CDS view or CDS table function with parameters annotated as such in Open SQL, you can (and for #CLIENT you even must) leave away the explicit parameter passing. Open SQL implicitly passes the contents of the respective system fields for you!
Example Open SQL Access
FROM demo_cds_system_fields( )
INTO TABLE @DATA(result).
The parameters are passed implicitly. This replaces
FROM demo_cds_system_fields( p_datum = @sy-datum,
p_uzeit = @sy-uzeit,
p_langu = @sy-langu,
p_uname = @sy-uname )
INTO TABLE @DATA(result).
A value for p_mandt cannot be passed explicitly any more.
The implicit parameter passing is for your convenience and only available in Open SQL. If you use CDS entities with parameters within CDS entities you have to pass parameters explicitly again. You might pass the above mentioned session variables then.
Date and Time
Did you notice that you can implicitly pass values for system date and time from AS ABAP to CDS entities but that there are no session variables for those (at least not in release 7.50)?
Instead, with ABAP 7.50 a new set of built-in date and time functions is available in ABAP CDS.
The following example shows how the current date and time can be extracted from a time stamp using string functions:
substring( cast( tstmp_current_utctimestamp() as abap.char(17) ), 1, 8 )
substring( cast( tstmp_current_utctimestamp() as abap.char(17) ), 9, 6 )
Smells like a workaraound? More functionality for date and time handling in ABAP CDS is still in development!