Skip to content

ABAP Keyword Documentation →  ABAP - Dictionary →  ABAP CDS in ABAP Dictionary →  ABAP CDS - Data Definitions →  ABAP CDS - DDL for Data Definitions →  ABAP CDS - DEFINE VIEW →  ABAP CDS - SELECT →  ABAP CDS - SELECT, select_list 

ABAP CDS - SELECT, element

Other versions: 7.31 | 7.40 | 7.54

Syntax


... { [@element_annot1] 
      [@element_annot2]
      ...
        [KEY] { { association
                | element
                | path_expr.element [AS alias] }
              | { literal
                | parameter
                | session_variable
                | aggr_expr
                | arith_expr
                | builtin_func
                | case_expr
                | cast_expr AS alias }
      [@<element_annot1]
      [@<element_annot2]
      ... }
  | { $EXTENSION.* } }  ...

Alternatives

1. ... association|element|path_expr|literal|parameter|...

2. ... $EXTENSION.*

Effect

Defines an element of a SELECT list of a CDS view. The names of the elements in a SELECT list must be unique and must be in the same namespace as the parameters of the view.

Alternative 1

... association|element|path_expr|literal|parameter|...

Extras

1. ... @element_annot ... @<element_annot

2. ... KEY
3. ... AS alias

Effect

Specifies individual elements in the SELECT list of a CDS view.

  • When the name element is specified directly, an element of a data source data_source of the current CDS view is defined as an element. The field name can be prefixed with the name of the data source or its alternative name. The prefix is separated using a period (.). AS can be used to define an alternative element name alias.
  • path_expr.element specifies an element element of the target data source of the last CDS association of the path expression path_expr. The element defined in this way is part of the results set of the CDS view, a field of the CDS database view, and a component of a type defined in ABAP with respect to the CDS view. If no alternative element name alias is defined, and there is no name list, the element name is defined by the name of the element named by the path expression.
  • literal can be used to declare a literal. AS must be used to define an alternative element name alias. The literal can be prefixed with the name of a domain.
  • parameter can be used to specify a parameter from the parameter list parameter_list. If AS is used, an alternative element name alias must be defined, which cannot be the name pname of the parameter.
  • session_variable can be used to specify a session variable. AS must be used to define an alternative element name alias.
  • aggr_expr, arith_expr, builtin_func, and case_exp can be used to specify aggregate expressions, arithmetic expressions, calls of built-in functions in the database, and case distinctions. The expressions and functions are evaluated when the CDS view is accessed in the database system. If AS is used, an alternative element name alias has to be defined, unless there is a name list.

Elements with the type CHAR can have a maximum of 1333 characters. Elements with the type LRAW or LCHR must be at the end of the view. Only one such element is allowed per view. Furthermore, an element of this type must be prefixed directly with a field of the type INT2 or INT4 (for the maximum length of the field).


Example

SELECT list with all possible elements.

@AbapCatalog.sqlViewName: '...'
define view ...
  with parameters
    p_delay :abap.int4
  as select from
    spfli
    association to scarr as _scarr on
      _scarr.carrid = spfli.carrid
    {
      carrid,
      _scarr.carrname,
      '...'                                   as text,
      :p_delay                                as delay,
      $session.user                           as usr,
      count( * )                              as cnt,
      fltime + :p_delay                       as fltime_delayed,
      concat(concat('"',_scarr.carrname),'"') as carrname_quoted,
      case when fltime < 60  then 'short'
           when fltime > 300 then 'long'
           else 'medium'
      end                                     as duration,
      cast(connid as int4)                    as connid_number
    }
    group by
      carrid,
      connid,
      _scarr.carrname,
      fltime

Addition 1

... @element_annot ... @<element_annot

Effect

Specifies annotations for the element. The annotations can be specified before the element using @element_annot or after the element using @<element_annot.


Notes

  • An annotation can be used to assign further technical and semantic attributes to an element in ABAP Dictionary. Framework-specific annotations can be used to give the element specific semantic attributes for other SAP frameworks.

  • With respect to the element annotations, publications of a CDS association association are handled like any regular element. When an CDS association defined in a different CDS view is published, the annotations of preceding publications are inherited when annotations are evaluated using the class CL_DD_DDL_ANNOTATION_SERVICE.

Addition 2

... KEY

Effect

KEY is used to define the current element as the key element of the current CDS entity. Those elements of a SELECT list can be defined as key elements that are placed without gaps at the start of the list. The key elements of the CDS entity are evaluated as follows if they are used as a data source of a SELECT statement in ABAP SQL:

