Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
mickaelquesnot
Active Participant
0 Likes
4,159

Defining a basic Core Data Services (CDS) view in SAP involves creating a data model on the database layer rather than the application layer. This offers significant advantages in terms of performance, data access, and reusability. Here's a breakdown of how to define a basic CDS view:

1. Development Environment:

You'll typically use the ABAP Development Tools (ADT) in Eclipse to create CDS views. This provides a modern and integrated development environment.

2. CDS View Syntax:

The basic syntax for defining a CDS view is as follows:

SQL
 
@AbapCatalog.sqlViewName: 'SQL_VIEW_NAME'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
define view VIEW_NAME
  as select from DATABASE_TABLE or VIEW
{
  element1 as alias1,
  element2 as alias2,
  ...
  elementN as aliasN
};

Let's break down each part:

  • @AbapCatalog.sqlViewName: 'SQL_VIEW_NAME': This annotation is mandatory. It defines the name of the corresponding SQL view that will be created in the database. This is the name you'll use when accessing the view from standard SQL.
  • @AbapCatalog.compiler.compareFilter: true: This annotation is recommended. It ensures that filter conditions are pushed down to the database level for better performance.
  • @AccessControl.authorizationCheck: #CHECK: This annotation is crucial for security. It enforces authorization checks based on the defined authorization objects.
  • define view VIEW_NAME: This keyword starts the definition of the CDS view, followed by the name you want to give to the CDS entity. This is the name used within ABAP programs.
  • as select from DATABASE_TABLE or VIEW: This specifies the data source for the view. You can select from database tables, database views, or other CDS views.
  • { element1 as alias1, ... }: This is the element list. It defines the fields you want to include in the view. You can rename fields using aliases (as alias).

3. Example:

Let's say you have a database table SCARR (Carrier) and you want to create a CDS view that includes the carrier ID and name:

SQL
 
@AbapCatalog.sqlViewName: 'Z_CARRIER_VIEW'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
define view ZCarrierView
  as select from SCARR
{
  carrid as CarrierID,
  carrname as CarrierName
};

In this example:

  • Z_CARRIER_VIEW is the name of the SQL view.
  • ZCarrierView is the name of the CDS view.
  • carrid is renamed to CarrierID.
  • carrname is renamed to CarrierName.

4. Key Concepts and Enhancements:

  • Aliases: Using aliases makes your code more readable and can be essential when joining tables with fields that have the same name.
  • Data Types: The data types of the elements in the view are derived from the underlying database table or view.
  • Expressions: You can use expressions in the element list to create calculated fields. For example:
SQL
 
define view ZSalesOrderValue
  as select from VBAK
{
  vbeln as SalesOrder,
  netwr as NetValue,
  waerk as Currency,
  netwr * 1.19 as GrossValue // Calculated field
};
  • Joins: You can join multiple tables or views in the FROM clause.
SQL
 
define view ZCustomerOrders
  as select from KNA1 as Customer
    inner join VBAK as SalesOrder
      on Customer.kunnr = SalesOrder.kunnr
{
  Customer.kunnr as CustomerID,
  Customer.name1 as CustomerName,
  SalesOrder.vbeln as SalesOrderNumber
};
  • WHERE Clause: You can use a WHERE clause to filter the data.
SQL
 
define view ZActiveCustomers
  as select from KNA1
{
  kunnr as CustomerID,
  name1 as CustomerName
} where kunnr like '1000%'; // Filter customers starting with 1000
  • Associations: You can define associations between CDS views to navigate related data. This is a more advanced topic but very powerful for building complex data models.

5. Activation and Usage:

After defining the CDS view in ADT, you need to activate it. This creates the corresponding SQL view in the database. You can then use the CDS view in ABAP programs using Open SQL statements:

ABAP
 
SELECT *
  FROM ZCarrierView
  INTO TABLE @DATA(lt_carriers).

You can also access the underlying SQL view directly using native SQL if needed.

Benefits of CDS Views:

  • Performance: Data processing is pushed down to the database level, which is much faster than processing data in the application server.
  • Simplified Data Access: CDS views provide a simplified and consistent way to access data.
  • Reusability: CDS views can be reused in multiple applications.
  • Extensibility: You can extend standard SAP data models using CDS views without modifying the original objects.

By understanding these basic concepts, you can start building efficient and powerful data models using CDS views in your SAP development.

 

https://www.linkedin.com/posts/mickaelquesnot_gusap-s4-hanadefining-a-basic-cds-viewdocx-activity-72...

1 Comment