ABAP CDS - Syntax
The syntax of the DDL and of the DCL of the ABAP CDS comprises elements of the general DDL and DCL of SQL. In addition, the syntax makes it possible to define annotations and associations. The syntax and semantics basically match the general CDS concept.
➤ General Syntax Rules
The general syntax rules for the DDL and DCL for defining CDS objects in ABAP CDS are as follows:
- Keywords
- Keywords must be all uppercase, all lowercase, or in lowercase with an uppercase initial letter. Other mixes of uppercase and lowercase are not allowed.
- Names
- Names are not case-sensitive.
- A name can have a maximum of 30 characters.
- A name can consist of letters, numbers, underscores (_), and slashes (/).
- A name must start with a letter, slash character, or underscore. Underscores are recommended as the naming convention for associations.
- The separator for names with multiple parts is a period (.). Names with multiple parts can be paths, columns with a prefixed data source, and annotations.
- The following CDS keywords cannot be used as names: ALL, AND, AS, ASSOCIATION, AVG, CASE, CAST, CROSS, COUNT, DISTINCT, EXISTS, EXTEND, FROM, FULL, GROUP, HAVING, INNER, JOIN, KEY, LEFT, MAX, MIN, NOT, NULL, ON, OR, ORDER, RIGHT, SELECT, SUM, UNION, VIEW, WHEN, WHERE
- Literals
- Numeric literals must always be specified in full and a decimal point (.) used as a decimal separator if necessary.
- Character literals are enclosed in single quotation marks (').
- Comments
- Two forward slashes (//) or two hyphens (--) introduce a comment, which continues until the end of the line.
- Comments within lines or that span multiple lines are enclosed by the characters /* and */.
- Separators
- Statements can be closed using a semicolon (;). This is optional.
- Protected words
- Certain keywords are protected and cannot be used as self-defined names. The reserved names that cannot be used are specified in the database table TRESE.
Note:
The closing semicolon after a statement is optional because one piece of CDS source code can currently only contain a single statement.
Examples
- SELECT, select, and Select are all valid ways of specifying a keyword. seLect and SeLect, on the other hand, produce syntax errors.
- MYVIEW, myview, and myView all identify the same operand.
- 0.5 is a valid numeric literal, but .5 is not.
➤ Language Elements
The following sections summarize the language elements of the DDL and DCL of the ABAP CDS, arranged by topics.
- DDL
- CDS Views
- CDS View Enhancements
- CDS Table Functions
- DCL
- CDS Roles
In addition to the language elements show here, CDS source code can also contain annotations with metadata.
1. DDL
1.1 CDS Views
Keywords and Additions
Language Element | Meaning |
DEFINE VIEW ... AS | Defines a CDS view |
WITH PARAMETERS | Defines input parameters of a CDS view |
SELECT [DISTINCT] ... FROM | SELECT statement of a CDS view |
INNER JOIN ... ON | Inner join in a SELECT statement |
LEFT|RIGHT OUTER JOIN ... ON | Outer join in a SELECT statement |
ASSOCIATION ... TO ... AS ... ON | Defines an association for a path expression in a SELECT statement |
1: | Attribute of a path expression |
INNER ... WHERE | Attribute of a path expression |
LEFT OUTER ... WHERE | Attribute of a path expression |
KEY ... AS | Element of a SELECT list |
$EXTENSION.* | Elements of an enhancement in a SELECT list |
WHERE ... | WHERE clause of a SELECT statement |
GROUP BY ... | GROUP-BY clause of a SELECT statement |
HAVING ... | HAVING clause of a SELECT statement |
UNION ALL ... | UNION clause of a SELECT statement |
Expressions
Operators | Meaning |
MAX, MIN, AVG, SUM, COUNT | Aggregate expression in a SELECT statement |
+, -, *, / | Arithmetic expression in a SELECT statement |
CASE, WHEN, THEN, ELSE, END | Case distinction in a SELECT statement |
CAST | Cast expression in a SELECT statement |
=, <>, <, >, <=, >=, BETWEEN, LIKE, IS NULL, NOT, AND, OR |
Relational expression in a SELECT statement |
Predefined Functions
Function | Meaning |
ABS, CEIL, DIV, DIVISION, FLOOR, MOD, ROUND | Numeric function in a SELECT statement |
CONCAT, CONCAT_WITH_SPACE, INSTR,LEFT, LENGTH, LPAD, LTRIM, REPLACE,RIGHT, RPAD, RTRIM, SUBSTRING |
String function in a SELECT statement |
BINTOHEX, HEXTOBIN | Byte string function in a SELECT statement |
COALESCE | Coalesce function in a SELECT statement |
UNIT_CONVERSION, CURRENCY_CONVERSION, DECIMAL_SHIFT |
|
Conversion functions in a SELECT statement | |
DATS_IS_VALID, DATS_DAYS_BETWEEN, DATS_ADD_DAYS, DATS_ADD_MONTHS |
Date function in a SELECT statement |
TIMS_IS_VALID | Time function in a SELECT statement |
TSTMP_IS_VALID, TSTMP_CURRENT_UTCTIMESTAMP, TSTMP_SECONDS_BETWEEN, TSTMP_ADD_SECONDS |
Time stamp function in a SELECT statement |
Language Element | Meaning |
EXTEND VIEW ... WITH | Enhancement of a CDS view with an CDS view enhancement |
Language Element | Meaning |
DEFINE TABLE FUNCTION ... RETURNS ... IMPLEMENTED BY METHOD ... |
Defines a CDS table function |
2. DCL
Language Element | Meaning |
DEFINE ROLE ... GRANT SELECT ON | Defines a CDS role |
WHERE, AND, OR | WHERE clause in the definition of a CDS role |
... = ASPECT pfcg_auth ... | Condition for mapping roles |
COALESCE | Coalesce function in a SELECT statement |
... =|<>|<|>|<=|>=|LIKE|IS NULL ... | Literal condition |
➤ Annotations
An annotation enriches a definition in the ABAP CDS with metadata. It can be specified for specific scopes of a CDS object, namely specific places in a piece of CDS source code. SAP uses a set of predefined SAP annotations.
➥ Syntax for Annotations
- A simple or structured name annotation[.annotation1[.annotation2]...] after the characters @ or @<
- One of the following optional alternatives specified after a colon (:):
- Specified value value
- Structuring of the annotation using subannotations subannos in curly brackets
- Definition of an annotation array array in square brackets
- The annotation v_annot3 is an array consisting of two elements, which are identified in the evaluation using the indexes $1$ and $2$.
- v_annot4 and v_annot5 are specified in the same way. The structuring in v_annot4 is resolved into individual specifications as in v_annot5.
- The annotation v_annot6 is an array, in which the third component (a nested square bracket) is ignored. The other elements are identified for evaluation using the indexes $1$, $2$, and $4$.
- SAP provides a predefined set of SAP annotations that distinguishes between ABAP annotations and component annotations:
- The ABAP runtime environment evaluates every CDS object in ABAP CDS when it is activated and used. A set of ABAP annotations is employed here that expect certain values and are checked when activated. These ABAP annotations are listed in their specific scope in this documentation.
- Software components can dictate component annotations that are evaluated by dedicated frameworks.
- All other annotations are not usually evaluated, however it is possible that certain frameworks only allow certain annotations.
- Literals
- Character literals or numeric literals enclosed in quotation marks
- Truth values true and false
- These values can be enclosed in quotation marks but do not have to be.
- Enumeration values #value
- Specific annotations expect preset values, which are specified directly and without quotation marks after the # character.
- Values can be specified.
- Further substructures can be created.
- Arrays can be defined.
- The specified values value1, value2, ... define elements, which the specified values are assigned to.
- Subannotations subannos1, subannos2, ... in curly brackets define structured elements with one or more subannotations.
- It is possible to specify annotation arrays array1, array2, ... in square brackets in the syntax, however this is currently ignored when the the annotations are evaluated.
- The first element is an annotation with subannotation annot1 and without specified values.
- The second element consists of two annotations with subannotations annot2.s1 and annot2.s2 without specified values. The subannotations are created by the structured annotation annot2.
- The third element, like the second element, consists of two annotations with subannotations annot3.s1 and annot3.s2, which are explicitly specified here.
➥ Scopes for Annotations
Annotations can be specified for the following scopes in source code in ABAP CDS:
- DDL:
- View annotations for a CDS view
- In front of the statement DEFINE VIEW
- Extension annotations for a CDS view extension
- In front of the statement EXTEND VIEW
- Function annotations for a CDS table function
- In front of the statement DEFINE TABLE FUNCTION
- Parameter annotations for a parameter of a CDS view
- In front of and after the parameter
- Parameter annotations for a parameter of a CDS table function
- In front of and after the parameter
- Element annotations for an element of the SELECT list of a CDS view
- In front of and after the element
- Element annotations for an element in the element list of a CDS table function
- In front of and after the element
- DCL:
- Role annotations for a CDS role
- In front of the statement DEFINE ROLE
➥ Evaluation of Annotations
When an object defined in CDS source code is activated, the metadata defined by the annotations is saved in internal database tables in ABAP Dictionary. These tables can then be accessed to evaluate the data. This is done for every annotation with correct syntax regardless of name and value. ABAP annotations and component annotations are evaluated in different ways:
- ABAP Annotations
ABAP annotations define technical and semantic attributes of a CDS object. They are usually evaluated for every CDS object when activated by the ABAP runtime environment. ABAP annotations can modify the behavior of Open SQL statement that access a CDS entity. One important example is how the client-dependency of a CDS entity is defined using the associated ABAP annotation.
ABAP annotations introduced using EndUserText are used to define translatable semantic texts for a CDS object. The value of an annotation like this is saved in special tables that have a language key and that are translatable. The value specified in the source code should consist of text in the original language of the CDS source code and is translated into the required languages. The methods of the class CL_DD_DDL_ANNOTATION_SERVICE read these texts in accordance with an input parameter for the language. There are also special methods GET_LABEL_... that are designed for these texts only.
- Component annotations
The component annotations must be evaluated by the frameworks of the corresponding software components using a suitable API and are not relevant for activations and the ABAP runtime environment. Here, the names and values of these component annotations must follow the rules of the relevant framework. For SAP components, these can be taken from the tables of the SAP annotation documents.
The class CL_DD_DDL_ANNOTATION_SERVICE is provided for evaluations of the annotations of CDS entities. The method GET_ANNOTATIONS of the class CL_DD_DDL_ANALYZE also returns the annotations of CDS entities.
Annotations that are not evaluated by the ABAP runtime environment or by frameworks of other software components are usually ignored.
Note
The elements of annotation arrays are saved under the name of the array. A consecutive index enclosed in $ characters $1$, $2$, ... is appended to this array.
Example
The following CDS source code, which defines a CDS view, contains the specific ABAP annotations that are introduced using EndUserText.
@EndUserText.label: 'Demo View mit Texten'
@AbapCatalog.sqlViewName: 'DEMO_VIEW_TEXTS'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_text_annotations
with parameters @EndUserText.label:'Eingabeparameter'
param : abap.int4
as select from demo_expressions
{ @EndUserText:{ label:'Ein Element', quickInfo:'Feld' }
id }
The program DEMO_CDS_DDL_TEXTS uses methods of the class CL_DD_DDL_ANNOTATION_SERVICE to access the associated texts (in a language-dependent way). The original language of the view is German and the German texts must be available in every system. If an English translation exists, this is also read and displayed.
1. ABAP CDS - Evaluation of Annotations
This example demonstrates how semantics annotations in CDS source code are evaluated.
Source Code
REPORT demo_cds_semantics_annotation.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA incomplete_addresses TYPE STANDARD TABLE
OF demo_cds_semantics_annotation
WITH EMPTY KEY.
cl_dd_ddl_analyze=>get_annotations(
EXPORTING
ddlnames = VALUE #( ( 'DEMO_CDS_SEMANTICS_ANNOTATION' ) )
leaves_only = abap_true
IMPORTING
fieldannos = DATA(fieldannos) ).
TYPES field_anno LIKE LINE OF fieldannos.
DATA address_annos TYPE STANDARD TABLE OF field_anno-annoname
WITH EMPTY KEY.
address_annos = VALUE #(
( 'SEMANTICS.NAME.FULLNAME' )
( 'SEMANTICS.ADDRESS.STREET' )
( 'SEMANTICS.ADDRESS.CITY' )
( 'SEMANTICS.ADDRESS.ZIPCODE' )
( 'SEMANTICS.ADDRESS.COUNTRY' ) ).
DATA address_components TYPE STANDARD TABLE OF field_anno-fieldname
WITH EMPTY KEY.
address_components = VALUE #(
FOR address_anno IN address_annos
( VALUE #( fieldannos[ annoname = address_anno ]-fieldname
DEFAULT '---' ) ) ).
SELECT *
FROM demo_cds_semantics_annotation
INTO @DATA(address).
LOOP AT address_components INTO DATA(component).
ASSIGN COMPONENT component OF STRUCTURE address
TO FIELD-SYMBOL(<value>).
IF sy-subrc <> 0 OR <value> IS INITIAL.
incomplete_addresses = VALUE #( BASE incomplete_addresses
( address ) ).
EXIT.
ENDIF.
ENDLOOP.
ENDSELECT.
cl_demo_output=>display( incomplete_addresses ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
Description
This program uses a simple example to show how annotations can be evaluated in a framework. All rows in a CDS entity are to be found that do not contain a fully specified address. Here, a fully specified address is simply a set of elements for name, street, city, postal code, and country, none of which can be empty.
To do this, the program uses cl_dd_ddl_analyze=>get_annotations to read the element annotations of a CDS entity and gets the names of the elements defined (using semantics annotations) as the required components of an address in the internal table address_components. In the next step, the CDS entity is read using SELECT and a dynamic ASSIGN statement is used to check whether all required elements have a non-initial value for each read row. The output consists of any rows that do not contain a full address.
The following CDS view is used as an example for a CDS entity:
@AbapCatalog.sqlViewName: 'DEMO_CDS_SEMANNO'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_semantics_annotation
as select from scustom
{ id,
@Semantics.name.fullName
name,
@Semantics.name.prefix
form,
@Semantics.address.street
street,
@Semantics.address.postBox
postbox,
@Semantics.address.zipCode
postcode,
@Semantics.address.city
city,
@Semantics.address.country
country,
@Semantics.address.subRegion
region,
@Semantics.contact.type
custtype,
@Semantics.language
langu,
@Semantics.eMail.address
email }
This view wraps the database table SCUSTOM, which contains address data. The assignment of the semantics annotations to the columns of the database table gives the columns semantics that can be evaluated. Previously, this was only provided by those names and data elements that are no longer relevant for the evaluation depicted here.
➥ SAP Annotations
SAP annotations are annotations specified by SAP. When specified by SAP, an annotation must have the following:
- the name
- the possible values
- the structure consisting of subannotations
- the structure consisting of annotation arrays
SAP annotations are evaluated by SAP frameworks and can be one of the following two types: ABAP annotations or component annotations. ABAP annotations are evaluated when the object defined in the DDL source code is activated or when the object is used in the ABAP runtime environment. Component annotations, on the other hand, are evaluated by frameworks of other software components. An ABAP annotation can also be evaluated by other software components. Any application annotations that are not evaluated by a framework are ignored.
The following tables are a compilation of SAP annotations in the ABAP CDS DDL.
- Annotations for which the component "ABAP" is entered in the "Framework" column are evaluated as ABAP annotations by the ABAP runtime environment. The precise semantics of these annotations are described under the scope in question in the documentation.
- Annotations for which a different component is entered in the "Framework" column are component annotations. The component defines technical and semantic attributes of CDS objects required by specific software components, such as ODATA, UI, and Analytics. The documentation CDS Annotations describes the meaning of component annotations, which SAP component is evaluated by these annotations, and the permitted values for these annotations.
If square brackets [ ] are specified after a name in square brackets in the "Annotation" column, this means that the annotation before the square brackets is an annotation array and the element after the square brackets belongs to this element. The notation in the CDS source code compiles the elements in the square brackets
1. ABAP CDS, Annotation Array
This example shows how an annotation array is evaluated in a piece of CDS source code.
Source Code
REPORT demo_cds_annotation_array.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
cl_dd_ddl_analyze=>get_annotations(
EXPORTING ddlnames = VALUE #( ( 'DEMO_CDS_ANNOTATION_ARRAY' ) )
leaves_only = abap_true
IMPORTING
fieldannos = DATA(annos) ).
cl_demo_output=>display( annos ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
Description
The program reads the element annotations of the following CDS view:
@AbapCatalog.sqlViewName: 'DEMO_CDS_ANNOARR'
define view demo_cds_annotation_array
as select from demo_expressions
{ @Consumption.filter.hierarchyBinding:
[ { type: '...', value: '...', variableSequence: '...' },
{ type: '...', value: '...', variableSequence: '...' } ] id }
An annotation array Consumption.filter.hierarchyBinding with two elements is assigned to the view field ID and each of the elements is structured using three subannotations type, value, and variableSequence.
The annotation array is a component annotation defined by SAP and specified in the following form in the table for consumption annotations:
- Consumption.filter.hierarchyBinding[ ].type,
- Consumption.filter.hierarchyBinding[ ].value,
- Consumption.filter.hierarchyBinding[ ].variableSequence
The output of the program shows how the annotation is saved internally and how it can be evaluated:
- CONSUMPTION.FILTER.HIERARCHYBINDING$1$.TYPE
- CONSUMPTION.FILTER.HIERARCHYBINDING$1$.VALUE
- CONSUMPTION.FILTER.HIERARCHYBINDING$1$.VARIABLESEQUENCE
- CONSUMPTION.FILTER.HIERARCHYBINDING$2$.TYPE
- CONSUMPTION.FILTER.HIERARCHYBINDING$2$.VALUE
- CONSUMPTION.FILTER.HIERARCHYBINDING$2$.VARIABLESEQUENCE
ABAP CDS - Syntax的更多相关文章
- ABAP CDS ON HANA-(11)ABAP CDSでの関連付け
Association in ABAP CDS An association in CDS view joins different data sources. Defining and using ...
- ABAP CDS - SELECT, association
ABAP CDS - SELECT, association Syntax ... ASSOCIATION [ [min..max] ] TO target [AS _assoc] ON cond_e ...
- ABAP CDS - DEFINE VIEW, name_list
Syntax ... ( name1, name2, ... ) ... Effect Defines the element names of a CDS view in ABAP CDS in a ...
- ABAP CDS - DEFINE VIEW, view_annot
Syntax ... @annotation ... Effect Specifies Annotation annotation in the definition of a CDS view of ...
- ABAP CDS - Annotations 注解
Syntax ... annotation[.annotation1[.annotation2]][:value] ... Effect Annotation that can be specifi ...
- HANA CDS与ABAP CDS
如果你在网络或者SCN上面搜索CDS,即SAP的Core Data Services,你会很容易地找到类似“Core Data Services(CDS)是一个在SAP HANA中用于定义和消费富语义 ...
- 教程:基于访问控制的ABAP CDS视图权限
Hi! 对每一个CDS视图,我们都可以通过DCL(Data Control Language)定义访问控制.在这篇文章中,我会介绍ABAP CDS视图中非常重要的一面:权限管理. 本文的阐述基于我正在 ...
- CDS测试框架介绍:如何为ABAP CDS Entities写测试
动机 现在大家都知道单元测试对我们代码的好处.并且我们都承认它是开发过程中不可或缺的一部分.但是在把代码切换到数据库的模式下的时候,我们被粗暴地打回了软件测试的黑暗年代...我们现在面临着逻辑下推到A ...
- ABAP CDS Table Function介绍与示例
Core data services(以下简称CDS)可以指两样东西,一个是HANA CDS,一个是ABAP CDS. 如我们所知,HANA CDS只支持HANA数据库,ABAP CDS理论上支持多种 ...
随机推荐
- Struts2学习-拦截器2
1.做一个登陆页面(loginView.jsp,才用Action来访问),2.登陆成功后,可以跳转到系统的首页(index.jsp),3.首页有一个链接(testOtherAction访问其它的功能模 ...
- 2.LVS配置过程
请查看我的有道云笔记: http://note.youdao.com/noteshare?id=866edb5736418d29c86d68b5198c5c1c&sub=66F88F0A24D ...
- ZooKeeper 典型应用场景-负载均衡
负载均衡(Load Balance)是一种相当常见的计算机网络技术,用来对多个计算机(计算机集群).网络连接.CPU.硬盘驱动器或其他资源进行分配负载,以达到优化资源使用.最大化吞吐率.最小化响应时间 ...
- March 17 2017 Week 11 Friday
Simplicity is the ultimate sophistication. 简约才是精巧到了极致. Recently I have spent a great number of time ...
- 发送请求获取响应内容(c#)
C#请求url,获取响应的脚本 public string ResultOfApi(string url) { //发送请求 HttpWebRequest request = null; HttpWe ...
- [转]查找问题的利器 - Git Bisect
转自:http://gitbook.liuhui998.com/5_4.html 假设你在项目的'2.6.18'版上面工作, 但是你当前的代码(master)崩溃(crash)了. 有时解决这种问题的 ...
- JavaEE权限管理系统的搭建(六)--------使用拦截器实现菜单URL的跳转权限验证和页面的三级菜单权限按钮显示
本小结讲解,点击菜单进行页面跳转,看下图,点击管理员列表后会被认证拦截器首先拦截,验证用户是否登录,如果登录就放行,紧接着会被权限验证拦截器再次拦截,拦截的时候,会根据URL地址上找到对应的方法,然后 ...
- triplet loss
因为待遇低,因为工作不开心,已经严重影响了自己的工作积极性和工作效率,这几天发觉这样对自己实在是一种损失,决定提高工作效率,减少工作时间. 说说最近做的tracking, multi-object t ...
- LeetCode12.整数转罗马数字 JavaScript
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...
- 《深入理解Spring Cloud与微服务构建》书籍目录
转载请标明出处: https://blog.csdn.net/forezp/article/details/79735542 本文出自方志朋的博客 作者简介 方志朋,毕业于武汉理工大学,CSDN博客专 ...