Search code examples
abaphanacds

Selecting from a CDS entity versus from a CDS Database view


In SAP's ABAP CDS, the define view statement is used to create a CDS view. This object has a CDS entity name, which is how it appears in the editor and packages but it also has an AbapCatalog.sqlViewName annotation that has to be different. The documentation of the define view syntax has some information:

Two objects are created for a CDS view (of the ABAP CDS) that is defined using DEFINE VIEW. A name must be specified for each of the two objects:

  • The name CDS_DB_VIEW of the CDS database view must be specified in quotation marks after the annotation @AbapCatalog.sqlViewName. The usual rules for ABAP Dictionary views apply to this name and it is not case-sensitive (it is transformed internally into uppercase letters). The associated SQL view is created under this name on the database.

  • The name cds_entity of the CDS entity is defined after the keywords DEFINE VIEW (DEFINE is optional). No quotation marks need to be specified. This name follows the rules of the CDS database view, but it can be 30 places in length.

Both names are in the namespace of the data types in ABAP Dictionary and must each be unique. The two names can be used in other DDL statements or in ABAP programs to access the CDS view.

It mentions some other differences such as the entity not being available in SAP's Data Dictionary tools (SE11, SE16) and the fact that selects from the entity always run in strict mode but the kicker is:

In the DDL of the CDS and in ABAP, a CDS view can be addressed as a CDS database view CDS_DB_VIEW and as a CDS entity cds_entity. It is strongly recommended, however, that only the CDS entity is used.

When I run both in a simple query, I notice that when selecting from the entity there is no MANDT column but this column is present when selecting from the CDS database view.

So, what are the real differences between the database view and the CDS entity that developers should keep in mind when working with CDS views?


Solution

  • You should no longer query the ABAP CDS database view and restrict your queries to use the ABAP CDS entity. Although querying the database view is possible in NetWeaver 7.40 it is deprecated in higher releases. So don't worry about the database View :-)

    See the documentation for :Obsolete Access to CDS Views in Open SQL:

    To keep the behavior consistent and to use only the defined properties of a CDS view, only the name of the CDS entity should be used to access a CDS view.

    Direct access to a CDS database view is forbidden in strict mode from Release 7.50.

    Concerning the client handling you should accordingly focus on the client behavior of the CDS entity. The different options are described ABAP CDS Client Handling.