ABAP on HANA之CDS Association和Path Expression
本文阐述了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
Material Quantity Unit Conversion using CDS Views
ABAP on HANA之CDS Association和Path Expression的更多相关文章
- ABAP CDS - SELECT, association
ABAP CDS - SELECT, association Syntax ... ASSOCIATION [ [min..max] ] TO target [AS _assoc] ON cond_e ...
- 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 ...
- ABAP CDS - Language Elements
The following sections summarize the language elements of the DDL and DCL of the ABAP CDS, arranged ...
- 【ABAP CDS系列】ABAP CDS中的系统信息
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP CDS系列]ABAP CDS中的系统 ...
- 【HANA系列】【第七篇】SAP HANA XS使用Data Services查询CDS实体【一】
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列][第七篇]SAP HANA XS ...
- ABAP CDS-介绍(ABAP CDS视图)
前言 文章翻译自Tushar Sharma的文章,转载请注明原作者和译者! 在SAP发展到SAP HANA版本之后,SAP内部的技术正在快速地变化,SAP开发业务应用程序的方式已经发生了范式转变(根本 ...
- ABAP CDS-Part 1(ABAP CDS实体)
文章翻译自Tushar Sharma的文章,转载请注明原作者和译者! 目录 预备条件 一.概述 二.ABAP CDS实体(CDS Entity) a.定义ABAP CDS Views b.ABAP C ...
- ABAP CDS - DEFINE VIEW, view_annot
Syntax ... @annotation ... Effect Specifies Annotation annotation in the definition of a CDS view of ...
- ABAP CDS ON HANA-(7)CDSビューでの集約
Aggregate expression in CDS View An aggregate expression calculates a single value from an operand o ...
随机推荐
- 用 pyinstaller 打包含xpinyin 库的Python程序
在文章用 pyinstaller 打包含有 pinyin 库的程序中,给出了如何使用pyinstaller 打包含xpinyin 库的Python程序的方法,能生成可运行的exe文件.本文将会给出 ...
- HTTP状态码分类
前言: 我们经常使用浏览器发出http请求,那么对于请求返回的状态,对于开发人员来讲,我们必须要明白其所代表的含义,如: 常见状态吗:400(请求无效),401(需要权限),500(服务器错误), 今 ...
- 推荐写作平台gitbook——让我们换一种形式写作
https://www.gitbook.com/ 我一直用这个平台进行写作.目前有两本电子书可以供大家阅读,分别如下 Office 365 开发入门指南 https://www.gitbook.com ...
- .NET MVC项目设置包含Areas中的页面为默认启动页
利用vs创建一个MVC项目后,一般的默认启动页是根目录下-->Controllers-->HomeController-->Index这个方法对应的页面. 我先说下创建Areas的流 ...
- Git合并指定文件到另一个分支
经常被问到如何从一个分支合并特定的文件到另一个分支.其实,只合并你需要的那些commits,不需要的commits就不合并进去了. 合并某个分支上的单个commit 首先,用git log或sourc ...
- spring boot 打jar包,获取resource路径下的文件
前言:最近在spring boot项目静态类中获取resource路径下文件,在idea中启动都可以获取,但是打包后变成了jar包 就无法获取到. 我想到了两种方法,一种是根据http访问静态资源比如 ...
- Netty 系列六(编解码器).
一.概念 网络传输的单位是字节,如何将应用程序的数据转换为字节,以及将字节转换为应用程序的数据,就要说到到我们该篇介绍的编码器和解码器. 将应用程序的数据转换为网络格式,以及将网络格式转换为应用程序的 ...
- SpringBoot的打包失败
打包是根据pom.xml文件来打的包. 如果使用Maven的默认打包,只会将src/main下的java和resources里面的内容打进Jar包中. 通过maven-assembly-plugin这 ...
- [js高手之路]深入浅出webpack教程系列1-安装与基本打包用法和命令参数
[js高手之路]深入浅出webpack教程系列索引目录: [js高手之路]深入浅出webpack教程系列1-安装与基本打包用法和命令参数 [js高手之路]深入浅出webpack教程系列2-配置文件we ...
- 4 ;XHTML表格
1.表格的基本格式 2.<table>标签下的常用属性 3.<table>标签下的边框设置 4.<tr><th><td>标签下的常用属性 5 ...