We see in the association condition in the CDS view source code in the figure below $projection
.
In SAP CDS (Core Data Services), $projection
it is a special keyword used to select specific fields in a query and create a temporary view. It can be used in the source code of a CDS view to perform processing and transformations on result sets. Using it $projection
, data can be filtered and transformed as needed, projecting a source dataset into a new, more specific view of the data.
Suppose there is a simple business scenario involving a SalesOrder
data table named , which contains sales order information, including order number, customer name, product, quantity, and amount. We can create a basic CDS view, then use $projection
to create a new view, selecting only the required fields.
The following is an example, assuming we have a SalesOrder
CDS data table called :
@AbapCatalog.sqlViewName: 'ZSALESORDER'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales Order'
define entity SalesOrder {
key OrderID : UUID;
CustomerName : String(50);
Product : String(50);
Quantity : Integer;
Amount : Decimal(15, 2);
}
We can now SalesOrder
create a CDS view based on the above entity and select $projection
the required fields using the as follows:
@AbapCatalog.sqlViewName: 'ZSalesOrderView'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales Order View'
define view SalesOrderView as select from SalesOrder {
key OrderID,
$projection.CustomerName,
$projection.Product,
Quantity,
$projection.Amount
}
In the example above, $projection
the keyword is used to select the CustomerName
and Product
fields, and the original Quantity
field is preserved. Amount
The field is also selected into the new view, but is not treated specially, so it remains unchanged.
By using $projection
, we can create a new CDS view containing only the fields we are interested in without having to redefine all the fields and logic. This helps simplify queries and data processing, while also improving the performance of queries because only required fields are selected.
Let's look at another example.
In SAP's ABAP programming environment, the CDS (Core Data Services) view is a powerful tool that allows developers to operate the database in a more efficient and concise manner. In the source code of a CDS view, $projection
is a special keyword that is used to refer to the current projection list. That is, $projection
all fields contained in the current CDS view definition are referenced.
For example, if you define some fields in a CDS view, such as id
, , name
and status
, and then you need to refer to these fields in a certain part of the view (such as a conditional expression or a calculation expression), then you can use $projection
the key Character.
Here is a $projection
simple example using :
@AbapCatalog.sqlViewName: 'Z_MY_VIEW'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'My View'
define view ZMyView as select from sflight as flight {
key flight.carrid as CarrierId,
flight.connid as ConnectionId,
flight.fldate as FlightDate,
flight.price as Price,
flight.currency as Currency,
flight.passengers as Passengers,
flight.distance as Distance,
flight.distance * $projection.Passengers as TotalDistanceTravelled
} where $projection.TotalDistanceTravelled > 10000
In the above example, the CDS view ZMyView
contains several fields such as CarrierId
, ConnectionId
, FlightDate
, Price
, Currency
, Passengers
and Distance
. Then we define a calculated field TotalDistanceTravelled
whose value is the value of Distance
the field multiplied by the value of $projection.Passengers
. Here, $projection.Passengers
it is used to refer to Passengers
the field.
Then, in where
the clause, we used $projection.TotalDistanceTravelled
to refer to TotalDistanceTravelled
the field. Here $projection.TotalDistanceTravelled > 10000
means to select only those TotalDistanceTravelled
records whose field value is greater than 10000.
It should be noted that $projection
it can only be used in the calculation expression and conditional expression of the CDS view, and cannot select
be used in the list of clauses. That is, you cannot write $projection.someField as SomeField
code like this.