Skip to Content

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.
  clnt := session_context(‘CLIENT’);
  unam := session_context(‘APPLICATIONUSER’);
  lang := session_context(‘LOCALE_SAP’);
ENDMETHOD.

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!





To report this post you need to login first.

8 Comments

You must be Logged on to comment or reply to a post.

  1. Sagar Joshi

    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?

    (0) 
  2. Fouad Sebbane

    Hi Horst,

    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.

    Thanks,

    Fouad

    (0) 
    1. Horst Keller Post author

      Is there a CDS function that allows me only to read the newest X entries?

      No. All  that date/time stuff is still under development and we’ll have to see how far it will get.

      Horst

      (0) 
  3. Horst Keller Post author

    Hi Florian Pfeffer,

    The example for implicit parameter passing isn’t wrong.

    • Yes it is copied from  the ABAP Keyword Documentation and there it can’t be wrong, since it is not part of the docu text but the embedded source code of an existing and syntax checked CDS view from the example library, hehehe 😉 .
    • The link you give above explains the syntax. An annotation introduced with @< instead of @ can be placed behind an element of a comma separated list (that is an element of the select list or a parameter). This was introduced with 740, SP08.

    Why do you think it is wrong in the system?


    Best

    Horst

    (0) 
  4. Matthias Heitmann

    Hi Horst,

    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.

    Thanks,
    Mathias

    (0) 
    1. Horst Keller Post author

       

      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.

      (1) 

Leave a Reply