本文阐述了ABAP CDS association的概念,并且展示了在CDS视图中和SQL语句中写路径表达式(Path Expression)代码的方法。我也会解释如何在CDS asociation中指定inner join——默认情况下是left outer join,以及如何为association添加过滤。

对于CDS的相关开发,SAP希望我们使用association而不是join,因为association更加接近“概念思维”。基本上,association本身不是join,它只是有关join连接可能性的元数据,它会按需成为join。真实的join会在路径表达式使用association的时候被创建。

一个简单的CDS association例子,它看起来和left outer join没区别:

@AbapCatalog.sqlViewName: 'ZCDS_ASSOC11'
define view zcds_assoc1 as select from scarr as sca
association [0..1] to spfli as _spfli
on sca.carrid = _spfli.carrid
{ * }

暴露CDS association的例子:

@AbapCatalog.sqlViewName: 'ZCDS_ASSOC41'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view zcds_assoc4 as select from sairport as sair
association [1..*] to spfli as _spfli on
$projection.airportfrom = _spfli.airpfrom
{
sair.id as airportfrom,
sair.name,
sair.time_zone,
-- exposing association
_spfli
}

在下面的例子里,你可以看到SPFLI表对SFLIGHT表和SAIRPORT表的association。通过别名alias _sfli和SFLIGHT和_sair,SAIRPORT的全部字段暴露在projection列表中。当路径表达式用于调用association时,会根据选择字段创建join条件:spfli.carrid = sflight.carrid and spfli.connid = sflight.connid and on spfli.airport = sairport.id。

@AbapCatalog.sqlViewName: 'ZCDS_ASSOC21'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view zcds_assoc2 as select from spfli
association to sflight as _sfli on
spfli.carrid = _sfli.carrid and
spfli.connid = _sfli.connid
association [1..1] to sairport as _sair on
$projection.airportfrom = _sair.id
{
spfli.carrid,
spfli.connid,
spfli.airpfrom as airportfrom,
-- exposing association
_sfli,
_sair
}

在下面的例子里,定义association时使用了上面的CDS entity,ZCDS_ASSOC2 :

@AbapCatalog.sqlViewName: 'ZCDS_ASSOC31'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view zcds_assoc3 as select from scarr
association [1..1] to zcds_assoc2 as _cdsassoc on
$projection.carrierid = _cdsassoc.carrid
{
scarr.carrid as carrierid,
-- Exposing Association
_cdsassoc
}

Open SQL语句中的路径表达式:在SQL语句中调用CDS association,需要使用如下的路径表达式。在上面的CDS association中,通过_cdsassoc暴露了完整的projection列表。

