CDS(一)

Core Data Services 核心数据服务

DDL 定义Query Language 查询DCL 控制(权限相关)
model,从语义层获取数据open SQL 访问CDS viewCDS权限定义,集成旧的权限概念
扩展native SQL扩展SQL建模和声明

ABAP ViewCDS View
支持所有数据库
支持查询内连接内连接,外连接,union
支持计算不支持支持aggregation,grouping,calculation
支持子view不支持

可以基于一个CDSview创建另一个

CDS命名:

其他规则:

SQL视图名称与不带“_”前缀的CDS名称相同。

CDS视图参数以“P_”开头,后跟CamelCase名称。

Associations以“_”开头,后跟不带前缀的视图名称。例如:“_Product”表示与“I_Product”视图的关联 

字段:简洁,驼峰法命名(SalesOrder)

UUID:Universally Unique Identifier 唯一标识

特殊字段

代码:代码是具有固定值列表的字段,例如语言和货币:

表示一个布尔值或是个真实的值陈述

OrderIsReleased, NotificationHasLongText

数量或数量:

NetAmountInDisplayCurrency, TaxAmount, OrderQuantity

计时:

CreationDateTime

比率和比率:

ExchangeRate, ConditionRateInPercent, ProbabilityRatio

Session variables

as select from sflight 
     association to ZI_HT_DDIC as _Gesellschaft
      on $projection.carrid = _Gesellschaft.carrid
{
       key carrid,                               
       key connid,
       @EndUserText.label: 'Test-01'
       @EndUserText.quickInfo: 'Test-02'
       key fldate,
       @Semantics.quantity.unitOfMeasure: 'quantity'
       seatsmax*70        as  AvgGewicht,
       @Semantics.unitOfMeasure: true
       cast(('KG') as abap.cuky( 5 )) as ZUNIT,
       @Semantics.amount.currencyCode: 'currency'
       price,
       @Semantics.currencyCode: true
       currency,
       planetype,
        
       _Gesellschaft
}

可以查看所有的注释标识

 

Select Distinct

如果结果集中有重复的条目,DISTINCt可排除结果集中的重复条目。


define view ZDEMO_CDS_DDL
  as select distinct from sbook
{
  carrid,
  connid
}

Key 字段声明

指定CDS View中哪些字段是Key字段

CASE表达式

实现分支运算

  • case...when...else...end as 构成分支运算的逻辑
  • when语句中可以是逻辑表达式

CAST表达式

强制类型转换

  • CAST可以完成所需要的强制类型转换,进而实现在CDS中的运算需求;其中abap.fltp代表转换为abap中的浮点型

COALESCE函数

常用用户处理NULL的状况,为NULL语设定默认值

  • coalesce(arg1, arg2): 如果arg1不为NULL则返回arg1的值,否则返回arg2的值

define view ZI_HT_DDIC 
//with parameters p_carrid: abap.char( 3 ) 
//使用association 方法关联其他数据源(数据库表、视图、CDS)
 as select from spfli {
    key carrid,                                //指定CDS View中哪些字段是Key字段
    key connid,
        airpto,
        cityfrom,
        cityto,
        countryfr,
        arrtime,
        deptime,
        concat(cityfrom, cityto) as test1 ,     //concat () 拼接两个字符串,substring( )获取字符串中的一个子串。
//      concat( concat (substring(arrtime,5,2),'-'),  

/*case...when...else...end as 构成分支运算的逻辑
when语句中可以是逻辑表达式*/
       case carrid
            when 'AA' then 'American Airlines'
            when 'AB' then 'Air Berlin'
            when 'UA' then 'United Airlines'
            when 'CN' then 'China Airlines'
            else 'Other Airlines'
            end as airline_name,
            
 //CAST可以完成所需要的强制类型转换,进而实现在CDS中的运算需求;其中abap.fltp代表转换为abap中的浮点型
 
 case carrid
            when 'AA' then cast ( connid as abap.fltp ) * 1.3
            else          cast ( connid as abap.fltp ) * 0.9
       end as final_amount,
//coalesce(arg1, arg2): 如果arg1不为NULL则返回arg1的值,否则返回arg2的值

 coalesce( airpto, arrtime ) as status
     
}
// where carrid = $parameters.p_carrid;

Build-in 函数

常见的数值表达式如下:
ABS:获取绝对值
CEIL:向上取整
FLOOR:向下取整
DIV:除法计算,取整数位
DIVISION:除法计算,保留 N 位小数
MOD:除法计算,取余数
ROUND:计算舍入值

