Core Data Services (CDS) ABAP

CDS Scalar Functions

Motivation: Why Scalar Functions?

CDS Views and Table Functions are designed for tabular results.
But what if we want to compute a single value?

✅ Reusable calculations
✅ Uses SQLScript logic inside the CDS context
✅ Can be embedded in CDS Views, AMDP, or ABAP SQL

What Is a Scalar Function?

A CDS Scalar Function is a function that:

  • Accepts one or more input values
  • Returns exactly one value
  • Is declared in a DDLS
  • Is implemented using SQLScript
  • Has a scalar return type (e.g., abap.int4, abap.string, abap.bool)

➡️ Used like a normal function call:
get_priority_cat( t.priority )

Defining a Scalar Function

define scalar function ZUDF_NAME
  with parameters
    Firstname: abap.char( 50 ),
    Lastname:  abap.char( 50 )
  returns abap.char( 53 ); // Jörg Brandeis ==> J. Brandeis

➡️ Multiple parameters possible
➡️ Return value is exactly one field

Scalar Function Implementation Reference

The link between the function and its implementation is a separate development object.

The name must end with _SQL.

Implementation Using AMDP

CLASS zcl_amdp_udf DEFINITION
  PUBLIC FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    INTERFACES if_amdp_marker_hdb.

    CLASS-METHODS name FOR SCALAR FUNCTION zudf_name.
ENDCLASS.


CLASS zcl_amdp_udf IMPLEMENTATION.
  METHOD name BY DATABASE FUNCTION FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY.

    result = left( upper( Firstname ), 1 ) || '- ' || initcap( Lastname );
  ENDMETHOD.
ENDCLASS.

Usage in CDS Views

define view entity ZI_00_USERS
  as select from zbc_users
{
  key user_id as UserId,

      zudf_Name( Firstname => firstname,
                 Lastname  => lastname ) as Name
}

➡️ As simple to use as any other SQL function!

Usage in ABAP SQL

➡️ According to documentation this is supported in cloud systems (Public/Steampunk),
but as of 2023 it still produced errors.

SELECT FROM zbc_users
  FIELDS user_id,

         zudf_Name( Firstname = firstname,
                    Lastname  = lastname ) AS NAME
  INTO TABLE @DATA(Result).

Typical Use Cases

✅ Categorization (e.g., priority, age, revenue groups)
✅ Validation & transformation of input values
✅ Abbreviations, formatting, quick calculations
✅ Reusable business logic implemented in SQLScript

Restrictions & Notes

  • Performance must be tested carefully
  • No side effects allowed (read-only)
  • Input parameters must be scalar

Summary

✅ CDS Scalar Functions = small, reusable calculation units
✅ Return exactly one value
✅ Usable in CDS, Open SQL, AMDP
✅ Implemented with SQLScript