Day 2: CDS View Basics
Click Here To Explore Day 1: Introduction to ABAP on HANA
Day 2: CDS View Basics
Syntax and structure of CDS
Data elements, labels, associations, joins
Types: Basic View, Composite View
Predefined functions
Annotations: @AbapCatalog, @EndUserText
Hands-on:
Create 2–3 CDS views with associations
Practice INNER JOIN, LEFT OUTER JOIN
📘 Syntax and Structure of CDS Views
SAP recommends using the DEFINE VIEW ENTITY syntax for defining CDS views, especially in S/4HANA and RAP models. This approach simplifies the syntax and eliminates the need for certain annotations like @AbapCatalog.sqlViewName.
Example:
@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Customer Basic View'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
dataClass: #MIXED
}
define view entity ZDEMO_Customer as select from snwd_bpa
{
key node_key as GenericNodeKey,
bp_id as BusinessPartnerID,
company_name as CompanyName,
email_address as EmailAddress
}
In this example, snwd_bpa is the source table, and we're selecting specific fields with aliases for clarity.
🧩 Data Elements, Labels, Associations, and Joins
🔹 Data Elements and Labels
Data elements define the technical attributes of a field, such as data type and length. Labels, specified using annotations like @EndUserText.label, provide descriptive text for fields and views, enhancing readability and usability in UI applications.
🔹 Associations and cardinality in CDS Views.
An Association in CDS defines a relationship between two entities, similar to a foreign key relationship in traditional databases. Associations allow for navigation from one entity to related data in another, enabling modular and reusable data models.
Example:
association [0..1] to I_Country as _Country
on $projection.Country = _Country.Country
This defines an optional association to the I_Country view, allowing access to country-related information.
🔹 Understanding Cardinality
Cardinality specifies the nature of the relationship between the source and target entities in an association. It indicates how many records in the target entity relate to a single record in the source entity.
Common cardinalities:
[0..1]: Zero or one related record.
[1..1]: Exactly one related record.Medium+2LinkedIn+2SAP Help Portal+2
[0..*]: Zero or many related records.SAP Help Portal
[1..*]: One or many related records.SAP Help Portal
Example:
define view entity ZI_SalesOrder
as select from vbak
association [0..*] to vbap as _Items
on $projection.vbeln = _Items.vbeln
{
key vbak.vbeln,
vbak.erdat,
_Items
}
In this example:
Each sales order (vbak) can have zero or more associated items (vbap).
The association _Items allows navigation to the related items.
Best Practices:
Always define the cardinality to match the actual data relationship.
Incorrect cardinality can lead to performance issues or incorrect query results.
Use associations to simplify complex joins and enhance readability.
🔹 Joins
Joins combine data from multiple tables based on specified conditions. CDS views support various types of joins:
Inner Join: Returns records with matching values in both tables.
Left Outer Join: Returns all records from the left table and matched records from the right table.
Right Outer Join: Returns all records from the right table and matched records from the left table.
Cross Join: Returns the Cartesian product of the two tables.
Example:
from scustom as cust
inner join scity as city
on cust.city = city.id
This inner join combines customer data with city information where the city IDs match.
🧱 Types of CDS Views
SAP categorizes CDS views into several types, each serving a specific purpose:
Basic View: Directly maps to a single table, providing a foundation for other views.
Composite View: Combines multiple basic views or tables, adding business logic.
Consumption View: Designed for consumption by applications, often incorporating annotations for UI and analytics.
Interface View: Defines a stable contract for data access, used in APIs.
Projection View: Simplifies access to a subset of fields from another view, commonly used in RAP.
🏷️ Common Annotations
Annotations enrich CDS views with metadata, influencing behavior and integration. Key annotations include:
@EndUserText.label: Provides a descriptive label for the view or field.
@AbapCatalog.sqlViewName: Specifies the name of the underlying SQL view (not required with DEFINE VIEW ENTITY).
@AccessControl.authorizationCheck: Controls authorization behavior, e.g., #CHECK or #NOT_REQUIRED.
@VDM.viewType: Indicates the view type, such as #BASIC, #COMPOSITE, or #CONSUMPTION.
🛠️ Sample CDS Views with Associations
Here are examples of CDS views utilizing associations:
1. Booking with Customer Association:
@EndUserText.label: 'Booking with Customer Info'
define view entity ZI_BookingWithCustomer
as select from sbook
association [0..1] to scustom as _Customer
on sbook.customid = _Customer.id
{
key sbook.bookid,
sbook.customid,
_Customer.name as CustomerName
}
2. Flight with Airline Association:
@EndUserText.label: 'Flight with Airline Info'
define view entity ZI_FlightWithAirline
as select from sflight
association [0..1] to spfli as _Airline
on sflight.carrid = _Airline.carrid
{
key sflight.connid,
sflight.carrid,
_Airline.cityfrom
}
3. Customer with Booking Association:
@EndUserText.label: 'Customer with Booking Info'
define view entity ZI_CustomerWithBooking
as select from scustom
association [0..1] to sbook as _Booking
on scustom.id = _Booking.customid
{
key scustom.id,
scustom.name,
_Booking.bookid
}
4. Booking with Flight Association:
@EndUserText.label: 'Booking with Flight Info'
define view entity ZI_BookingWithFlight
as select from sbook
association [0..1] to sflight as _Flight
on sbook.connid = _Flight.connid
{
key sbook.bookid,
_Flight.carrid,
_Flight.price
}
5. Comprehensive Booking View:
@EndUserText.label: 'Comprehensive Booking View'
define view entity ZI_ComprehensiveBooking
as select from sbook
association [0..1] to scustom as _Customer
on sbook.customid = _Customer.id
association [0..1] to sflight as _Flight
on sbook.connid = _Flight.connid
{
key sbook.bookid,
_Customer.name as CustomerName,
_Flight.carrid,
_Flight.price
}
🔄 Sample CDS Views with Joins
Examples demonstrating the use of joins in CDS views:
1. Inner Join - Customer and Booking:
@EndUserText.label: 'Customer and Booking Inner Join'
define view entity ZI_CustomerBooking
as select from scustom as cust
inner join sbook as book
on cust.id = book.customid
{
key book.bookid,
cust.name,
book.connid
}
2. Left Outer Join - Flight and Booking:
@EndUserText.label: 'Flight and Booking Left Outer Join'
define view entity ZI_FlightBooking
as select from sflight as flight
left outer join sbook as book
on flight.connid = book.connid
{
key flight.connid,
flight.carrid,
book.bookid
}
3. Right Outer Join - Booking and Customer:
@EndUserText.label: 'Booking and Customer Right Outer Join'
define view entity ZI_BookingCustomer
as select from sbook as book
right outer join scustom as cust
on book.customid = cust.id
{
key cust.id,
cust.name,
book.bookid
}
4. Cross Join - Flight and Customer:
define view entity ZI_CrossJoinExample
as select from scarr
cross join spfli
{
key scarr.carrid,
scarr.carrname,
spfli.connid,
spfli.cityfrom,
spfli.cityto
}
SAP ABAP CDS Views – Predefined Functions
SAP ABAP Core Data Services (CDS) provides a rich set of predefined functions that can be used in CDS views for data transformation, logic control, and performance optimization. Below are the key categories of predefined functions, their descriptions, and real-world usage examples.
🔢 1. Mathematical Functions
🛠️ Example:
CEIL( vbak.netwr / 500 ) as RoundedBucket,
MOD( CAST( vbak.vbeln AS abap.int4 ), 100 ) as OrderSuffix
📅 2. Date & Time Functions
🛠️ Example:
DATS_DAYS_BETWEEN( vbak.erdat, CURRENT_DATE ) as DaysSinceOrder
🔤 3. String Functions
🛠️ Example:
CONCAT_WITH_SPACE( kna1.name1, kna1.name2, 1 ) as FullName,
REPLACE( kna1.name1, 'Ltd', 'Limited' ) as LegalNameFormatted
🔄 4. Conditional & Logical Functions
🛠️ Example:
CASE status WHEN 'A' THEN 'Active' ELSE 'Inactive' END as StatusText,
COALESCE( kna1.name2, 'Not Provided' ) as AlternateName
💲 5. Currency and Unit Conversion Functions
🛠️ Example:
CURRENCY_CONVERSION( amount => vbak.netwr,
source_currency => vbak.waerk,
target_currency => 'USD',
exchange_rate_type => 'M',
date => vbak.erdat ) as AmountInUSD
⚠️ Requires Analytical View with proper annotations
📊 6. Aggregate Functions
🛠️ Example:
SELECT vbeln, SUM(netwr) as TotalSales
FROM vbak
GROUP BY vbeln
Or
in CDS:
define view ZC_SalesAgg as
select from vbak
association [0..*] to vbap as _Items on _Items.vbeln = vbak.vbeln
{
key vbak.vbeln as SalesOrder,
SUM( _Items.netwr ) as TotalOrderValue
}
group by vbak.vbeln
Comments
Post a Comment