CDS Views in SAP ABAP

CDS View | CDS View Interview Questions | CDS View in SAP HANA | CDS View in SAP | Association in CDS View | Annotations in CDS View | ABAP on HANA|

Table of Contents

In SAP development, leveraging the power of Core Data Services (CDS) views is essential for creating robust data models and unlocking the full potential of your SAP HANA database. With the help of HANA Studio (Eclipse), developers can seamlessly design and deploy CDS views to meet their organization’s evolving business needs. In this comprehensive guide, we’ll delve into the intricacies of creating CDS views using HANA Studio (Eclipse), explore different CDS view types, dive into the features of ABAP CDS, and learn how to utilize templates effectively. 

There are two types of CDS views: 

    1.    HANA CDS Views
    2.    ABAP CDS Views
CDS View in SAP ABAP

What are the features of ABAP CDS?

ABAP CDS (Core Data Services) offers a variety of features that enhance data modeling capabilities within the ABAP programming language. Some key features of ABAP CDS include:

    • Declarative Data Modeling:

      ABAP CDS allows developers to define data models in a declarative manner using a syntax that closely resembles SQL (Structured Query Language). This simplifies the process of defining complex data structures and relationships within ABAP programs.
    • Data Definition Language (DDL):

      ABAP CDS provides a rich set of data definition language (DDL) statements for defining entities, attributes, associations, and other elements of a data model. Developers can use DDL statements to create and manipulate data structures within ABAP programs.
    • Annotations:

      Annotations allow developers to add metadata and additional information to data model elements, such as fields, entities, and associations. Annotations provide context and semantics to data model elements, enabling better understanding and interpretation of the data model.
    • Built-in Functions:

      ABAP CDS includes a variety of built-in functions for performing data manipulation and calculations within data models. These functions allow developers to perform common operations such as string manipulation, date calculations, and mathematical calculations directly within ABAP CDS views.
    • Associations:

      Associations enable developers to define relationships between entities within a data model. Associations allow for navigation between related entities and support features such as cardinality, aggregation, and composition.
    • Aggregation Functions:

      ABAP CDS supports aggregation functions such as SUM, COUNT, MIN, and MAX for performing aggregate calculations on data sets. These functions allow developers to summarize and analyze data within CDS views.
    • Joins and Union Operations:

      ABAP CDS supports join and union operations for combining data from multiple sources within a single data model. Joins allow developers to merge data sets based on common keys or conditions, while unions combine data sets vertically.
    • Data Validation:

      ABAP CDS includes features for defining data validation rules and constraints within data models. Developers can use validation expressions to enforce data integrity and ensure that data meets specified criteria.
    • View Hierarchies:

      ABAP CDS supports the definition of view hierarchies, allowing developers to organize and structure data models in a hierarchical manner. View hierarchies provide a logical organization of data model elements and support navigation between related views.
    • Text and Value Help Annotations:

      ABAP CDS supports annotations for defining text and value help for data model elements. Text annotations allow developers to specify descriptive text for data model elements, while value help annotations provide assistance and suggestions for input values during data entry.

Overall, ABAP CDS offers a robust set of features for data modeling and manipulation within the ABAP programming language, enabling developers to create sophisticated and flexible data models for ABAP applications.

