CDS Table Functions
RAP / CDS / ODataintermediate

CDS Table Functions Part 1: When You Really Need a Table Function

Key takeaway

Decide when a CDS table function is really needed: what it is, what normal CDS view entities can already do, and the typical cases (complex SQLScript logic, ranking, iterative calculations) where a table function is justified.

What this complete series covers

Learn CDS Table Functions in a clear, practical sequence — from the basic idea to real project use, troubleshooting, and interview-ready understanding.

Who it is for
Best for SAP consultants, developers, and learners who want a practical understanding of CDS Table Functions.
Prerequisites
Basic ABAP and ADT in Eclipse are recommended. The series explains the RAP/CDS/OData concepts as they are introduced.
Final outcome
By the end, you should be able to explain CDS Table Functions clearly, follow the main end-to-end flow, apply it in a project scenario, and troubleshoot the most common issues.
  1. Part 1: CDS Table Functions Part 1: When You Really Need a Table Function

    Choose CDS table functions only when a set-oriented requirement genuinely needs SQLScript or HANA-specific database logic that ordinary CDS view entities and modern Open SQL cannot express cleanly.

  2. Part 2: CDS Table Functions Part 2: Building a Table Function with an AMDP Implementation

    Understand the complete design-time contract between DEFINE TABLE FUNCTION and the implementing AMDP class method, including parameters, return structure, SQLScript implementation, and consumption.

  3. Part 3: CDS Table Functions Part 3: Parameters, Client Handling and Session Context

    Design table-function parameters and client-sensitive logic safely by understanding explicit parameters, session context, client fields, and why implicit assumptions can produce cr

  4. Part 4: CDS Table Functions Part 4: Performance with HANA Plan Analysis

    Tune CDS table functions by measuring real SQLScript execution plans, intermediate row counts, predicate pushdown, joins, and data movement instead of assuming database pushdown is

  5. Part 5: CDS Table Functions Part 5: Testing, Clean Core and Alternatives

    Make CDS table functions maintainable with database-aware unit/integration tests, released-data-source checks, fallback alternatives, and explicit clean-core justification.

System and release assumptions: Examples target recent SAP S/4HANA / ABAP Cloud capabilities. RAP, CDS, OData, and Fiori Elements syntax and tooling can differ by ABAP platform release.

Explanation

📌 KEY POINTS
✅ A table function = CDS entity whose data is computed by an AMDP method in SQLScript.

✅ Try a normal CDS view entity first — CASE, casts, aggregation, unions, associations cover most needs.

✅ Use a table function only for logic CDS cannot express efficiently (complex ranking, multi-step SQLScript).

⚠️ Table functions are database-specific (HANA), harder to test and get fewer optimizations — they have a cost.

━━━━━━━━━━━━━━━━━━━━
▶ WHAT THIS SERIES COVERS

  • Part 1 — when you really need a table function (this lesson)
  • Part 2 — building one with an AMDP implementation
  • Part 3 — parameters, client handling and session context
  • Part 4 — performance with HANA plan analysis
  • Part 5 — testing, clean core and alternatives

Running example: purchase request aging with ranking per supplier.

━━━━━━━━━━━━━━━━━━━━
▶ WHAT A TABLE FUNCTION IS

It has two parts:

  • A CDS definition ("define table function") with parameters and a "returns" structure — consumers see it like any CDS entity.
  • An AMDP class method ("FOR TABLE FUNCTION") written in SQLScript that returns the result table.

Consumers — other CDS views, ABAP SQL, RAP queries, analytics — do not see the difference.

━━━━━━━━━━━━━━━━━━━━
▶ WHAT CDS VIEW ENTITIES ALREADY DO

Before writing SQLScript, check whether a view entity can do it:

  • Calculations: CASE, CAST, arithmetic, string functions, date functions (DATS_DAYS_BETWEEN).
  • Aggregations: SUM, COUNT, MIN, MAX with GROUP BY and HAVING.
  • Combinations: joins, associations, UNION.
  • Parameters with session defaults.
  • Currency/unit conversion functions.

Example: "days open" per request is just DATS_DAYS_BETWEEN(created_on, $session.system_date) — no table function needed.

━━━━━━━━━━━━━━━━━━━━
▶ WHEN A TABLE FUNCTION IS JUSTIFIED

  • Ranking and window logic, e.g. "the three oldest open requests per supplier" (ROW_NUMBER() OVER (PARTITION BY …)) where your release does not support it in CDS.
  • Multi-step logic with intermediate tables that would need several stacked views and still be inefficient.
  • Special HANA functions not available in CDS.
  • Performance-critical logic proven (by measurement) to be faster in SQLScript.

