ABAP CDS - session_variable
ABAP CDS - session_variable
Syntax
... $session.vname ...
Effect
Specifies a session variable vname in an operand position in a SELECT statement of a CDS view in ABAP CDS. The variable is case-sensitive. $session.vname, $Session.Vname, and $SESSION.VNAME can all be used. No other spellings are allowed.
Session variables have a predefined name and can be set to a predefined value when the CDS view is used in Open SQL. This also applies to CDS views that are used as data sources in other CDS views. The following session variables exist:
vname | Value in Open SQL accesses |
user | Current user name, nominal value of the ABAP system field sy-uname. |
client | Current client, nominal value of the ABAP system field sy-mandt. |
system_language | Language of the current text environment, nominal value of the ABAP system field sy-langu |
Notes
- From a technical perspective, session variables indicate global variables of the current database that are set to their value when the CDS view is used in Open SQL. If Open SQL is not used to access a CDS view with session variables, the content of the variables is undefined (with the exception of the SAP HANA database.
- On a SAP HANA database used as a central AS ABAP database, the ABAP-specific session variables are called APPLICATIONUSER, CLIENT, and LOCALE_SAP and exist and are set independently of ABAP CDS and Open SQL. They can be accessed using the database function SESSION_CONTEXT.
- For input parameters of CDS views annotated with the special annotation environment.systemField, Open SQL can pass the values of the session variables specified here automatically too. It is generally preferable to use this type of local input parameter.
- In existing CDS views without corresponding input parameters, session variables can be passed to the input parameters of CDS views or CDS table functions used there.
- If the Open SQL statement SELECT is used to access a CDS view that uses session variables, the additions USING CLIENT and CLIENT SPECIFIED cannot be used.
Example
The following CDS view contains the possible session variables in the SELECT list. The program DEMO_CDS_SESSION_VARIABLES uses Open SQL to access the view and fills the session variables with the associated values.
@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
}
Example
The following CDS view DEMO_CDS_SELECT_T100 accesses a further CDS view DEMO_CDS_SELECT_T100_LANGU with an input parameter for the language to which the annotation environment.systemField with the value system_language is assigned. Unlike in Open SQL, there is no implicit pass of the actual parameter to the input parameter here and the corresponding session variable $session.system_language is passed instead. The program DEMO_CDS_T100 uses Open SQL to access both views and the result is identical. When DEMO_CDS_SELECT_T100_LANGU is accessed, the value of the system field sy-langu is passed implicitly to the input parameter. When DEMO_CDS_SELECT_T100 is accessed, the session variable $session.system_language is filled with this value.
@AbapCatalog.sqlViewName: 'DEMO_CDS_T100'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_select_t100
as select from
demo_cds_select_t100_langu( p_langu:$session.system_language )
{ * }
@AbapCatalog.sqlViewName: 'DEMO_CDS_T100_LG'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_select_t100_langu
with parameters @Environment.systemField:#SYSTEM_LANGUAGE
p_langu : lang
as select from t100 { * }
where sprsl = :p_langu and
arbgb = 'SABAPDEMOS'
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架