View Definitions in ABAP Core Data Services (ABAP CDS)
ABAP Core Data Services, or ABAP CDS for short, is an infrastructure for defining and consuming semantically rich data models in ABAP. They use a CDS DDL to define CDS entities that implement a semantic data model. The most important CDS modeling entities are CDS View entities.
![](/service/media/topic/a73b8342-8269-4e4e-a40d-ab81b7f5e106/S4D400_24_en-US_media/S4D400_24_en-US_images/14_-_Analyze_CDS_001.png)
The CDS View definition contains re-usable SQL logic; sometimes as simple as a projection of table fields and sometimes more sophisticated with calculations, aggregations, joins, unions, and so on.
A CDS View definition can contain associations to reflect the relations of the data model. Consumers of the view can use the associations to retrieve related data.
Finally, annotations are used to semantically enrich the view definition. This metadata is evaluated by frameworks that build on top of CDS View definitions. One such framework is the ABAP RESTful application programming model , for which we will discuss an example later in this course.
Let's watch examples of CDS view definitions.
![](/service/media/topic/a73b8342-8269-4e4e-a40d-ab81b7f5e106/S4D400_24_en-US_media/S4D400_24_en-US_images/14_-_Analyze_CDS_002.png)
CDS View definitions are contained in repository objects of type Data Definition. Let us now have a look at the source code of data definition /DMO/I_CONNECTION.
The main part is the DEFINE VIEW ENTITY statement. It contains the name of the CDS View entity and, after keyword FROM, the data source. In our example, the name of the view entity is /DMO/I_Connection and the data source is database table /dmo/connection. Optional addition AS, defines an alias name Connection to address the data source inside the View definition.
Note
The source of a CDS View entity could also be another CDS View.A pair of curly brackets contains the list of view elements. In our example, the view elements are fields of database table /dmo/connection. Keyword Key in front of the first two elements defines them as key fields of the CDS View entity. Optional addition AS defines an alias name for each view element.
![](/service/media/topic/a73b8342-8269-4e4e-a40d-ab81b7f5e106/S4D400_24_en-US_media/S4D400_24_en-US_images/14_-_Analyze_CDS_003.png)
Addition association defines a relation to another CDS view entity. In our example, the related is CDS view entity /DMO/I_Carrier and the name of the association is _Airline.
This association becomes available for consumers of the view by adding it to the element list. This is referred to as exposing the association.
![](/service/media/topic/a73b8342-8269-4e4e-a40d-ab81b7f5e106/S4D400_24_en-US_media/S4D400_24_en-US_images/14_-_Analyze_CDS_004.png)
Annotations start with the at sign (@) and they are used to semantically enrich the view definition for consumers. Annotations before the view definition are called entity annotations. Entity annotations are used to define metadata for the view entity as a whole. Annotations between the curly brackets are called element annotations. Element annotations are used to define metadata for the different elements of the view.
![](/service/media/topic/a73b8342-8269-4e4e-a40d-ab81b7f5e106/S4D400_24_en-US_media/S4D400_24_en-US_images/14_-_Analyze_CDS_005.png)
You already learned how to use the Data Preview tool to display and analyze the content of a database table. This tool is also available for CDS view entities.
To open the Data Preview for a given CDS entity, right-click anywhere in the data definition and choose Open With > Data Preview. Alternatively, place the cursor anywhere in the database table definition and press Ctrl + F8.
The tool displays the data returned by the CDS entity. The same functions are available to sort or filter the data and adjust the display.
If the view definition contains one or more associations, you can use them to display related data. To do so, proceed in the following manner:
- Right-click on a row in the display.
- From the context menu, choose Follow Association.
- From the list of available associations, choose the one in which you are interested.
![](/service/media/topic/a73b8342-8269-4e4e-a40d-ab81b7f5e106/S4D400_24_en-US_media/S4D400_24_en-US_images/14_-_Analyze_CDS_006.png)
If you want to find all CDS Views with a certain database table as a source, you can utilize the Where-used List tool of ADT. To use this tool, proceed as follows:
- Open the definition of the database table.
- Right-click anywhere in the source code and choose Get Where-used List from the context menu. Alternatively, you can press Ctrl + Shift + G, or choose the button from the toolbar with the same symbol.
- The Search view displays a list of all development objects that directly use the database table.
![](/service/media/topic/a73b8342-8269-4e4e-a40d-ab81b7f5e106/S4D400_24_en-US_media/S4D400_24_en-US_images/14_-_Analyze_CDS_007.png)
You can apply filters to the Where-used List if, for example, you are only interested in objects from certain packages or objects of the specific object type. The example illustrates how to filter for CDS views that use the table.