━━━━━━━━━━━━━━━━━━━━
▶ THE COSTS

  • HANA-specific code; database knowledge needed for maintenance.
  • Optimizer cannot push consumer filters into the function body as freely as into views — selective inputs should be parameters (Part 4).
  • Client handling and authorization need care (Parts 3 and 5).
  • In ABAP Cloud, only own objects and objects released for AMDP use (C4) may be accessed.

━━━━━━━━━━━━━━━━━━━━
▶ DECISION CHECKLIST

  1. Can a view entity (or a stack of two) express it? → use CDS.
  2. Is the logic ranking/iterative/multi-step? → consider a table function.
  3. Is it performance-driven? → measure both versions first.
  4. Document why CDS was not enough.

✅ CHECKPOINT: you can justify (or reject) a table function. Part 2 builds one.

Code example

ABAP Code
// ===== FIRST CHOICE: a normal CDS view entity (no SQLScript needed) =====define view entity ZI_PurchReqAge  as select from zpr_head{  key request_uuid as RequestUUID,      request_id   as RequestID,      supplier     as Supplier,      status       as Status,      created_on   as CreatedOn,      dats_days_between( created_on, $session.system_date ) as DaysOpen,      case        when dats_days_between( created_on, $session.system_date ) > 30 then 'OVERDUE'        when dats_days_between( created_on, $session.system_date ) > 14 then 'LATE'        else 'ON_TIME'      end as AgingBucket}where status = 'S' // ===== JUSTIFIED CASE: ranking per supplier ("top 3 oldest per supplier") =====// needs ROW_NUMBER() OVER ( PARTITION BY supplier ORDER BY created_on )// -> implemented as a CDS table function + AMDP (Part 2)://// define table function ZTF_PurchReqAgingRank//   with parameters p_keydate : abap.dats//   returns { client : abap.clnt; RequestID : ...; Supplier : ...; AgeRank : abap.int4; }//   implemented by method zcl_tf_purchreq_aging=>get_aging_rank; // ===== Decision checklist =====// 1 view entity possible?          -> CDS view entity// 2 ranking / multi-step logic?    -> table function candidate// 3 performance reason?            -> measure both first// 4 document the reason in the object's header comment

Real project scenario

A developer wrote a table function for "days open per purchase request" because "SQLScript is faster on HANA". A reviewer showed the same result as a five-line CDS view entity using DATS_DAYS_BETWEEN, which was easier to read, testable with standard tools, and — as ST05 showed — equally fast.
The table function was removed. Later, when the business asked for the three oldest open requests per supplier, the team did use a table function with ROW_NUMBER, documented why, and kept it small and isolated.

Common mistakes

  • Writing table functions for logic a view entity can express.
  • Choosing SQLScript for "performance" without measuring.
  • Forgetting client handling and authorization.
  • Not documenting why a table function was needed.
  • Using table functions as the default modeling tool.

Best practices

  • CDS view entities first; table functions as an exception.
  • Use table functions for ranking, window and multi-step logic.
  • Measure before and after when performance is the reason.
  • Keep table functions small and well documented.
  • Check ABAP Cloud rules (C4) for accessed objects.

Interview angle

Questions: "What is a CDS table function?", "When would you use one instead of a CDS view?"
Strong answer: a CDS entity whose result is computed by an AMDP method (FOR TABLE FUNCTION) in SQLScript; consumers use it like any CDS entity. Use it only when view entities cannot express the logic efficiently — ranking/window functions, multi-step SQLScript, special HANA functions — and justify performance claims by measurement. Mention costs: HANA dependency, filter pushdown limits, client handling, testing effort.

Practice this lesson

Quick check

1. A report joins four tables and sums amounts per company code. Which modeling choice fits best?

2. Which requirement best justifies a CDS table function?

3. Which scenario is the best justification for choosing a CDS table function over a standard CDS view entity?

Interview questions

1. When would you use a CDS table function instead of a CDS view entity?

Answer it in your own words first, then compare.

2. What trade-offs do you consider before approving a CDS table function?

Answer it in your own words first, then compare.

Practice more with Interview AI →

Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.

More lessons in this topic

Interview questions on this topic

Related SAP topics