HANA CDS Views In Simple Words- SAP ABAP ON HANA.
SAP HANA supports SQL as a means to define, read, and manipulate data. On top of SQL, many technologies introduce higher-level models to add semantics and ease consumption, such as OData EDM models, the semantic layer in the BI platform, JPA and enterprise objects in Java, and the business objects frameworks in ABAP. These higher-level models share many commonalities, but are usually specific to their technology. This prevents their re-use across stacks and increases effort for application developers. To address this, SAP introduces a set of domain-specific languages and services, called CDS, for defining and consuming semantically rich data models.
The Core data Services (CDS) views are collection of below three domain specific languages and services for consuming semantically rich data models.
- Data Definition Language (DDL): The DDL part of CDS can be considered an enhancement of SQL that allows you to define semantically rich database tables and views, often referred to as CDS entities, and user-defined types in the database. This allows you to simplify and harmonize the way you define and consume your data models. The enhancements include annotations to enrich the data models with additional (domain-specific) metadata and associations on a conceptual level, replacing joins with simple path expressions in queries.
- Query Language (QL): Views defined using the CDS DDL can be consumed in ABAP programs using the Open SQL query language, as well as in other CDS views. Open SQL has been extended to provide support for CDS entities. In addition, it is possible to query the CDS features supported by the underlying database before executing the Open SQL statement to enable alternative implementations.
- Data Control Language (DCL): The DCL is used to define authorizations for CDS entities in a declarative way. The ABAP CDS authorization concept is a new concept and can be used independently or in combination with the classic concept. Whereas the classic authorization concept is based on authorization objects and the authorizations are checked either implicitly (when transactions are called) or explicitly (using the statement AUTHORITY CHECK), the CDS authorization concept is based on implicit checks automatically performed by the ABAP runtime when the CDS entity is accessed using Open SQL.
CDS are not only an integral part of SAP HANA, but can also be leveraged in the ABAP stack and are optimally integrated into the ABAP Dictionary and language. The corresponding new ABAP Repository object types DDL Source and DCL Source, for defining enhanced view entities in ABAP, allow you to push more data-intensive calculations to the database than is possible using classical ABAP Dictionary views.
You can use ABAP CDS views to read data and calculate additional information while reading data. ABAP CDS views do not support data modifications as of now.
Differences Between ABAP Dictionary Views & CDS Views:
ABAP CDS share with ABAP Dictionary views that are supported on all database systems and supported by the ABAP stack. However, ABAP CDS views provide the following additional capabilities:
- Enhanced support for combining queries, such as UNION and OUTER joins.
- Support for calculations that use expressions in the column list and for selection, as well as aggregation and grouping in the data model.
- Ability to be nested, which allows for more design for reuse.
How To Create CDS Views:
Follow the below mentioned steps for creating a CDS view:
- Use the ABAP Development Tools. The ABAP Workbench does not support the new type of ABAP repository objects called DDL Sources.
- In the ABAP Project tree, select the package to contain the CDS view and choose context menu entry New → Other ABAP Repository Object → Dictionary → DDL Source.
- Use the DDL statement DEFINE VIEW and SQL-like syntax to define the view.
The CDS view entities are different from these associated representations on the database layer, that is, the corresponding SQL views, and in the ABAP Dictionary. These representations are generated, updated, and deleted automatically when activating the DDL source.
Examples:
CDS view as projection and using column name as alias to rename a column.
Alternative syntax for: Column list in curly brackets {..} after FROM clause.
Annotations allow for the specification of ABAP- specific additional information, such as how records of the view should be buffered by the ABAP table buffer, or whether the view is client dependent or not.
By default , CDS views are considered as client-dependent, and you don’t have to include client fields explicitly in the field list or in the join conditions.
Annotations enriches the CDS models with additional metadata and they begin with the escape character @.
The annotation @AbapCatalog.sqlViewName is mandatory, and specifies the name of the representation of the DDL source in the ABAP dictionary and the database.
Example with annotations:
Different Operations That Can Be Performed In The CDS Views are mentioned below:
Like new open SQL, CDS supports various expressions, such as the following:
- The simplest form of expression is numerical or string literal.
- Arithmetic expressions using operators +, -, *, / or the unary -, or using built-in functions such as CELL and MOD.
- String expressions, including the string concatenation operator &&, or built-in functions, such as LPAD, CONCAT, REPLACE, and SUBSTRING.
- Special built-in functions can be used: COALESCE to replace null values in results, CURRENCY_CONVERSION and UNIT_CONVERSION to convert between currencies, respectively units.
- Some built-in functions and the division operator are available since ABAP 7.4 SP08 only.
- CAST expressions, with which the data type of values can be converted.
- CASE expressions, which can be nested. An alias name is required for the resulting column, except for nested CASE. The resulting column type is derived from expressions after the THEN and ELSE clause.
Like Open SQL, CDS supports the following functions also:
- A WHERE clause in view definition.
- INNER joins, LEFT OUTER and RIGHT OUTER joins. The join condition can be specified after the projection list when using the Open SQL-like syntax, or before the projection list when using the curly bracket syntax. If joins are used, no asterisk is supported on SELECT statements.
- The calculation of aggregations, grouping records, and filtering groups. The GROUP BY and HAVING clauses may be familiar from Open SQL or standard SQL aggregations.
- Using the UNION and UNION ALL keywords, you can define views that merge the results of two or more queries. This may be known from standard SQL. Similar to standard SQL, the following occurs:
- Using keyword UNION, the combined result contains no duplicate rows, that is, UNION implies DISTINCT.
- Using keyword UNION ALL, the results are basically combined immediately, and duplicate rows are not removed.
- Matching columns must be in the same order and of compatible type.
- This is not supported in new Open SQL yet.
ABAP CDS View on Database
Each active ABAP CDS view has a corresponding ABAP Dictionary view and database views. These corresponding objects are managed automatically by the ABAP system when activating the DDL source. One way to check them is as follows:
- In the DDL source, place the cursor on the value of the AbapCatalog.sqlViewName annotation, press F3 or Control to turn the value into a link, and click the link. This opens the ABAP Dictionary view.
- In the menu of the ABAP Dictionary screen, choose Extras → CREATE statement to see the SQL statement used by the system to create the corresponding database view.
- You can also open the ABAP Dictionary view directly using transaction code SE11 or in the subfolder Dictionary / Views of the ABAP package in the ABAP Development Tools for Eclipse or the ABAP Workbench.
How To Select Data Using CDS Views?
We can select data from CDS view using an Open SQL SELECT statement.
You can use the ABAP Dictionary name, respectively the SQL view name, of the CDS view in the SELECT statement FROM clause. When using the new Open SQL syntax, you can, and are recommended to, use the CDS view name, as specified using the DEFINE VIEW keywords in the DDL source file.
The CDS view cannot have the same name as the SQL view / ABAP Dictionary view and the CDS view name also does not have to equal the name of the ABAP DDL source.
CDS views are client-dependent by default, even if no client field is included in the view definition.
We do not recommend including client fields in the definition of CDS views.
To retrieve data from a client-dependent CDS view, but from a different client, use the new Open SQL syntax USING CLIENT.