All skills
secondsky avatar

/sap-abap-cds

@620a19a
by Eddiesecondsky/sap-skills456 stars
120

Comprehensive SAP ABAP CDS (Core Data Services) reference for data modeling, view development, and semantic enrichment. Use when creating CDS views or view entities, defining data models with annotations, working with associations and cardinality, implementing input parameters, using built-in functions, writing CASE expressions, implementing access control with DCL, handling CURR/QUAN data types, troubleshooting CDS errors, querying CDS views from ABAP, or displaying data with SALV IDA. Covers ABAP 7.4+ through ABAP Cloud.

Use this Skill: https://skilld.dev/gh/secondsky/sap-skills/sap-abap-cds

This session only. Nothing lands on disk.

referencesexpressions-reference.md

≈2.3k tokens on demand. Your agent reads this file only when SKILL.md points to it.

ABAP CDS Expressions Reference

Complete reference for expressions, operators, and conditional logic in ABAP CDS views.

Source: https://discoveringabap.com/2021/10/13/exploring-abap-on-hana-7-expressions-operations-in-cds-views/

Version Note: $session.user, $session.client, $session.system_language require 7.40 SP08. $session.system_date requires 7.51+. CASE, arithmetic, and CAST expressions are available from 7.40 SP08.


Projection List Elements

The projection list (SELECT list) can contain:

Element Description Alias Required
Field Table/view field Optional
Literal Constant value Yes
Expression Calculated value Yes
Session Variable System value Yes
Association Relationship Optional

Field References

Simple Field

{
  matnr,
  maktx
}

With Source Prefix

{
  source.matnr,
  source.maktx
}

With Alias

{
  matnr as MaterialNumber,
  maktx as Description
}

Key Fields

{
  key matnr,
  key spras,
      maktx
}

Literals

Typed Literals

{
  'EUR' as DefaultCurrency,          -- Character
  100 as DefaultQuantity,            -- Integer
  123.45 as DefaultPrice,            -- Decimal
  abap.dats'20241115' as FixedDate   -- Typed date
}

Untyped Literals

{
  'Active' as StatusText,
  0 as InitialValue
}

Note: Literals require an alias.


Session Variables

Access ABAP system fields:

Variable Equivalent Since
$session.user SY-UNAME 7.4 SP8
$session.client SY-MANDT 7.4 SP8
$session.system_language SY-LANGU 7.4 SP8
$session.system_date SY-DATUM 7.51
{
  $session.user as CurrentUser,
  $session.client as ClientId,
  $session.system_language as Language,
  $session.system_date as Today
}

Comparison Operators

Basic Operators

Operator Description Example
= Equal status = 'A'
<> Not equal status <> 'D'
< Less than amount < 1000
> Greater than amount > 0
<= Less or equal date <= $session.system_date
>= Greater or equal priority >= 1

BETWEEN Operator

where date between '20240101' and '20241231'

-- Equivalent to:
where date >= '20240101' and date <= '20241231'

IN Operator

where status in ('A', 'B', 'C')

-- Equivalent to:
where status = 'A' or status = 'B' or status = 'C'

NULL Checks

where description is null
where description is not null

Pattern Matching (LIKE)

where name like 'SAP%'        -- Starts with SAP
where name like '%GmbH'       -- Ends with GmbH
where name like '%Partner%'   -- Contains Partner
where code like 'A_B'         -- A followed by any char, then B

Escape Character:

where name like '%10#%%' escape '#'  -- Contains literal %

Arithmetic Operations

Basic Arithmetic

Operation Operator Example
Addition + price + tax
Subtraction - gross - discount
Multiplication * quantity * price
Division / total / count
Negation - -amount

Examples

{
  quantity * unit_price as LineTotal,
  gross_amount - discount as NetAmount,
  total / 100 as Percentage,
  -balance as NegatedBalance
}

Operator Precedence

  1. () Parentheses
  2. - Negation
  3. *, / Multiplication, Division
  4. +, - Addition, Subtraction
-- Without parentheses: 10 + (5 * 2) = 20
10 + 5 * 2

-- With parentheses: (10 + 5) * 2 = 30
(10 + 5) * 2

CASE Expressions

Simple CASE

Compare single expression to multiple values:

case status
  when 'A' then 'Active'
  when 'I' then 'Inactive'
  when 'D' then 'Deleted'
  else 'Unknown'
end as StatusText

Searched CASE

Multiple independent conditions:

case
  when amount > 10000 then 'High'
  when amount > 1000 then 'Medium'
  when amount > 0 then 'Low'
  else 'Zero'
end as AmountCategory

Nested CASE

case type
  when 'S' then
    case status
      when 'A' then 'Sales Active'
      when 'C' then 'Sales Closed'
      else 'Sales Other'
    end
  when 'P' then 'Purchase'
  else 'Other'
end as TypeDescription

CASE with Calculations

case indicator
  when 'H' then amount
  when 'S' then -amount
  else 0
end as SignedAmount

CASE in WHERE Clause

where case
        when type = 'A' then priority
        else 0
      end > 5

Note: Using CASE in WHERE clause is useful for conditional filtering without creating intermediate computed fields. However, it may impact query performance compared to post-filtering in ABAP or using separate WHERE conditions. Use judiciously on large datasets.


