Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
horst_keller
Product and Topic Expert
Product and Topic Expert

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.

Session Variables

If a SAP HANA Database serves as the central database of an AS ABAP, you have access to the following three global session variables:

  • CLIENT
  • APPLICATIONUSER
  • LOCALE_SAP

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:

METHOD get_session_variables_amdp
       BY DATABASE PROCEDURE FOR HDB
       LANGUAGE SQLSCRIPT.
  ...


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

  • $session.user
  • $session.client
  • $session.system_language

Simple example:

@AbapCatalog.sqlViewName: 'DEMO_CDS_SESSVAR'

@AccessControl.authorizationCheck: #NOT_REQUIRED

define view demo_cds_session_variables

as

select

   from demo_expressions

     { id,

       $session.user            as system_user,

       $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:

  • #CLIENT
  • #SYSTEM_DATE
  • #SYSTEM_TIME
  • #SYSTEM_LANGUAGE
  • #USER


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 View


@AbapCatalog.sqlViewName: 'DEMO_CDS_SYST'

@AccessControl.authorizationCheck: #NOT_REQUIRED

define view demo_cds_system_fields

  with parameters

    @Environment.systemField : #CLIENT

    p_mandt : syst_mandt,

    @Environment.systemField : #SYSTEM_DATE

    p_datum : syst_datum,

    @Environment.systemField : #SYSTEM_TIME

    p_uzeit : syst_uzeit,

    p_langu : syst_langu  

    @<Environment.systemField : #SYSTEM_LANGUAGE,

    p_uname : syst_uname  

    @<Environment.systemField : #USER

  as select from demo_expressions          

     { :p_mandt as client,

       :p_datum as datum,

       :p_uzeit as uzeit,

       :p_langu as langu,

       :p_uname as uname  }

      where id = '1';


Example Open SQL Access


SELECT *

   FROM demo_cds_system_fields(  )

   INTO TABLE @DATA(result).


The parameters are passed implicitly. This replaces


SELECT *

  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!





9 Comments