Dear SAPLearners, in this blog post you will learn about Session Variable in ABAP CDS view.
Session Variable in ABAP CDS View
Session variables are global variables of the database with predefined value. The variable is case-insensitive. 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 Variable | ABAP system field |
$session.client | sy-mandt |
$session.system_date | sy-datum |
$session.system_language | sy-langu |
$session.user | sy-uname |
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.
Conclusion
Congrats!! you have successfully learned about session variable in ABAP CDS view. Please feel free to comment and let us know your feedback and check out our large collection of ABAP CDS Views. Subscribe for more updates
If you liked it, please share it! Thanks!