In SAP ABAP, CDS Views (Core Data Services Views) are a powerful tool for defining and managing database views that support both read and transactional operations. These views can be classified into several types depending on their purpose and functionality.

Here are the main types of CDS views in SAP ABAP:

1. Basic CDS View

A basic CDS view is the most fundamental type of CDS view. It is used to read data from a database table or another CDS view. It is typically read-only and does not allow direct manipulation of data (Create, Update, Delete).

Example:

@AbapCatalog.sqlViewName: 'ZFLIGHT'
@EndUserText.label: 'Flight Data'
define view ZFLIGHT as select from sflight {
key carrid,
key connid,
fldate,
price
}

2. Consume-Only CDS View

A consume-only CDS view is a type of CDS view that is intended solely for data consumption. These views do not perform any data modifications (like creating, updating, or deleting data). They are generally used for retrieving data for reporting or analytical purposes.

Example:

@AbapCatalog.sqlViewName: 'ZFLIGHT_CONSUME'
@EndUserText.label: 'Flight Data Consume-Only View'
define view ZFLIGHT_CONSUME as select from sflight {
key carrid,
key connid,
fldate,
price
}

3. Transactional CDS View

A transactional CDS view enables Create, Read, Update, and Delete (CRUD) operations on the underlying data. These views are configured to allow modification of data via annotations and are commonly used in applications built using the ABAP RESTful Application Programming (RAP) model.

Key Annotations for Transactional Views:

Example:

@AbapCatalog.sqlViewName: 'ZFLIGHT_TXN'
@EndUserText.label: 'Transactional Flight Data'
@ObjectModel.transactionalProcessingEnabled: true
@ObjectModel.createEnabled: true
@ObjectModel.updateEnabled: true
@ObjectModel.deleteEnabled: true
define view ZFLIGHT_TXN
as select from sflight {
key carrid,
key connid,
fldate,
price
}

4. Extended CDS View

An extended CDS view is an enhancement of an existing CDS view. This type of view allows you to add new fields to an already defined CDS view without changing the underlying data model. You can extend the base view with new fields, calculations, or transformations.

Example:

@AbapCatalog.sqlViewName: 'ZFLIGHT_EXT'
@EndUserText.label: 'Extended Flight Data'
define view ZFLIGHT_EXT
extend view ZFLIGHT as select from sflight {
price * 1.1 as price_with_tax
}

5. CDS View with Association

A CDS view with associations is a view that defines relationships between different tables or entities. It enables the joining of tables without performing an explicit SQL join, making the view more efficient and easier to maintain.

Example:

define view ZFLIGHT_ASSOC as select from sflight {
key carrid,
key connid,
fldate,
price,
association [0..1] to scarr as _carrier
on _carrier.carrid = sflight.carrid
}

In this example:

6. CDS View with Aggregation

A CDS view with aggregation allows you to perform grouping and aggregation operations such as SUM, AVG, COUNT, MIN, MAX, etc. It is useful when you need to aggregate data over groups of values.

Example:

define view ZFLIGHT_AGGREGATED as select from sflight {
key carrid,
count( connid ) as num_flights,
sum( price ) as total_revenue
}
group by carrid

7. CDS View with Parameters

A CDS view with parameters allows you to define dynamic filters and pass runtime parameters to control the data selection. These parameters allow you to filter data at runtime.

Example:

define view ZFLIGHT_PARAM as select from sflight {
key carrid,
key connid,
fldate,
price
}
where carrid = :p_carrid and fldate > :p_date

In this example:

8. CDS View with Calculation

A CDS view with calculation allows you to define computed fields directly in the view, such as calculations on existing fields. You can use arithmetic operations, string operations, and other functions to transform data.

Example:

define view ZFLIGHT_CALC as select from sflight {
key carrid,
key connid,
fldate,
price,
price * 1.2 as price_with_tax
}

9. CDS View with Local Currency

A CDS view with local currency allows you to define a currency conversion mechanism. It may involve converting the price or amount fields to the local currency.

Example:

define view ZFLIGHT_CURRENCY as select from sflight {
key carrid,
key connid,
fldate,
price,
price * currency_exchange_rate as price_in_local_currency
}

10. CDS View with Dynamic SQL

A CDS view with dynamic SQL allows you to generate dynamic queries based on the context or parameters at runtime. This is useful in scenarios where the structure of the query changes based on input values.


Summary of CDS View Types

These different types of CDS views provide a flexible and efficient way to model, consume, and manipulate data in SAP, enabling a broad range of use cases from simple reports to complex transactional applications.