ABAP CDS - SELECT, association

Syntax

... ASSOCIATION [ [min..max] ] TO target [AS _assoc] ON cond_exp 
                [ WITH DEFAULT FILTER cond_exp ] ...

Extras:

1. ... [min..max]

2. ... AS _assoc

3. ... WITH DEFAULT FILTER cond_exp

Effect

Defines an association of the name _assoc in a SELECT statement of a CDS view. An association associates the current CDS view as a source data source with the target data source target specified in the definition of the association using an ON condition cond_exp . A data source target can be a database table defined in ABAP Dictionary, a classic view, an external view, or a CDS entity.

An association of a SELECT statement in a CDS view can be accessed as follows:

  • By specifying its name in a path expression in the same statement and in all places where this is documented.
  • If an association is published using a path expression in the SELECT list of the current SELECT statement, the following can use it in their path expressions:
can use it in their path expressions.

When a CDS view is activated with path expressions, every association specified here is transformed to a join expression. The source data source represents the left side and the target data source represents the right side. The ON condition of the association is added to the ON condition of the join. By default, the category of the join is determined by where the path expression is used:

  • After FROM, it is an inner join (INNER JOIN)
  • In all other locations, it is a left outer join (LEFT OUTER JOIN)

This setting can be overwritten when specifying the association in a path expression using an attribute.

When specifying the ON condition, the following special rules apply:

  • If the association in the SELECT list of the current SELECT statement is published, the fields of the source data source specified in the ON condition must also be listed in the SELECT list. This ensures that a join expression can be built from the association (when used in a path expression). In the ON condition, the field name can be prefixed by $projection instead of the name of the source data source. An alternative element name defined using AS can be used here instead of the field name.
  • The fields of the target data source must be prefixed in the ON condition by the name of the association (prefix _assoc. separated by a period).

Notes

  • Associations not listed in the SELECT list can only be used in path expressions of the current SELECT statement.
  • The syntax for defining and using associations is a higher-value wrapping of the syntax for joins. Using associations instead of directly programming joins makes it easier to read the definition of a CDS view. Associations can be used to model relationships between CDS entities that can be accessed simply using path expressions in CDS views or in Open SQL.
  • When a CDS view is activated, a join defined by an association is built for every use in a path expression and not for the definition of the association. No joins are constructed for associations that are not used in their CDS views.
  • Associations and join expressions can be used in a SELECT statement of a CDS view.
  • Special rules apply to associations in SELECT statements joined with UNION.
  • Cyclical dependencies should be avoided when using associations to prevent problems occurring in mass activations of CDS entities.

Addition 1

... [min..max]

Effect

Defines the cardinality of the target data source of a CDS view, which is defined with an association ASSOCIATION. The square brackets [ ] are part of the syntax. For min and max, positive integers (including 0) and asterisks (*) can be specified:

  • max cannot be 0.
  • An asterisk * for max means any number of rows.
  • min can be omitted (set to 0 if omitted).
  • min cannot be *.
  • When an association is used in a WHERE condition, 1 must be specified for max.

If the cardinality is not defined explicitly, the cardinality "to 1" is used implicitly ([min..1]).

When specified, the cardinality is used mainly to document the semantics of the data model. The cardinality is not validated at runtime but can produce syntax check warnings.

Note

The specified cardinality is evaluated by the syntax check for paths specified in the CDS DDL of CDS or in Open SQL. Currently, a warning is produced if a value greater than 1 is specified for max, indicating that a path specified in this way influences the cardinality of the results set.

Addition 2

... AS _assoc

Effect

Defines the name _assoc of an association defined using ASSOCIATION of a CDS view. If no name is defined explicitly using AS, _assoc is set implicitly to the name of the target data source. The name _assoc must comply with the naming rules for names.

Note

It is advisable to use an underscore _ as the first character of the association name.

Example

Example of a simple association. The following CDS view provides the same result as the CDS view DEMO_CDS_SCARR_SPFLI in the joins example, as shown in the program DEMO_CDS_ASSOCIATION using an assertion. Furthermore, the association spfli_scarr is published to be used from outside in the SELECT list by specifying a path that contains only the name of an association. The program DEMO_CDS_ASSOCIATION also shows how the association can be accessed by specifying a path in Open SQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@AbapCatalog.sqlViewName: 'DEMO_CDS_ASSOC'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_association(
    _spfli_scarr,
    id,
    carrier,
    flight,
    departure,
    destination
  )
  as select from
    spfli
    association [1..1] to scarr as _spfli_scarr on
      $projection.carrid = _spfli_scarr.carrid
    {
          _spfli_scarr,
      key spfli.carrid,
      key _spfli_scarr.carrname,
      key spfli.connid,
          spfli.cityfrom,
          spfli.cityto
    }

