CDS Table Functions Part 1: When You Really Need a Table Function
Free sample lesson · open to everyone
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.
In this lessonShow sectionsHide sections
Expert visual walkthrough
When You Really Need a Table Function
CDS Table Functions · Chapter-specific technical teaching map
Compare key concepts
CDS view / Open SQL
SQLScript calculation need
Table function and AMDP
Cloud restrictions
Tutor's chapter 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.
In a real project
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…
Watch out for
• 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. •…
The situation: a 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.
Explanation
This series runs across five parts: this lesson covers when a table function is really needed; Part 2 builds one with an AMDP implementation; Part 3 covers parameters, client handling and session context; Part 4 covers performance with HANA plan analysis; and Part 5 covers testing, clean core and alternatives. The running example throughout is purchase request aging with a ranking per supplier.
A table function has two parts. The CDS definition, written with "define table function", declares parameters and a "returns" structure, so consumers see it like any other CDS entity. An AMDP class method, written "FOR TABLE FUNCTION" in SQLScript, actually returns the result table. Consumers — other CDS views, ABAP SQL, RAP queries, analytics tools — see no difference between this and a regular view.
Before reaching for SQLScript, it's worth checking what a plain CDS view entity can already do. It handles calculations (CASE, CAST, arithmetic, string functions, date functions like DATS_DAYS_BETWEEN), aggregations (SUM, COUNT, MIN, MAX with GROUP BY and HAVING), combinations (joins, associations, UNION), parameters with session defaults, and currency or unit conversion functions. For example, "days open" per request is simply DATS_DAYS_BETWEEN(created_on, $session.system_date) — no table function required.
A table function becomes justified in a few specific situations: ranking and window logic, such as finding the three oldest open requests per supplier with ROW_NUMBER() OVER (PARTITION BY …), when the release in use doesn't support that in CDS; multi-step logic with intermediate tables that would otherwise need several stacked views and still end up inefficient; special HANA functions that aren't available in CDS; or performance-critical logic that has actually been measured to be faster in SQLScript.
That said, table functions come with real costs. The code becomes HANA-specific, requiring database knowledge to maintain. The optimizer can't push consumer filters into the function body as freely as it can with views, so selective inputs need to be made parameters, as covered in Part 4. Client handling and authorization need particular care, covered in Parts 3 and 5. And in ABAP Cloud, only your own objects and objects released for AMDP use (release contract C4) may be accessed.
A simple decision checklist helps: first ask whether a view entity, or a stack of two, can express the logic — if so, use CDS. If the logic is ranking, iterative, or multi-step, consider a table function. If the driver is performance, measure both versions before deciding. Either way, document why CDS wasn't enough. With that, the choice to build — or reject — a table function can be justified, and Part 2 goes on to build one.
Code example
// ===== 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 commentWatch out
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.
Do this
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.
In the interview
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
0 of 3 questions attempted
Question 1 of 3
A report joins four tables and sums amounts per company code. Which modeling choice fits best?
Question 2 of 3
Which requirement best justifies a CDS table function?
Question 3 of 3
Which scenario is the best justification for choosing a CDS table function over a standard CDS view entity?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1When would you use a CDS table function instead of a CDS view entity?
2What trade-offs do you consider before approving a CDS table function?
See it in code
Each example starts from a requirement, shows the design, then the code step by step with a plain-language explanation of every part.
- AMDP and a CDS table function: aggregate orders in SQLScript on HANA
- CDS date functions: days requested, a reminder date and an urgency flag
- Clean core: read products with the released CDS views I_Product and I_ProductDescription
- Prepare S/4HANA data for a data product: a CDS view enabled for extraction with delta
Was this lesson helpful?
Keep exploring
Referenced SAP objects
Reviewed reference pages for the objects this page mentions.
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.