In SAP ABAP CDS Views, a transaction can be defined in a CDS view to represent business logic for data manipulation (Create, Update, Delete). A transaction-based CDS view allows the execution of changes to the database table using ABAP code or custom transaction logic in a more structured manner.
While CDS views themselves are primarily designed for read-only access to database tables, transactional CDS views are a feature that extends their capabilities to allow updates and changes.
Transactional CDS Views (For Create, Update, Delete Operations)
A transactional CDS view is a special type of view that allows for insert, update, and delete operations. These operations are typically bound to a specific business object or entity and help to simplify data manipulation in applications that use ABAP RESTful Programming model (RAP) or Fiori apps.
Annotations for Transactional CDS Views
To define transactional behaviors in CDS views, you use the @ObjectModel annotation to indicate that a CDS view can be used for CRUD (Create, Read, Update, Delete) operations.
Basic Example of a Transactional CDS View
@AbapCatalog.sqlViewName: 'ZFLIGHT_TXN'@EndUserText.label: 'Transactional View for Flight Data'@ObjectModel.transactionalProcessingEnabled: truedefine view ZFLIGHT_TXN with parameters p_carrid: s_carrid as select from sflight { key carrid, key connid, fldate, price}where carrid = :p_carrid
Key Annotations Used for Transactional CDS Views:
- @ObjectModel.transactionalProcessingEnabled:
- This annotation enables transactional processing (CRUD operations) for the CDS view.
- This allows the CDS view to interact with create, update, and delete operations.
- @ObjectModel.writeActive:
- Specifies whether the CDS view is writable (i.e., supports update, insert, or delete operations).
- @ObjectModel.createEnabled:
- Enables the creation of new records through the CDS view.
- @ObjectModel.updateEnabled:
- Allows for the modification of existing records through the CDS view.
- @ObjectModel.deleteEnabled:
- Permits the deletion of records from the CDS view.
Example of a Transactional CDS View with Create and Update Capabilities
@AbapCatalog.sqlViewName: 'ZFLIGHT_TXN_CREATE_UPDATE'@EndUserText.label: 'Transactional CDS View for Flight Data with Create and Update'@ObjectModel.transactionalProcessingEnabled: true@ObjectModel.writeActive: true@ObjectModel.createEnabled: true@ObjectModel.updateEnabled: truedefine view ZFLIGHT_TXN_CREATE_UPDATE with parameters p_carrid: s_carrid as select from sflight { key carrid, key connid, fldate, price}where carrid = :p_carrid
Explanation:
- The CDS view is transactional, meaning it can be used for CRUD operations.
- The
@ObjectModel.createEnabled
and@ObjectModel.updateEnabled
annotations enable the creation and updating of flight data records via this CDS view. - The
@ObjectModel.writeActive
annotation ensures that the view supports writing data.
Transactional Operations:
- Create Operation:
You can create new records in the database table that the CDS view references. This is possible if the view is configured with
@ObjectModel.createEnabled: true
.
Example:
- In an ABAP program, you can use the INSERT statement to add records based on the data exposed by the CDS view.
insert into ZFLIGHT_TXN_CREATE_UPDATE (carrid, connid, fldate, price)values ('LH', '1001', '2025-01-01', 500).
- Update Operation:
If the view supports updates (
@ObjectModel.updateEnabled
), you can modify the records based on the CDS view.
Example:
- In ABAP, you can use the UPDATE statement to modify existing data.
update ZFLIGHT_TXN_CREATE_UPDATEset price = 600where carrid = 'LH' and connid = '1001'.
- Delete Operation:
The DELETE operation can be executed if the view supports deletions via the annotation
@ObjectModel.deleteEnabled
.
Example:
- In ABAP, use the DELETE statement to remove records.
delete from ZFLIGHT_TXN_CREATE_UPDATEwhere carrid = 'LH' and connid = '1001'.
Transactional Views in the ABAP RESTful Application Programming Model (RAP):
The ABAP RESTful Application Programming Model (RAP) makes use of transactional CDS views to enable data changes via the Fiori apps or UI5 applications. RAP allows developers to build applications that leverage CDS views for transactional data manipulation.
Conclusion:
- Transactional CDS views enable Create, Read, Update, and Delete operations in a standardized and flexible manner.
- These views can be used for CRUD operations, making them an essential part of ABAP development for data manipulation.
- You must use annotations like
@ObjectModel.transactionalProcessingEnabled
,@ObjectModel.createEnabled
,@ObjectModel.updateEnabled
, and@ObjectModel.deleteEnabled
to enable these operations in a CDS view.