Example

The following CDS view sales_order_invoice_header returns information about sales invoices and works with the following databases: snwd_so_inv_head, snwd_so, snwd_bpa, snwd_so_inv_item.

Two associations are defined:

  • _buyer stands for a join between the current view and the target data source snwd_bpa.
  • _invoice_items stands for a join between the current view and the target data source snwd_so_inv_item.

The source data source fields used in the ON conditions - node_key and buyer_guid - are part of the SELECT list. Here the recommended prefix $projection is used instead of the prefixes snwd_so_inv_head or snwd_so_inv_head.

The association _buyer is not listed in the SELECT list and can only be used in path expressions of the current SELECT statement. This association can be specified in the WHERE condition due to the cardinality [1..1]. The association _invoice_items is not accessed in path expressions of the current SELECT statement. However, this association is listed in the SELECT list, which means it can be used in path expressions of other CDS views. This association cannot be specified in a WHERE condition due to the cardinality [1..*].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@AbapCatalog.sqlViewName: 'SALESO_INVHDR_VW'
define view sales_order_invoice_header as
  select from snwd_so_inv_head
           inner join snwd_so
             on snwd_so_inv_head.so_guid = snwd_so.node_key
         association [1..1] to snwd_bpa as _buyer
           on $projection.buyer_guid = _buyer.node_key
         association [1..*] to snwd_so_inv_item as _invoice_items
           on $projection.node_key = _invoice_items.parent_key
         key snwd_so_inv_head.node_key,      //used in assoc _invoice_items
               snwd_so_inv_head.buyer_guid,    //used in assoc _buyer
               snwd_so.so_id as sales_order_id,
               _buyer.bp_id as buyer_id,       //from assoc _buyer
               snwd_so_inv_head.payment_status,
              @Semantics.currencyCode
               snwd_so_inv_head.currency_code,
              @Semantics.amount.currencyCode: 'currency_code'
               snwd_so_inv_head.gross_amount,
               _invoice_items                  //publish assoc _invoice_items
         }
          where _buyer.bp_role = '001';          //usage of assoc buyer

  

The CDS view can be accessed in an ABAP program with a simple SELECT statement (Open SQL).

SELECT sales_order_id, buyer_id, payment_status 
       FROM sales_order_invoice_header 
       INTO CORRESPONDING FIELDS OF TABLE @itab.

The complexity of the actual query is wrapped transparently in the CDS view for the application programmer. When the view is accessed, the join (defined by the association _invoice_items) between snwd_so_inv_head and snwd_so_inv_item is not built, because there are no path expressions that need to access the join.

The CDS view sales_order_invoice_header mentioned above is used as the data source in the definition of the CDS view sales_order_invoice_items. This data source is used to access the published association _invoice_items. The elements of the association are accessed in this view. There is no visual indication that it is the result of a join. This join between snwd_so_inv_head and snwd_so_inv_item is created when the CDS view sales_order_invoice_items is activated. The other association _buyer of the CDS view sales_order_invoice_header cannot be accessed.

1
2
3
4
5
6
7
8
9
@AbapCatalog.sqlViewName: 'SALESO_INVITM_VW'
define view sales_order_invoice_items as
  select from sales_order_invoice_header as header
  { header.sales_order_id,
    header._invoice_items.inv_item_pos as item_position,
   @Semantics.currencyCode
    header._invoice_items.currency_code,
   @Semantics.amount.currencyCode: 'currency_code'
    header._invoice_items.gross_amount }
 

Addition 3

... WITH DEFAULT FILTER cond_exp

Effect

Defines a standard filter condition for a path expression.

  • If no filter condition is specified when the association is used in an path expression in the attributes attributes, the condition cond_exp specified using DEFAULT FILTER is used as the filter condition and applied in an extended condition for the join. The same rules apply to the default filter condition as to a filter condition specified as an attribute.
  • If no filter condition is specified when the association is used in a path expression in the attributes attributes, this condition is used instead of the default filter condition.

Note

When the syntax check evaluates a cardinality specified using [min..max], the default filter condition is respected alongside the ON condition.

機械日本語翻訳>>