How do I define the CDS view in SAP ABAP?

 

      1. Open ABAP Development Tools (ADT): Start by opening ABAP Development Tools in your SAP system. This can be done by navigating to the ABAP perspective in Eclipse or SAP GUI, depending on your preferred development environment.

      2. Create a New ABAP CDS View: Once in the ABAP Development Tools, navigate to the package where you want to create the ABAP CDS view. Right-click on the package and select New—Other ABAP Repository Object.

      3. Select ABAP CDS View: In the “New ABAP Repository Object” dialog, choose “Core Data Services” under the “Data Definition” category. Then, click “Next.”

      4. Provide CDS View Name: Enter a name for your ABAP CDS view in the “Name” field. This name should follow naming conventions and describe the purpose or content of the view.

      5. Define Data Source: In the next screen, you’ll define the data source for your CDS view. This can be a database table, another CDS view, or a combination of sources using join operations.

      6. Define View Structure: Define the structure of your CDS view by specifying the fields and their data types. You can also define associations, annotations, and other elements as needed.

      7. Add Annotations (Optional): Optionally, you can add annotations to your CDS view to provide additional metadata and context. Annotations can include descriptions, labels, and other information to enhance the usability and understandability of the view.

      8. Activate and Save: Once you’ve defined the structure of your ABAP CDS view, click the “Activate” button to activate the view. This will check for any syntax errors and save the view definition in the ABAP repository.

      9. Test and Validate: After activating the CDS view, you can test and validate it to ensure that it behaves as expected. You can use tools such as the ABAP CDS Test Tool or execute the view in a test program to verify its functionality.

      10. Deploy and Use: Once the CDS view is tested and validated, you can deploy it for use in your ABAP applications. You can consume the CDS view in ABAP programs, ABAP Managed Database Procedures (AMDP), SAP Fiori applications, and other SAP development artifacts.

By following these steps, you can define an ABAP CDS view in SAP and leverage its powerful capabilities for data modeling and manipulation in your ABAP applications.

