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
define view demo_cds_session_variables
$session.client as system_client,
$session.system_language as system_language }
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!
define view demo_cds_system_fields
@Environment.systemField : #CLIENT
@Environment.systemField : #SYSTEM_DATE
@Environment.systemField : #SYSTEM_TIME
@<Environment.systemField : #SYSTEM_LANGUAGE,
@<Environment.systemField : #USER
as select from demo_expressions
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.
An important one is TSTMP_CURRENT_UTCTIMESTAMP(), that returns the current time stamp. Others check values and calculate with dates and times, as e.g. dats_days_between.
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!
Cool new features.
This workaround substring( cast( tstmp_current_utctimestamp() as abap.char(17) ), 1, 8 ) is pretty bad in my opinion since the execution on HANA column store is shifted to row store operation and particularly if this is kind of date filter is needed in JOIN conditions I think this may big overhead for performance.
When is a filter on HANA column store for SY-DATUM would be available in 7.50?
Sorry, not in 7.50. Maybe 7.51 ...
thanks for the very valuable Information’s.
Is there a CDS function that allows me only to read the newest X entries? My table has a date field, but since the last X orders could be created within the last 2 days, 2 months or 2 years, the date field doesn’t help much.
No. All that date/time stuff is still under development and we'll have to see how far it will get.
Hi Florian Pfeffer,
The example for implicit parameter passing isn't wrong.
Why do you think it is wrong in the system?
thanks for the update on the features of 750. How would you suggest to address a system field like sy-langu in CDS on a 740 release? Especially on a database which may not support views with parameters.
I'd simply say not possible. ABAP system fields live on the application server. They are mirrored on HANA in global session variables and can be accessed natively there. But not in CDS before release 7.50.
Can i use the parameter defined in where clause of the CDS view?