DATA(w_dbfeature) = cl_abap_dbfeatures=>use_features( 
 requested_features = VALUE #( ( cl_abap_dbfeatures=>views_with_parameters ) ) ).
IF w_dbfeature IS NOT INITIAL.
SELECT carrierid ,
\_cdsassoc\_sfli-fldate AS flightdate,
\_cdsassoc\_sair-name AS flightname
FROM zcds_assoc3
WHERE carrierid = @carrid
INTO TABLE @DATA(t_data1).
ENDIF.

CDS视图中的路径表达式:

@AbapCatalog.sqlViewName: 'ZCDS_ON_ASSOC1'
define view zcds_on_assoc with parameters airport: S_FROMAIRP as select from zcds_assoc2 as cds2
{
cds2.carrid,
cds2.connid,
cds2.airportfrom,
cds2._sair.name, -- use inner join...by default association uses left outer join
cds2._sfli.planetype
}
where cds2.airportfrom = :airport

如我所提到的那样,在被路径表达式调用时,CDS association会默认创建left outer join。

在SPFLI表数据中,没有RTM机场的航班。

当我们输入参数airport = RTM的时候 ,下面的CDS视图的查询结果会是一条RTM机场的数据,但是这条记录里没有carrid。

@AbapCatalog.sqlViewName: 'ZCDS_ON_ASSOC41'
define view zcds_on_assoc4 with parameters airport: S_FROMAIRP as select from zcds_assoc4 as cds_assoc
{
cds_assoc.airportfrom,
cds_assoc.name,
cds_assoc.time_zone,
cds_assoc._spfli.carrid -- use inner join...by default association uses left outer join
}
where cds_assoc.airportfrom = :airport

运行上面的CDS视图:

data preview中的结果:

为了把默认的left outer join变成inner join,我们需要使用[inner],如下:

@AbapCatalog.sqlViewName: 'ZCDS_ON_ASSOC41'
define view zcds_on_assoc4 with parameters airport: S_FROMAIRP as select from zcds_assoc4 as cds_assoc
{
cds_assoc.airportfrom,
cds_assoc.name,
cds_assoc.time_zone,
cds_assoc._spfli[inner].carrid -- use inner join...by default association uses left outer join
}
where cds_assoc.airportfrom = :airport

如果运行它,输入参数RTM,得到的结果为空:

目前,还不可以在CDS association中使用right outer join。

CDS association中的过滤例子如下:

@AbapCatalog.sqlViewName: 'ZCDS_ASSOC_FILT'
define view ZCDS_ASSOC_FILTER as select from zcds_assoc2 as cds2
{
cds2._sair[ id = 'TYO' ].name,
cds2._sfli.planetype
}

CDS视图的输出结果:

希望本文对你有帮助!

本文链接:https://www.cnblogs.com/hhelibeb/p/9202781.html

英文原文:CDS Associations and 路径表达式s – ABAP on HANA

参考阅读:ABAP 7.52 中的Open SQL新特性

       Material Quantity Unit Conversion using CDS Views

       HANA CDS与ABAP CDS

ABAP on HANA之CDS Association和Path Expression的更多相关文章

  1. ABAP CDS - SELECT, association

    ABAP CDS - SELECT, association Syntax ... ASSOCIATION [ [min..max] ] TO target [AS _assoc] ON cond_e ...

  2. 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 ...

  3. ABAP CDS - Language Elements

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

  4. 【ABAP CDS系列】ABAP CDS中的系统信息

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP CDS系列]ABAP CDS中的系统 ...

  5. 【HANA系列】【第七篇】SAP HANA XS使用Data Services查询CDS实体【一】

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列][第七篇]SAP HANA XS ...

  6. ABAP CDS-介绍(ABAP CDS视图)

    前言 文章翻译自Tushar Sharma的文章,转载请注明原作者和译者! 在SAP发展到SAP HANA版本之后,SAP内部的技术正在快速地变化,SAP开发业务应用程序的方式已经发生了范式转变(根本 ...

  7. ABAP CDS-Part 1(ABAP CDS实体)

    文章翻译自Tushar Sharma的文章,转载请注明原作者和译者! 目录 预备条件 一.概述 二.ABAP CDS实体(CDS Entity) a.定义ABAP CDS Views b.ABAP C ...

  8. ABAP CDS - DEFINE VIEW, view_annot

    Syntax ... @annotation ... Effect Specifies Annotation annotation in the definition of a CDS view of ...

  9. ABAP CDS ON HANA-(7)CDSビューでの集約

    Aggregate expression in CDS View An aggregate expression calculates a single value from an operand o ...

随机推荐

  1. Redis学习笔记(1)-安装Oracle VM VirtualBox

    Oracle VM VirtualBox官网网址 打开安装包网址界面,如下所示,点击截图红框. 下载完成后,点击exe文件,不停的点击下一步. 因为是使用MarkDown编辑器书写的尝试,所以写的简单 ...

  2. Linux配置2个或多个Tomcat同时运行

    一.问题说明今天操作Linux部署项目的时候,公司领导要求,只给一个服务器,但是有2个项目要部署,而且需要独立分开运行. 二.解决方法Linux配置两个或多个Tomcat,一个Tomcat对应部署一个 ...

  3. linux磁盘管理增加,扩容

    一.磁盘空间不足,添加新的磁盘 一般来说,当我们在服务上插入新的磁盘时,服务器是会对磁盘进行识别的.但是,有的时候服务器并没有对这些新插入的磁盘进行识别.这时,我们可以通过重启服务器,来使服务器重新加 ...

  4. iframe实用操作

    iframe高度设置为子页面高度 //需要使用Jquery   $(document).ready(function () {             parent.document.getEleme ...

  5. JAVA & .NET创建对象构造函数调用顺序

    JAVA 定义Person类 package models; ​ public class Person { public Person() { System.out.println("pe ...

  6. 爬虫之re数据提取的使用

    本文将业务场景中最常用的几点实例,给大家列举出来,不常见的不再一一赘述.  使用urllib库可以模拟浏览器发送请求获得服务器返回的数据,下一步就是把有用的数据提取出来.数据分为两种形式:结构化和非结 ...

  7. springMVC_11拦截器实现登录

    一.   思路 controller实现核对用户名和密码,如果核对正确则保存到session中并且跳转到主页 系统中包含诸多界面,部分界面不需要登录即可进行访问,通过拦截器实现判断是否是不需要登录的界 ...

  8. tomcat端口修改以及jvm启动参数设置

    1.端口更改:找到config目录下server.xml文件 如下 <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to t ...

  9. LeetCode | HouseCode 算法题

    题目: You are a professional robber planning to rob houses along a street. Each house has a certain am ...

  10. 2.Odoo产品分析 (一) – 一切为零

    查看Odoo产品分析系列--目录 1. 默认数据库 声明在先  本系列文档(Odoo产品分析)整理来自本人对该ERP的理解,并结合文档Working-with-Odoo-10-Second-Editi ...