ABAP for HANA: CDS Views & AMDP

ABAP CDS defines semantically rich data models in the database with SQL-like DDL plus annotations.

33.1 Core Data Services (CDS)

ABAP CDS defines semantically rich data models in the database with SQL-like DDL plus annotations. CDS entities are created in ADT (*New → Data Definition*) and are the foundation of analytics, Fiori, RAP, and released APIs.

CDS view entity define view entity Current standard (7.55+); replaces DDIC-based define view CDS projection view define view entity … as projection on Service-specific layer in RAP Abstract / custom entity

define abstract entity / define custom entity Action parameters / data from ABAP class (query provider) define table function Implemented by AMDP define hierarchy define role (DCL) annotate entity UI annotations separated from the model Table entity / external entity define table entity (newer releases) Emerging features — check your release

33.2 A basic view entity

Features: joins and associations (lazy joins, followed via path expressions like _Customer.CustomerName), case, arithmetic, built-in functions (concat, substring, coalesce, cast, currency/unit conversion currency_conversion( ), date functions), aggregates with group by, union, parameters (with parameters p_date : abap.dats), session variables ($session.user, $session.client, $session.system_date, $session.system_language).

@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales order - interface view'
define view entity ZI_SalesOrder
  as select from zorders as o
  association [1..1] to I_Customer as _Customer on $projection.Customer = _Customer.Customer
  association [0..*] to ZI_SalesOrderItem as _Item on $projection.SalesOrder = _Item.SalesOrder
{
  key o.order_id                         as SalesOrder,
      o.customer                         as Customer,
      o.order_date                       as OrderDate,
      @Semantics.amount.currencyCode: 'Currency'
      o.amount                           as GrossAmount,
      o.currency                         as Currency,
      o.status                           as OverallStatus,
      case o.status
        when 'O' then 'Open'
        when 'C' then 'Completed'
        else 'Unknown'
      end                                as StatusText,
      dats_days_between( o.order_date, $session.system_date ) as AgeInDays,

      /* exposed associations */
      _Customer,
      _Item
}

33.3 Aggregation and parameters

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Revenue per customer'
define view entity ZI_CustomerRevenue
  with parameters
    p_from : abap.dats
  as select from ZI_SalesOrder
{
  key Customer,
      Currency,
      @Semantics.amount.currencyCode: 'Currency'
      sum( GrossAmount ) as TotalRevenue,
      count( * )         as OrderCount
}
where OrderDate >= $parameters.p_from
group by Customer, Currency

33.4 The Virtual Data Model (VDM)

S/4HANA organizes CDS views in layers:

Basic / interface views (I_…, e.g. I_Customer, I_SalesOrder) — reusable, close to the tables; many are released (C1 contract) for custom use.

Composite views — combine basic views.

Consumption / projection views (C_…) — tailored to an app or analytics query.

Custom views use Z names, by convention ZI_ (interface), ZC_ (consumption/projection), ZR_ (RAP root in newer guidelines).

33.5 Important annotation families

Require DCL role (#CHECK, #NOT_REQUIRED, #MANDATORY) @EndUserText.label / .quickInfo

@Semantics.amount.currencyCode, .quantity.unitOfMeasure Amount/quantity references

@ObjectModel.text.element, .representativeKey @UI.lineItem, .identification, .selectionField, .facet, .headerInfo Fiori elements layout

@Analytics.dataCategory, @Aggregation.default Analytics (cubes, dimensions)

@Search.searchable, @Search.defaultSearchElement Allow metadata extensions

33.6 ABAP Managed Database Procedures (AMDP)

AMDP lets you write SQLScript (HANA's procedural SQL) inside ABAP class methods; ABAP manages deployment and transport.

Rules: parameters by value, no RETURNING, all used tables listed in USING, client handled explicitly (or via AMDP OPTIONS CDS SESSION CLIENT current). CDS table functions expose AMDP results as CDS entities:

Use AMDP only when CDS/ABAP SQL can't express the logic — CDS is portable, easier to read, and supports authorizations natively.

CLASS zcl_amdp_revenue DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES if_amdp_marker_hdb.
    TYPES: BEGIN OF ty_rev,
             customer TYPE kunnr,
             revenue  TYPE p LENGTH 15 DECIMALS 2,
             rank_no  TYPE i,
           END OF ty_rev,
           tt_rev TYPE STANDARD TABLE OF ty_rev WITH EMPTY KEY.
    METHODS get_top_customers
      IMPORTING VALUE(iv_client) TYPE mandt
                VALUE(iv_top)    TYPE i
      EXPORTING VALUE(et_result) TYPE tt_rev.
ENDCLASS.

CLASS zcl_amdp_revenue IMPLEMENTATION.
  METHOD get_top_customers BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT
                           OPTIONS READ-ONLY USING zorders.
    lt_rev = SELECT customer, SUM( amount ) AS revenue
               FROM zorders
              WHERE mandt = :iv_client
              GROUP BY customer;
    et_result = SELECT customer, revenue,
                       RANK( ) OVER ( ORDER BY revenue DESC ) AS rank_no
                  FROM :lt_rev
                 ORDER BY revenue DESC
                 LIMIT :iv_top;
  ENDMETHOD.
ENDCLASS.
define table function ZTF_TopCustomers
  with parameters p_top : abap.int4
  returns { client : abap.clnt; customer : kunnr; revenue : abap.dec(15,2); }
  implemented by method zcl_amdp_tf=>get_top;