当前位置:首页 > 编程笔记 > 正文
已解决

CDS(一)

来自网友在路上 184884提问 提问时间:2023-09-28 06:10:27阅读次数: 84

最佳答案 问答题库848位专家为你答疑解惑

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 _Gesellschafton $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: truecast(('KG') as abap.cuky( 5 )) as ZUNIT,@Semantics.amount.currencyCode: 'currency'price,@Semantics.currencyCode: truecurrency,planetype,_Gesellschaft
}

可以查看所有的注释标识

 

Select Distinct

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


define view ZDEMO_CDS_DDLas 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 carridwhen '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 carridwhen 'AA' then cast ( connid as abap.fltp ) * 1.3else          cast ( connid as abap.fltp ) * 0.9end 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_idhaving 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 ainner join  makt as bon 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_FILTER02with 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_extassociation to zmind2_conn as _fromon zspfli.carrid = _from.carrier_id{_from.airport_from_id ,_from.airport_to_id
}

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

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

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"CDS(一)":http://eshow365.cn/6-15059-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!