$projection in SAP CDS view source code

We see in the association condition in the CDS view source code in the figure below $projection.

In SAP CDS (Core Data Services), $projectionit 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 SalesOrderdata 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 $projectionto create a new view, selecting only the required fields.

The following is an example, assuming we have a SalesOrderCDS 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 SalesOrdercreate a CDS view based on the above entity and select $projectionthe 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, $projectionthe keyword is used to select the CustomerNameand Productfields, and the original Quantityfield is preserved. AmountThe 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, $projectionis a special keyword that is used to refer to the current projection list. That is, $projectionall fields contained in the current CDS view definition are referenced.

For example, if you define some fields in a CDS view, such as id, , nameand 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 $projectionthe key Character.

Here is a $projectionsimple 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 ZMyViewcontains several fields such as CarrierId, ConnectionId, FlightDate, Price, Currency, Passengersand Distance. Then we define a calculated field TotalDistanceTravelledwhose value is the value of Distancethe field multiplied by the value of $projection.Passengers. Here, $projection.Passengersit is used to refer to Passengersthe field.

Then, in wherethe clause, we used $projection.TotalDistanceTravelledto refer to TotalDistanceTravelledthe field. Here $projection.TotalDistanceTravelled > 10000means to select only those TotalDistanceTravelledrecords whose field value is greater than 10000.

It should be noted that $projectionit can only be used in the calculation expression and conditional expression of the CDS view, and cannot selectbe used in the list of clauses. That is, you cannot write $projection.someField as SomeFieldcode like this.

Guess you like

Origin blog.csdn.net/i042416/article/details/132269432
Recommended