In SAP ABAP CDS Views, you can use table functions to enhance the data modeling capabilities of a CDS view. A table function is a reusable piece of logic that can be invoked within a CDS view. It can accept parameters and return a table, which can be further processed or joined with other data sources.

What is a Table Function in CDS Views?

A table function is a dynamic view or a piece of reusable logic that is executed within the context of a CDS view. It can receive input parameters and return a table as output. These functions can be useful for complex calculations, data transformations, or joining data sources that would otherwise require multiple views.

How to Define a Table Function in CDS Views

A table function is defined in the CDS view using the @ObjectModel.Implementation annotation.

Here is the basic structure:

@AbapCatalog.sqlViewName: 'ZFLIGHT_CDS'
@EndUserText.label: 'CDS View with Table Function Example'
define view ZFLIGHT_CDS as select from sflight {
key carrid,
key connid,
fldate,
price
}

Syntax for Defining a Table Function

To define a table function in a CDS view, use the following syntax:

define table function ZFLIGHT_TABLE_FUNC
with parameters
p_carrid: s_carrid,
p_date: abap.dats
returning value as table of ZFLIGHT

In this example:

Example: Table Function in a CDS View

Here is a more detailed example of how to create a table function and call it within a CDS view:

1. Define a Table Function in a CDS View

@AbapCatalog.sqlViewName: 'ZFLIGHT_FUNC'
@EndUserText.label: 'Flight Data Function Example'
define view ZFLIGHT_FUNC with parameters p_carrid: s_carrid, p_date: abap.dats as select from sflight {
key carrid,
key connid,
fldate,
price
}
where carrid = :p_carrid
and fldate > :p_date

2. Define the Table Function Implementation in the CDS

@ObjectModel.Implementation: 'ZFLIGHT_TABLE_FUNC'
define table function ZFLIGHT_TABLE_FUNC
with parameters
p_carrid: s_carrid,
p_date: abap.dats
returning value as table of ZFLIGHT {
key carrid,
key connid,
fldate,
price
}

Explanation:

3. Using the Table Function in a CDS View

Once you have defined the table function, you can use it within a CDS view to access the data it returns.

define view ZFLIGHT_DATA as select from ZFLIGHT_TABLE_FUNC( p_carrid = 'LH', p_date = '2025-01-01' ) {
key carrid,
key connid,
fldate,
price
}

Explanation:

Key Points about Table Functions in CDS Views:

  1. Input Parameters: Table functions can accept parameters that allow dynamic filtering and calculations based on user input.
  2. Returning a Table: Table functions return a table (a set of rows) which can be further processed or consumed in ABAP programs.
  3. Reusable Logic: Table functions enable reusable business logic, especially for complex data operations.
  4. Annotation @ObjectModel.Implementation: This annotation links the table function to the CDS view.
  5. Parameterization: Parameters allow for dynamic behavior based on user inputs or runtime values.

Benefits of Table Functions in CDS Views:

Conclusion:

Table functions in SAP ABAP CDS Views are a powerful tool to implement complex data logic that can be reused across different views or reports. They are parameterized and can return a table, which makes them a flexible option for sophisticated data modeling and manipulation in the SAP ecosystem.