Core Data Services (CDS) ABAP

Syntax Basics & First Example

DDIC-Based CDS View vs. CDS View Entity

(DDIC-Based) CDS Views

As a differentiation from CDS View Entities, the classic CDS Views were renamed to DDIC-Based CDS Views.

  • Until 7.55 they were the only CDS view type
  • Obsolete since 7.57
  • SAP uses thousands of them in the Virtual Data Model

➡️ Even though they are officially obsolete, they will continue to be used and supported for many decades.

CDS View Entities (1/2)

The CDS concept was extended with View Entities in ABAP 7.55.
SCN Blog 1
SCN Blog 2

Differences

  • No DDIC view is generated
  • Only one single name for DDL file, CDS view, and SQL view
  • Activation is faster
  • HANA-only
  • More expression features

CDS View Entities (2/2)

Usage in Frameworks

Where SQL View names were required for DDIC-based CDS Views, the entity name itself can now be used:

  • Extractor Views when creating a DataSource in BW
  • Name of CDS InfoProviders or CDS Queries with prefix 2C
  • When using the view in AMDP

One DDIC-Based CDS View – Three Objects

  • DDL File – Source file created and edited in ADT/Eclipse
  • CDS View – The CDS object from ABAP perspective
  • CDS DDIC/SQL View – The CDS object from DB perspective

TADIR entry

Artifacts in Eclipse

DDL File

  • Created in Eclipse
  • Transportable

CDS View

  • Generated from DDL
  • Usable as ABAP table type
  • Usable in ABAP SELECT
  • Supports CDS Access Control
  • Visible as source in SE11
  • Not usable in AMDP

CDS DDIC & SQL View

  • Database view in HANA
  • Visible as structure in SE11
  • Technically usable in ABAP (not recommended)
  • Usable in AMDP

Names of the 3 DDIC-Based CDS View Objects

The DDL file name is normally identical to the CDS view name.
The CDS view can be renamed before first activation — do NOT do this.

Names of CDS View (max 30 chars) and SQL View (max 16 chars) share a namespace → they must differ.

Data Model for the Examples

data model

Demo – Creating a DDIC-Based CDS View

Please watch first — do not work along yet.

@AbapCatalog.sqlViewName: 'ZV##_CDS_DEMO'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Demo CDS View'
define view z##_cds_demo
  as select from zbc_tasks as t
{
  key t.task_key    as TaskKey,
      t.summary     as Summary,
      t.status      as Status,
      t.project     as Project,
      t.description as Description,
      t.assignee    as Assignee,
      t.type        as Type,
      t.author      as Author,
      t.due_date    as DueDate,
      t.priority    as Priority
}

Exercise – Create a DDIC-Based CDS View

@AbapCatalog.sqlViewName: 'Z##_DEMO'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Demo CDS View'
define view z##_cds_demo
  as select from zbc_tasks as t
{
  key t.task_key    as TaskKey,
      t.summary     as Summary,
      t.status      as Status,
      t.project     as Project,
      t.description as Description,
      t.assignee    as Assignee,
      t.type        as Type,
      t.author      as Author,
      t.due_date    as DueDate,
      t.priority    as Priority
}

Exercise – Create CDS View Entity with Join

@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Join '
@ObjectModel.usageType:{
    serviceQuality: #X,
    sizeCategory: #S,
    dataClass: #MIXED
}

define view entity zi_##_tasks_join
  as select from zbc_tasks as t
    left outer join zbc_users as u 
      on t.assignee = u.user_id
{
  key t.task_key    as TaskKey,
      t.summary     as Summary,
      t.status      as Status,
      t.project     as Project,
      t.description as Description,
      t.assignee    as Assignee,
      u.firstname   as AssigneeFirstname,
      u.lastname    as AssigneeLastname,
      t.type        as Type,
      t.author      as Author,
      t.due_date    as DueDate,
      t.priority    as Priority
}

Syntax in CDS Views

  • Identifiers are case-insensitive, max. 30 chars, allowed: letters, numbers, _ and /
  • CamelCase is common
  • Literals:
    • Integers without decimal
    • With dot → float
    • Strings in '...'
  • Comments:
    • //
    • /* ... */
  • Dot is the separator between table and field → Tasks.Summary

Syntax Variants

Field list after FROM clause

define view ZCDS_where
  as select from snwd_so as so
    left outer join snwd_pd as pd 
    on so.buy_contact_guid = pd.node_key
{
    so_id,
    pd.product_id,
    ...
}

Advantage: FROM clause is visible immediately.

define view ZCDS_where
  as select
    so_id,
    pd.product_id
    ...
  from snwd_so as so
    left outer join snwd_pd as pd 
    on so.buy_contact_guid = pd.node_key

Annotations

Annotations provide metadata for different frameworks, e.g.

  • @Analytics
  • @Consumption
  • @EnterpriseSearch
  • @ObjectModel
  • @OData
  • @Search
  • @UI

They are mostly irrelevant for SQL generation.

Annotation Levels

Annotation always appears before the annotated element.

On CDS Views

@EndUserText.label: 'Extractor for SO Data'
@Analytics.dataExtraction.enabled : true

Parameters

@EndUserText.label: 'Language'
@Environment.systemField: #SYSTEM_LANGUAGE

Fields

@Semantics.amount.currencyCode: 'CURR'
@Semantics.currencyCode: true

Annotation Syntax (1/2) – Path

Direct path:

@Annotation.SubAnnotation.SubSub: <value>

With braces:

@Annotation: { Sub1: <value>,
               Sub2: { SubSub1: <value>,
                       SubSub2: <value> } }

Annotation Syntax (2/2) – Values

<value> may be:

  • Enumeration beginning with #
  • String literal
  • Numeric literal
  • Field/association names
  • true/false
  • Arrays: ['a', 'b'] or objects: [{...}, {...}]

Autocomplete via Ctrl + Space in ADT.

Annotation Example

define view zcds_Parameter
  with parameters 
    @EndUserText.label: 'Language'
    @Environment.systemField: #SYSTEM_LANGUAGE  
    p_langu : abap.lang,

    @EndUserText: { label: 'Client',
                    quickInfo: 'Client info' }
    @Environment: { systemField: #CLIENT }
    p_mandt : abap.clnt(3)

Inheritance of Annotations

Field annotations are inherited.
View-level/parameter-level annotations are not.

Disable inheritance:

@Metadata.ignorePropagatedAnnotations: true

Default = true for view entities.

Some field annotations actually apply to the whole view:

@UI.facet

SQL Preview

SQL preview

SQL CREATE statement

Dependency Analyzer

Active Annotations

active annotations