If a CDS role defined for a CDS entity is evaluated when the entity is accessed, its key elements must identify a unique row in the results set. If this is not the case, the internal instance of access control can produce duplicate entries or even incorrect results when aggregate functions are used. If a unique key cannot be determined for a CDS entity, no key element should be defined. A different instance of access control is then produced internally, which is less efficient.

By default, the key elements of the CDS entity are used to document the semantics of the data model. The addition KEY is then ignored when the CDS view is activated and when other accesses are performed in program executions.

The default setting can be overridden using the view annotation AbapCatalog.preserveKey:

  • If the value true is specified for this annotation, the key elements defined using KEY are also used as keys of the CDS database view.
  • If this annotation is not specified or specified with the value false, the key elements defined using KEY are ignored by the key of the CDS database view. Like in classic database views, the key fields of the CDS database view are derived implicitly from the key fields of the basis tables and the join conditions. If this is not possible, all fields of the database view are key fields.


Notes

  • In table buffering, the key fields of the CDS database view are always the decisive key fields.

  • In the syntax check in ABAP SQL, the key elements used depend on whether the name of the CDS entity is specified or the CDS database view (with the latter being obsolete).

Addition 3

... AS alias

Effect

Defines an alternative element name for the current element. The alternative element name replaces the actual name of the element from the data source data_source. The view field is created under the alternative element name in the CDS database view. Accordingly, the alternative element name must comply with the rules for names of view fields of database views, as well as the general naming rules for names: which means that it must also meet the

  • cannot be a reserved component name. The reserved names that cannot be used are in the database table TRESE.

This is only checked, however, if there is no explicit name list that overrides the alternative element names.

Alternative element names can be used in the current CDS view to grant unique names for identically named elements from different entities of the data source. When the current CDS view is accessed, the alternative element names must be used instead of the actual name. Alternative element names cannot be used within the CDS view, with one exception: alternative element names can be specified directly or after $projection in the ON condition of a CDS association.


Note

In particular, an alternative element name cannot be used as the operand of an expression.

Alternative 2

... $EXTENSION.*

Effect

Specifies all elements of an enhancement of the enhancement concept for classic objects in ABAP Dictionary. If specified, $EXTENSION. inserts (in this place) all fields of all customizing includes, append structures, and append views that exist in the current system for the database tables or classic views and which occur as a data source data_source in the current CDS view. $EXTENSION. is ignored for CDS views used as a data source and extended using EXTEND VIEW.

If specified, $EXTENSION.* is applied to the current CDS view only. It is not applied to other CDS views in whose data source the current CDS view is used or to CDS views in the data source of the current CDS view.

$EXTENSION.* cannot be specified if aggregate expressions aggr_expr occur in the current SELECT list or if the current CDS view is a union set created using UNION.


Notes

  • $EXTENSION.* is specified independently of when an enhancement is made. It is also applied when a database table or a view is enhanced only after the activation of the CDS view.

  • The statement EXTEND VIEW can be used to extend the current CDS view.

Example

The data source of the CDS view sales_order is an inner join of the database tables snwd_bpa and snwd_so and contains three directly defined elements sales_order_id, business_partner_id, and company_name and (because $EXTENSION.* is specified) all fields that exist due to enhancements in the database tables snwd_bpa and snwd_so. The alternative name partner is defined for the database snwd_bpa and is used in the ON condition. The names of the elements sales_order_id and business_partner_id are alternative element names. The element sales_order_id is defined as a key element.

@AbapCatalog.sqlViewName: 'SALES_ORDER_VW'
define view sales_order as
  select from snwd_bpa as partner
    inner join
      snwd_so on partner.node_key = snwd_so.buyer_guid
  { key so_id as sales_order_id,
        bp_id as business_partner_id,
        company_name, //from snwd_bpa
        $extension.* }


Example

The following view accesses the CDS view DEMO_CDS_ORIGINAL_VIEW, which is extended in the example for EXTEND VIEW. $EXTENSION.* is ignored, if specified.

@AbapCatalog.sqlViewName: 'DEMOCDSEXT1'
define view demo_cds_extension1
   as select from
    demo_cds_original_view
    
    {
      carrier,
      $extension.*
    }

The following view, on the other hand, accesses the CDS database view DEMO_CDS_ORIG of DEMO_CDS_ORIGINAL_VIEW. If specified, $EXTENSION.* includes both fields of the append view that extends the database view.

@AbapCatalog.sqlViewName: 'DEMOCDSEXT2'
define view demo_cds_extension2
   as select from
    demo_cds_orig  
    
    {
      carrier,
      $extension.*
    }

Continue

ABAP CDS - SELECT, element_annot

ABAP CDS - SELECT, association