CONCAT:连接字符串,参数固定为2个,各个表达式之间可以嵌套使用,CONCAT内部也可以使用 &&
&&:连接字符串,参数没有个数限制,但不能将其他内嵌表达式当作参数使用,仅作为操作符使用,在非SELECT语句中也可以被使用
CONCAT_WITH_SPACE:连接字符串,并用 N 个空格分隔,该表达式结果不能超过1333个字符
INSTR:遍历字符串,查找指定字符 s1 并返回第一次出现的位置,没有查到则返回0
LEFT/RIGHT:从字符串左/右侧开始取出 N 位字符,忽略前导/尾部的空格
LENGTH:计算字符串长度
DATS_IS_VALID/TIMS_IS_VALID:校验日期/时间有效性,有效时返回 1,否则返回 0
DATS_DAYS_BETWEEN:计算日期d1和d2相隔的天数
DATS_ADD_DAYS:为指定日期加上N天
DATS_ADD_MONTHS:为指定日期加上N月
TSTMP_IS_VALID:校验时间戳有效性,有效时返回 1,否则返回 0
TSTMP_CURRENT_UTCTIMESTAMP:返回当前时间戳
TSTMP_SECONDS_BETWEEN:计算时间戳 t1 和 t2 相隔的秒数,需要用赋值语句进行传参,可以添加相应的错误处理
TSTMP_ADD_SECONDS:为指定时间戳加上 N 秒,N 必须为 timestamp 类型
ABAP_USER_TIMEZONE:获取用户时区,不传参时默认获取当前用户当前 Client 的时区
ABAP_SYSTEM_TIMEZONE:获取系统时区,不传参时默认获取当前 Client 的时区TSTMP_TO_DATS:将时间戳转换成对应时区的日期
TSTMP_TO_TIMS:将时间戳转换成对应时区的时间
TSTMP_TO_DST:根据时间戳获取对应时区的夏令时标识
DATS_TIMS_TO_TSTMP:将日期和时间根据时区转换成时间戳

define view ZI_HT_Customer 


as select from zmind2_conn {
  key carrier_id as AirlineId,
   key connection_id as ConnectionId,
   airport_from_id as DepartureAirport,
   airport_to_id as DestinationAirport,
   departure_time as DepartureTime,
   arrival_time as ArrivalTime,
   distance as Distance,
   distance_unit as DistanceUnit,
 
  key carrier_id,
    min(distance) as MinDistance,
     max(distance) as MaxDistance,
     avg(distance) as AvgDistance,
     sum(distance) as SumDistance,
     count(*) as count_dis
             
   }
   
    group by  
    carrier_id
    having 
    count(*) > 2
  // where carrier_id = 'LH'
  • Having中指定的条件字段,只能是group by中的字段的子集;在Having中也可以使用聚集运算的中间结果集作为删选条件
  • 使用聚集运算时,要使用group by指定聚集的件,也即按哪些字段进行分组统计
  • Inner Join, Left Outer Join, Right Outer Join的用法同ABAP OPEN SQL的用法一致
  • UNION可以合并两个SELECT的结果集,并自定去除重复的条目                                       (1)UNION ALL 合并结果集,保留重复的条目(2)合并的两个结果集要有相同(3)字段数合并结果集的列类型要兼容(4)字段名称要相同

 增强或扩展CDS视图 | 优通SAP (ut163.com)

ABAP CDS View with input parameters

  • CDS Database View : It is read-only classical database view in ABAP Dictionary.
  • CDS Entity: It is actual CDS view. It covers the CDS Database view and makes other attributes possible, such as authorization checks defined in CDS view

1.Define input parameters in a CDS View.

2.Use input parameters in a CDS View.

@AbapCatalog.sqlViewName: 'ZHTTABLEFILTER'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Exercise 4'
define view ZHT_TABLE_FILTER
with parameters matnr:abap.numc(18)
  as select from mara as a
        inner join  makt as b
        on a.matnr = b.matnr
      {
        key a.matnr as material,
        a.ersda,
        a.created_at_time,
        a.ernam,
        a.laeda,
        a.aenam,
        a.vpsta,
        a.pstat,
        a.lvorm,
        a.mtart,
        a.mbrsh,
        b.maktx as description
      } where a.matnr = $parameters.matnr;

3.Call a parameterized view with open SQL.

define view ZHT_TABLE_FILTER02
    with parameters matnr:abap.numc(18)
as select * from ZHT_TABLE_FILTER  (matnr:$parameters.matnr)

尝试显示一个简单的ALV:

REPORT zht_exercise4.

  DATA alv  TYPE REF TO IF_SALV_GUI_TABLE_IDA.
   alv = cl_salv_gui_table_ida=>create_for_cds_view(
       iv_cds_view_name = 'ZHT_TABLE_FILTER' ).
       ALV->set_view_parameters(
        it_parameters = VALUE #(
        ( name = 'matnr' value = '000000000051011280')
        ) ).
if alv is not initial.
alv->fullscreen( )->display( ).
endif.

Extend View

在SE11中,我们通过append对table或view进行增强,在CDSview中,对原有的CDS View进行增强可以如下

@AbapCatalog.sqlViewAppendName: 'ZMIND2_EXT'
@EndUserText.label: 'Monster CDS view extension'
extend view ZI_HT_DDIC 
       with ZI_HT_Customer_ext
       association to zmind2_conn as _from
                  on zspfli.carrid = _from.carrier_id
       {
      _from.airport_from_id ,
      _from.airport_to_id
}

增强后,原CDS View将有增强标记

在原始view中,可以看到append的字段.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值