Session Variable in ABAP CDS view

0
1079

Dear SAPLearners, in this blog post you will learn about Session Variable in ABAP CDS view.

Session Variables in CDS

Session variables are global variables of the database with predefined value. These predefined values are set when the CDS view is used in Open SQL (or) CDS views that are used as data sources in other CDS views.

There are 4 session variables and each of these corresponds to the ABAP system field.

Session VariableABAP system field
$session.clientsy-mandt
$session.system_datesy-datum
$session.system_languagesy-langu
$session.usersy-uname
Session Variable

The variable name is case-sensitive. You can use the session variables in any of these formats $session.vname, $Session.Vname, and $SESSION.VNAME. Here “vname” is the client, system_date, system_language and user.

Session variables are an alternative way to access ABAP environment system fields in ABAP CDS view.

Basic Example #1:

In this example, $session.system_language is used to restrict the join


@AbapCatalog.sqlViewName: 'ZSESS_VAR'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Session Variable Demo'
define view Z_SESS_VAR_DEMO 
    as select from scarr
    left outer join tcurt
                on scarr.currcode = tcurt.waers
                and tcurt.spras = $session.system_language
{
    key scarr.carrid,
        scarr.carrname,
        scarr.currcode,
        scarr.url,
        tcurt.ltext
}

Basic Example #2:

In this example, $session.system_language is used in WHERE condition.


@AbapCatalog.sqlViewName: 'ZSESS_VAR'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Session Variable Demo'
define view Z_SESS_VAR_DEMO 
    as select from makt
{
    key makt.matnr,
        makt.maktx,
        makt.maktg
}
 where makt.spras = $session.system_language

The content of the variables is undefined when Open SQL is not used to access a CDS view with session variables on other databases but in SAP HANA database the content is set independently of ABAP CDS and Open SQL.

Congrats!! you have successfully learned about Session variable in ABAP CDS view.

Please feel free to comment and let us know your feedback. Subscribe for more updates

If you liked it, please share it! Thanks!

mail

SAPLearners.com is now on Telegram. Click here to join our channel and stay updated with the latest tutorials and updates.

Like us on Facebook and follow us on Twitter.