Logical Operators

AND

Both conditions must be true:

where status = 'A' and type = 'S'

OR

At least one condition must be true:

where status = 'A' or status = 'B'

NOT

Negates condition:

where not status = 'D'
-- Equivalent to: where status <> 'D'

Complex Logic

where (status = 'A' and type = 'S')
   or (status = 'B' and priority > 5)

Precedence

  1. NOT
  2. AND
  3. OR

Use parentheses for clarity:

-- Unclear:
where a = 1 or b = 2 and c = 3

-- Clear:
where a = 1 or (b = 2 and c = 3)

Aggregate Expressions

Functions

Function Description
sum(field) Sum of values
avg(field) Average
min(field) Minimum
max(field) Maximum
count(*) Row count
count(distinct field) Distinct count

GROUP BY

Required for non-aggregated fields:

{
  customer,
  sum(amount) as TotalAmount,
  count(*) as OrderCount
}
group by customer

HAVING

Filter aggregated results:

{
  customer,
  sum(amount) as TotalAmount
}
group by customer
having sum(amount) > 10000

Complete Example

define view Z_CUSTOMER_SUMMARY as select from sales_order
{
  customer,
  sum(amount) as TotalSales,
  avg(amount) as AvgOrderValue,
  min(order_date) as FirstOrder,
  max(order_date) as LastOrder,
  count(*) as OrderCount,
  count(distinct product) as UniqueProducts
}
group by customer
having count(*) >= 5

Path Expressions

Navigate through associations:

Basic Path

_Customer.name1 as CustomerName

Multi-level Path

_Order._Customer._Country.name as CountryName

Path with Filter

_Items[ItemNumber = '000010'].Material as FirstMaterial

Cardinality Indicator

When filter reduces to single result:

_Items[1: Status = 'A'].Quantity as ActiveQty

Conditional Navigation

Combine CASE with paths:

case
  when type = 'C' then _Customer.name1
  when type = 'V' then _Vendor.name1
  else 'Unknown'
end as PartnerName

NULL Handling

COALESCE

Return first non-null value:

coalesce(override_price, standard_price, 0) as EffectivePrice

CASE with NULL

case
  when field is null then 'Not Set'
  else field
end as SafeField

Arithmetic with NULL

NULL in arithmetic produces NULL:

-- If discount is NULL, result is NULL
price - discount as NetPrice

-- Safe version:
price - coalesce(discount, 0) as NetPrice

Type Conversion

Implicit Conversion

Some conversions happen automatically.

Explicit CAST

cast(numeric_field as abap.char(10)) as TextField,
cast(char_field as abap.int4) as IntField,
cast(amount as abap.curr(15,2)) as FormattedAmount

Common Type Conversions

From To Example
NUMC CHAR cast(numc as abap.char(n))
INT CHAR cast(int as abap.char(n))
CHAR INT cast(char as abap.int4)
CURR DEC cast(curr as abap.dec(n,d))

Best Practices

  1. Use parentheses: Clarify complex logic
  2. Handle NULL: Use COALESCE or CASE
  3. Alias expressions: Always name calculated fields
  4. Type consistency: Ensure CASE branches return same type
  5. Performance: Simple expressions perform better

Documentation Links

Last Updated: 2025-11-23

Source: SKILL.md on GitHub

1 warning15d4 checks · Risk SAFE
  • Gen Agent Trust Hub15d

    The skill provides comprehensive reference material, templates, and troubleshooting guides for SAP ABAP CDS development. It consists entirely of documentation and static templates without executable code. All external references point to official SAP resources and well-known community repositories.

  • Socket15d

    No alerts

  • Snyk15d

    Risk: LOW · No issues

  • Runlayer6mo

    5/12 files flagged

Signed by skilld at 620a19a. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 weeks ago.

Activeupdated 2 months ago
Other metadata
metadata
{
  "maintainer": "Eduard Jiglau",
  "maintainer_email": "hello@sap-ai-skills.com",
  "website": "https://sap-ai-skills.com",
  "version": "2.4.1",
  "last_verified": "2026-04-02",
  "abap_release": "7.4 SP8+ / 7.50+ / ABAP Cloud",
  "sources": [
    "https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/abencds.html",
    "https://github.com/SAP-samples/abap-cheat-sheets"
  ],
  "keywords": [
    "ABAP CDS",
    "Core Data Services",
    "CDS view",
    "CDS view entity",
    "define view",
    "DDL",
    "DCL",
    "annotations",
    "@AbapCatalog",
    "@AccessControl",
    "@EndUserText",
    "@Semantics",
    "@UI",
    "@Consumption",
    "@ObjectModel",
    "associations",
    "cardinality",
    "path expressions",
    "input parameters",
    "WITH PARAMETERS",
    "built-in functions",
    "CASE expression",
    "CAST",
    "session variables",
    "GROUP BY",
    "HAVING",
    "joins",
    "access control",
    "DEFINE ROLE",
    "pfcg_auth",
    "SALV IDA",
    "Eclipse ADT",
    "CDS annotations",
    "Fiori Elements",
    "OData",
    "RAP",
    "currencyCode",
    "unitOfMeasure",
    "SD_CDS_ENTITY105"
  ]
}

README badge

README badge for secondsky/sap-skills/sap-abap-cds