SAP CDS DEFINE SCALAR FUNCTION



Get Example source ABAP code based on a different SAP table
  


VERSION 5 IN
• DEFINE SCALAR FUNCTION ABAP_CDS_DDL_STATEMENT
• WITH PARAMETERS ABAP_CDS_SCALAR
• RETURNS ABAP_CDS_SCALAR

ABAP_CDS_FDL - DEFINE SCALAR FUNCTION

ABAP_SYNTAX

DEFINE SCALAR FUNCTION ScalarFunction
$[WITH PARAMETERS pname1 : typing $[,
pname2 : typing$]$[,
...$]$]
RETURNS typing$[;$]

What does it do?
Defines a CDS scalar function definition ScalarFunction as a CDS scalar function in the CDS FDL. The name ScalarFunction must follow the naming conventions for dictionary types. The following additional rules apply:
The name can be up to 26 characters long. Four characters are reserved for the suffix of the scalar function implementation reference.
The name must not start with the prefix QCM.
The first, second, third, and last character of the name must not be an underscore (_).
A CDS scalar function is located in the namespace of all global types of an AS ABAP.
Optionally, a list of input parameters pname1 , pname2, ... can be declared using the addition WITH PARAMETERS. These also define the input parameters of the AMDP scalar function. There is no limit to the number of input parameters. The typing of these input parameters is explained in the topic TITLE .
The name of an input parameter must comply with the naming rules for names. Furthermore, it cannot contain any slash (/) characters and cannot be a reserved name in ABAP Dictionary. All reserved names that cannot be used are specified in the DDIC database table TRESE . The evaluation of names is not case-sensitive. Each parameter name must be unique. Blanks in the comma-separated parameter list are ignored and can be omitted.
RETURNS defines the data type of the scalar return value. The topic TITLE explains how the return value can be typed.

ABAP_USE
CDS scalar functions are available in CDS view entities in operand positions that expect scalar values, similar to built-in functions. In particular, they can be specified in the following operand positions:
As the right-hand side of a join ON-condition of a CDS view entity as documented in the topic TITLE .
As an element in the element list of a CDS view entity, as documented in the topic TITLE .
As operands in a WHERE condition of a CDS view entity.
As operands of an arithmetic expression in a CDS view entity.
As operand of a case distinction in a CDS view entity.
As operand of a cast expression in a CDS view entity.
As operands of built-in functions wherever documented.
As an operand of a filter condition of a path expression.

ABAP_RESTRICTIONS
SQL-based scalar functions cannot be used in CDS entities other than CDS view entities.
SQL-based scalar functions can currently not be used in AB-SQL .
ABAP_RESTR_END
Once a CDS scalar function has been activated, a scalar function implementation reference must be created. This scalar function implementation reference binds the scalar function to a runtime environment and refers to an AMDP function implementation .
The scalar function is implemented in an AMDP function using the addition FOR SCALAR FUNCTION .



Latest notes:

After activation, a CDS scalar function cannot be renamed.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The following example demonstrates the definition, implementation, and use of an SQL-based scalar function.
CDS scalar function definition
The following CDS scalar function DEMO_CDS_SCALAR_HELLOWORLD has no input parameters. It only defines the data type of the return parameter.
DSFD DEMO_CDS_SCALAR_HELLOWORLD
CDS scalar function implementation reference
The scalar function implementation reference DEMO_CDS_SCALAR_HELLOWORLD_SQL binds the scalar function to the SQL Engine and defines the method EXECUTE of the class CL_DEMO_CDS_SCALAR_HELLOWORLD as AMDP function implementation.
AMDP function implementation
The following listing shows the AMDP function implementation. The value of the return parameter is set to Hello World!.
METH CL_DEMO_CDS_SCALAR_HELLOWORLD=>EXECUTE
Use in a CDS view entity
The following CDS view entity uses the scalar function DEMO_CDS_SCALAR_HELLOWORLD as an element in the SELECT list.
DDLS DEMO_CDS_SCALAR_USE
ABAP_EXAMPLE_END