This post talks about

  1. What is CDS Table Function?
  2. How to Create CDS Table Function Definition?
  3. How to Implement a CDS Table Function?

This is a post from series ABAP on HANA.

What is CDS Table Function?

ABAP CDS table functions define table functions that are implemented natively on the database and can be called in CDS. This is as per the SAP documentation.

In simple words,

  1. CDS table function combines CDS View and AMDP Method
  2. It is defined similar to CDS except for data source i.e. you create a field list similar to CDS View
  3. The data fetching logic is written inside AMDP Method

If this is still confusing – let us go through the creation process and then revisit these 3 statements.

Create CDS Table Function

1. In Eclipse ABAP perspective, right click on your package and choose New > Other Repository Object

2. Select Data Definition under Core Data Services and click Next.

3. Enter Name and Description, Click Next

4.Select a TR and Click on Next

5. Select a template Define Table Function with Parameters and click Finish

6. The template appears in the editor.

7. Complete the definition as below.

This defines a RETURN table. The output is always a table.

@EndUserText.label: 'Table Function for Flight Data'
define table function ZJP_FLIGHT_TABLE_FUNCTION
with parameters carrier : /dmo/carrier_id
returns {
key client        : abap.clnt;
key carrier_id    : /dmo/carrier_id;
key connection_id : /dmo/connection_id;
key flight_date   : /dmo/flight_date;
@Semantics.amount.currencyCode : 'currency_code'
price             : /dmo/flight_price;
@Semantics.currencyCode: true
currency_code     : /dmo/currency_code;
plane_type_id     : /dmo/plane_type_id;
seats_max         : /dmo/plane_seats_max;
seats_occupied    : /dmo/plane_seats_occupied;
}
implemented by method zjp_flight_class=>get_flights;

Notice that unlike CDS, table function definition does not have details of data source, so you need to mention the data types for each field.

8. Save it. Do not try to activate it at this point as the method and class does not exist.

Implement the CDS Table Function

1. Create a new class zjp_flight_class and add an AMDP Marker interface. (if_amdp_marker_hdb)

Refer Object Oriented ABAP [1] : Introduction to ABAP Class if you need help with this.

2. Define a static method get_flights with addition FOR TABLE FUNCTION with table function name. Add implementation for the method as below.

Save the class. Ignore the error for the moment.

CLASS zjp_flight_class IMPLEMENTATION.

  METHOD get_flights BY DATABASE FUNCTION 
                     FOR HDB LANGUAGE SQLSCRIPT
                     USING /dmo/flight.
    
    RETURN SELECT * from "/DMO/FLIGHT" 
        WHERE carrier_id = :carrier; 
  
  ENDMETHOD.

ENDCLASS.

3.Click on Activate inactive objects. This allows you to active multiple objects at a time.

4. Select the class and table function objects and click Activate.

Important : As there is interdependency of CDS and Class on each other, we need to activate both of them together.

If we want to be technically more accurate, the implementation is done as DATABASE FUNCTION and not DATABASE PROCEDURE, so we probably should not call this AMDP, but its easier for understanding.

Method Explanation

Definition
  • The method gets Importing and Returning parameters automatically from CDS table function where CDS parameters become the Importing Parameters and it has only one table as returning parameter with fields defined in the CDS.
  • FOR TABLE FUNCTION key word followed by CDS Table Function name is used to define the method.
Implementation
  • DATABASE FUNCTION is used in place of DATABASE PROCEDURE. Function returns a single table unlike AMDP which can return multiple tables.
  • The parameters defined in CDS Table Function can be directly access here using :parameter_name
  • RETURN statement is mandatory here. It can be used to pass result of query directly or any table used in the script.

Let us Test the CDS Table Function

1. It can be tested similar to any CDS View by running as ABAP Application.

2. Provide a value to the parameter.

3. The data can be seen as below

The CDS Table Function can be used to take benefits of CDS with flexibility of SQLSCRIPT.

Visit ABAP on HANA series for Tutorials on CDS, AMDP, Eclipse, and ALV IDA.


If you like the content, please subscribe…

Join 4,032 other subscribers

Discovering ABAP YouTube Channel