Some of the Common types of Views in ABAP CDS 

      1. Define View-CDS View in SAP ABAP:

        • A Define view is the simplest type of view in ABAP CDS.
        • It defines a projection of data from one or more underlying database tables.
        • Define views are used to select specific fields from tables and present them as a single virtual table.
        • These views do not involve any joins or additional processing; they simply select and project data from the underlying tables. 
        • Data Source can be a database table or existing CDS.
      2. Define View with Join:

        • A Define view with join combines data from multiple tables or Data Source using left outer join operations.
        • This type of view allows developers to join two or more tables based on common fields or conditions.
        • Joins conditions are specified in the ON Clause.
        • Define views with join are useful for querying data that is distributed across multiple tables and needs to be combined for analysis or reporting purposes.
        • Joins can be performed using different types of join operations such as inner join, left outer join, right outer join, and full outer join.
      3. Define View with Parameters:

        • A Define view with parameters allows developers to pass parameters or selection criteria to the view at runtime.
        • Parameters can be used to filter data, restrict the number of records returned, or customize the behavior of the view.
        • Define views with parameters enhance flexibility and reusability by allowing developers to create dynamic views that can adapt to different runtime scenarios.
      4. Define View with Aggregation:

        • A Define view with aggregation enables developers to perform aggregate calculations on data within the view.
        • This type of view allows for the calculation of aggregate functions such as SUM, COUNT, AVG, MIN, and MAX on selected fields.
        • Define views with aggregation are commonly used for generating summary reports, calculating key performance indicators (KPIs), and performing statistical analysis on data.
      5. Define View with Associations:

        • A Define view with associations defines relationships between entities within the view.
        • You can use association in the projection list and other data definitions that use other CDS view as data source.
        • Associations allow developers to navigate between related entities and access related data without explicitly joining tables.
        • This type of view supports cardinality, which specifies the number of related records that can exist for each record in the primary entity.
        • Define views with associations facilitate data modeling and querying in scenarios involving complex relationships between entities.
      6. Define View with Annotations:

        • A Define view with annotations includes annotations that provide additional metadata and context to the view.
        • Annotations enhance the understandability and usability of the view by providing descriptive information, labels, and semantic annotations.
        • This type of view is useful for documenting the purpose, usage, and characteristics of the view, making it easier for developers to understand and interact with the view.
      7. Define View with Hierarchies:

        • A Define view with hierarchies allows developers to define hierarchical relationships between entities within the view.
        • Hierarchies represent parent-child relationships where each record is linked to one or more parent records and can have one or more child records.
        • This type of view supports hierarchical querying and navigation, enabling developers to traverse the hierarchy and analyze data at different levels of granularity.
      8. Define View with Union:

        • A Define view with union combines data from multiple source views or tables using union operations.
        • Union operations merge data sets vertically, combining rows from different sources into a single result set.
        • Define views with union are useful for combining similar data sets from different sources or tables into a unified view for analysis or reporting purposes.
      9. Define View with Annotations for Analytics:

        • A Define view with annotations for analytics includes annotations specifically designed for analytical purposes.
        • These annotations provide additional information about the data model and its usage in analytical scenarios, such as defining measures, dimensions, and key figures.
        • Define views with annotations for analytics enable developers to create data models optimized for analytical reporting, dashboarding, and data visualization.
      10. Define View with Text Annotations:

        • A Define view with text annotations includes annotations for providing descriptive text and labels for data model elements.
        • Text annotations enhance the understandability and usability of the view by providing human-readable descriptions and explanations.
        • This type of view is useful for documenting the purpose, usage, and characteristics of the view, making it easier for developers and end-users to interpret and interact with the data model.
      11. Define View with Value Help Annotations:

        • A Define view with value help annotations includes annotations for providing value help and suggestions during data entry.
        • Value help annotations enhance the user experience by offering assistance and suggestions for input values based on predefined lists or ranges.
        • This type of view is useful for guiding users through the data entry process and ensuring data integrity by enforcing valid input values.
      12. Define View with Input Parameters:

        • A Define view with input parameters allows developers to define parameters that can be passed to the view at runtime.
        • Input parameters enable dynamic filtering and customization of the view’s output based on user input or system conditions.
        • This type of view enhances flexibility and reusability by enabling developers to create generic views that can adapt to different runtime scenarios.
      13. Define View with Authorization Checks:

        • A Define view with authorization checks includes authorization checks to enforce data access restrictions based on user roles and permissions.
        • Authorization checks ensure that users can only access data that they are authorized to view, protecting sensitive information and ensuring compliance with security policies.
        • This type of view enhances data security and governance by implementing fine-grained access controls at the data model level.
      14. Define View with Table Functions:

        • A Define view with table functions allows developers to define user-defined functions that return tabular data.
        • Table functions can be used within CDS views to perform complex data processing and transformations, enabling developers to encapsulate business logic and calculations.
        • This type of view enhances the expressiveness and functionality of CDS views by providing a mechanism for reusable data processing logic.
      15. Define View with Calculated Fields:

        • A Define view with calculated fields includes fields that are derived from calculations or expressions based on other fields in the view.
        • Calculated fields enable developers to perform calculations, aggregations, or data manipulations within the view definition, without the need for additional processing in application logic.
        • This type of view simplifies data modeling and analysis by allowing developers to define complex calculations directly within the view.
      16. Define View with Time-dependent Data:

        • A Define view with time-dependent data handles temporal aspects of data by incorporating time-dependent validity or change data capture (CDC) mechanisms.
        • Time-dependent data views enable developers to model historical data, track changes over time, and perform temporal queries to analyze data at different points in time.
        • This type of view is essential for scenarios involving historical analysis, trend analysis, and auditing of data changes over time.
      17. Define View with Virtual Elements:

        • A Define view with virtual elements includes virtual fields or elements that are not directly stored in the underlying database tables but are computed or derived at runtime.
        • Virtual elements enable developers to extend the data model with additional calculated or derived attributes, without modifying the underlying data schema.
        • This type of view enhances the flexibility and extensibility of the data model, allowing developers to incorporate additional information or logic as needed.

Structure Definition in ABAP Eclipse 

    1. Selecting All Columns in Core Data Services (CDS):
    • @AbapCatalog.sqlViewName: ‘ZSANDEEP’

      @AbapCatalog.compiler.compareFilter: true

      @AbapCatalog.preserveKey: true

      @AccessControl.authorizationCheck: #NOT_REQUIRED

      @EndUserText.label: ‘Sandeep Test’

      define view ZSANDEEPTEST as select from * from sflight

      {

      2. Selecting a Specific Column:

    1.  define view ZSANDEEPTEST as select carrid, connid, fldate, price from sflight

 {

3. Selecting fields after from claus:

define view ZSANDEEPTEST as select from from sflight

{

Carrid as Airline Number,

Connid as Airline Code,

Fldate as Flight Date,

Price as Flight Price

}

        • CTRL + S = SAVE
        • CTRL + F3 = Activate
        • Note: Once you activate CDS view a view in ABAP Dictionary will be created.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top