zoukankan      html  css  js  c++  java
  • 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

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

           Material Quantity Unit Conversion using CDS Views

           HANA CDS与ABAP CDS

  • 相关阅读:
    [Selenium]Eclipse hangs at 57% in debug mode with TestNG tests
    [Selenium] CSS3 选择器
    [Selenium]如何实现上传本地文件
    [Selenium]显式等待 Explicit wait & 隐式等待 Implicit wait
    [Selenium]中使用css选择器进行元素定位
    [Selenium]验证点了某个Button之后无反应
    7. Debug on local machine
    6. Manage the driver for browser and the script for Hub
    4. Configure maven in Spring Tool Suite
    3. Install Spring-Tool-Suite & TestNG
  • 原文地址:https://www.cnblogs.com/hhelibeb/p/9202781.html
Copyright © 2011-2022 走看看