ABAP CDS - SELECT, association的更多相关文章

  1. ABAP CDS - SELECT, WHERE

    格式 ... WHERE cond_expr ... 结果 定义CDS视图结果集的Where条件.访问CDS视图时,结果集仅包含来自数据源数据源的数据,该数据源数据源满足在where之后指定的条件co ...

  2. ABAP CDS ON HANA-(11)ABAP CDSでの関連付け

    Association in ABAP CDS An association in CDS view joins different data sources. Defining and using ...

  3. CDS测试框架介绍:如何为ABAP CDS Entities写测试

    动机 现在大家都知道单元测试对我们代码的好处.并且我们都承认它是开发过程中不可或缺的一部分.但是在把代码切换到数据库的模式下的时候,我们被粗暴地打回了软件测试的黑暗年代...我们现在面临着逻辑下推到A ...

  4. ABAP CDS Table Function介绍与示例

    Core data services(以下简称CDS)可以指两样东西,一个是HANA CDS,一个是ABAP CDS. 如我们所知,HANA CDS只支持HANA数据库,ABAP CDS理论上支持多种 ...

  5. Create Fiori List App Report with ABAP CDS view – PART 1

    From Create Fiori List App Report with ABAP CDS view – PART 1 In this blog, I am going to show How C ...

  6. ABAP CDS - Syntax

    The syntax of the DDL and of the DCL of the ABAP CDS comprises elements of the general DDL and DCL o ...

  7. ABAP CDS - Language Elements

    The following sections summarize the language elements of the DDL and DCL of the ABAP CDS, arranged ...

  8. HANA CDS与ABAP CDS

    如果你在网络或者SCN上面搜索CDS,即SAP的Core Data Services,你会很容易地找到类似“Core Data Services(CDS)是一个在SAP HANA中用于定义和消费富语义 ...

  9. 教程:基于访问控制的ABAP CDS视图权限

    Hi! 对每一个CDS视图,我们都可以通过DCL(Data Control Language)定义访问控制.在这篇文章中,我会介绍ABAP CDS视图中非常重要的一面:权限管理. 本文的阐述基于我正在 ...

随机推荐

  1. sass-基础

    导入: sass的导入(@import)规则和CSS的有所不同,编译时会将@import的scss文件合并进来只生成一个CSS文件. 但是如果你在sass文件中导入css文件如@import 'res ...

  2. 当尝试从ArcCatalog、.net应用或是Java应用中连接ArcGIS Server 时,显示下面任何一种错误提示: "Access Denied" 或 "The connection could not be made"

    Error: 访问拒绝或无法连接错误 文章编号 : 29042 软件: ArcGIS Server 9.0, 9.1, 9.2, 9.3, 9.3.1 操作系统: Windows 2000, XP, ...

  3. Linux与Windows区别——总结中

    一:在Linux系统中,每一个文件都多加了很多的属性进来,尤其是用户组的概念 二:Windows下面一个文件是否具有执行的能力是通过“扩展名”来判断的,如:.exe,.bat,.com等 Linux下 ...

  4. 04、Spark Standalone集群搭建

    04.Spark Standalone集群搭建 4.1 集群概述 独立模式是Spark集群模式之一,需要在多台节点上安装spark软件包,并分别启动master节点和worker节点.master节点 ...

  5. make知识

    makelist 语法 https://cmake.org/cmake/help/v3.10/manual/cmake-language.7.html CMakeLists.txt I am of t ...

  6. PHP获取系统时间不对的解决办法(转载)

    原地址:https://blog.csdn.net/u012124764/article/details/51450958 使用PHP获取系统时间,发现时间不对,是因为PHP默认的时区是UTC,应该将 ...

  7. eplise一键集成工具

    因为要做平台,后台的内容就由我负责,目前想让测试人员  在本地使用eplise可以进行脚本开发,但是很多人都死在了搭建环境的道路上,那我就做了一键集成,点击就可以把所需要的配置项进行配置,总结:实际就 ...

  8. 使用shell脚本实现在liunx上进行svn的上传下载更新功能

    最近有个功能,是需要从在liunx上拉取svn地址,并创建一个新文件进行提交,shell脚本如下 #!/bin/bash echo "Hello World !" myFile=& ...

  9. caffe RandomBrightness和RandomContrast

    1. void RandomBrightness(const cv::Mat& in_img, cv::Mat* out_img, const float brightness_prob, c ...

  10. CPP-基础:类

    1,成员访问属性 一,对于类的实现来说: private:类内部(包括类域范围内)可访问. protect:类内部(包括类域范围内)或 派生类类内部(包括类域范围内)可访问. public: 类内部和 ...