Table Function with Parameters


Let’s create a Simple Table Function right away…

Steps to create and test CDS Table Function which is always created along with AMDP class method:

  1. Right Click on Project > New > Other > Data Definition
  2. Provide Package, CDS name, and Description and Click Next
  3. Select Package if Package is not $TMP and Click Next
  4. Select “Define Table Function with Parameters” and Click Finish
  5. Code the CDS Table function in CDS Editor as below:
    1. Table Functions may start with Header annotations like in this example, @ClientHandling.type defines whether CDS is Client dependent or not.
    2. Table Function is defined using syntax DEFINE TABLE FUNCTION. Here CDS Table Function name is ZCDS_TABLE_FUNCTION1.
    3. Table function in this example is Parameter enabled which is made evident with syntax WITH PARAMETERS. In this case, annotation @Environment.systemField : #CLIENT specifies that the Parameter clnt with type syst_mandt is an optional parameter and if Parameter is not passed, the default value of clnt will be taken from System environment context. This means that any other entity e.g. ABAP program which consumes this CDS, may or may not pass this Parameter.
    4. The table function returns set of fields and this is specified in section-d. In this example, there are two return attributes viz. Mandt(Client) and matnr(Material Number). The first Parameter is mandatory on the first position because we defined this Table Function Client dependent on top annotation: @ClientHandling.type. The second field is Material number which selection logic will be written in AMDP(ABAP Managed Database Procedure) defined in next step.
    5. Table Function is implemented by a class method which must be an AMDP(ABAP Managed Database Procedure). AMDP to be called is defined by syntax IMPLEMENTED BY METHOD. We define the Class of AMDP method along with the class.
      • Note: During activation of Table Function, it is not necessary that AMDP class/method is already created. Table function must be activated first to activate AMDP class/method.
  6. Create a Global class to consume CDS Table function as below:
    1. AMDP class which is called in Table Function is to be defined separately using Eclipse ADT since AMDP creation is not provisioned in SAP GUI workbench. The name of the class is ZCDS_TABLE_FUNCTION in this example.
      • Note: The name of CDS Table Function and Class can’t be same. Also, there is one to one relationship between Table function and Class.
    2. This is mandatory to define marker interface IF_AMDP_MARKER_HDB for an AMDP. This is the way to tell ABAP compiler that particular class is special. In this case of AMDP, we can write embedded non-ABAP code(SQL script) in this class!
    3. A table function is implemented by a class method. The class method must be defined with syntax FOR TABLE FUNCTION with mention of Table Function name.
    4. AMDP method implementation has particular signature for Table function which mentions function is for HANA database with BY DATABASE FUNCTION FOR HDB, script language. Options READ-ONLY for performance optimization and USING keyword for the mention of to be used Tables/Methods.
    5. Section-e declares a local table with two fields in SQL script. This is of course not ABAP so data type and syntax are different.
      • Notice the comment initiated by ‘–’. This is possible because this is an SQL script comment and not an ABAP comment!
    6. This section SELECTs two fields MANDT and MATNR from MARA table and also returns a table from the method.
      • Small tip: Goto Eclipse> Windows > Preferences > General > Appearance > Color and Fonts > ABAP > Syntax Coloring > Embedded Languages (background color) > Edit to set a different background color to differentiate SQL script from normal ABAP
  7. Right click on CDS Editor > Open with > Data preview
  8. Check the output

Feel free to Clone the code from GitHub.


Leave a Reply