SAP CDS View基础语法
很多同学对于CDS view感觉无从下手,本篇博客将介绍CDS View的基础语法,并附有示例。1. 定义一个CDS View用途:创建一个CDS View语法:@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'define view ZDEMO_CDS_DDLas select from sbook{carrid,connid}解释:@AbapCatalog.sq
·
很多同学对于CDS view感觉无从下手,本篇博客将介绍CDS View的基础语法,并附有示例。
1. 定义一个CDS View
用途:
创建一个CDS View
语法:
@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
define view ZDEMO_CDS_DDL
as select from sbook
{
carrid,
connid
}
解释:
- @AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL' 指定了数据库SQL View的名字,在CDS View激活时,会在数据库层生成对应的SQL View
- define view ZDEMO_CDS_DDL是定义的CDS View的名字
- as select from sbook指定了CDS View的数据源,此处的数据源是DB Table sbook
- { }中定义了CDS view中包含哪些字段
运行效果:
2. Select Distinct
用途:
如果结果集中有重复的条目,DISTINCt可排除结果集中的重复条目。
语法:
@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
define view ZDEMO_CDS_DDL
as select distinct from sbook
{
carrid,
connid
}
解释:
- as select distinct from sbook指定了CDS View的数据源,此处的数据源是DB Table sbook
运行效果:
3. Where语句
用途:
限定筛选条件
语法:
@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
define view ZDEMO_CDS_DDL
as select from sbook
{
carrid,
connid
}
where carrid = 'AA'
运行效果:
4. Key 字段声明
用途:
指定CDS View中哪些字段是Key字段
语法:
@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
@AbapCatalog.preserveKey: true
define view ZDEMO_CDS_DDL
as select from sbook
{
key carrid,
key connid,
fldate,
bookid,
customid,
custtype
}
解释:
- @AbapCatalog.preserveKey: true 这句annotation 的值为TRUE时,SQL view中的key字段使用CDS中定义的key; 值为FALSE时,使用DB table中table的key field.
运行效果:
5. Build-in 函数
用途:
CDS中自带的一些运算函数
语法:
@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
@AbapCatalog.preserveKey: true
define view ZDEMO_CDS_DDL
as select from sbook
{
key carrid,
key connid,
concat( carrid, connid ) as flightno,
order_date,
concat( concat (substring(order_date,5,2),'-'),
substring(order_date,1,4) ) as ordermonth
}
解释:
- concat () 拼接两个字符串,substring( )获取字符串中的一个子串。
- 更多可用的Build-in函数可以查找ABAP的帮助文档
运行效果:
6. CASE表达式
用途:
实现分支运算
语法:
@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
@AbapCatalog.preserveKey: true
define view ZDEMO_CDS_DDL
as select from sbook
{
key carrid,
key connid,
case carrid
when 'AA' then 'American Airlines'
when 'AB' then 'Air Berlin'
when 'UA' then 'United Airlines'
else 'Other Airlines'
end as airline_name,
class,
case
when class = 'F' then 'First Class'
when class = 'Y' then 'Business Class'
else 'others'
end as class_level
}
解释:
- case...when...else...end as 构成分支运算的逻辑
- when语句中可以是逻辑表达式
运行效果:
7. CAST表达式
用途:
强制类型转换
语法:
@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
@AbapCatalog.preserveKey: true
define view ZDEMO_CDS_DDL
as select from sbook
{
key carrid,
key connid,
forcurkey,
forcuram as origin_amount,
case smoker
when 'X' then cast ( forcuram as abap.fltp ) * 1.3
else cast ( forcuram as abap.fltp ) * 0.9
end as final_amount
}
解释:
- CAST可以完成所需要的强制类型转换,进而实现在CDS中的运算需求;其中abap.fltp代表转换为abap中的浮点型
运行效果:
8. COALESCE函数
用途:
常用用户处理NULL的状况,为NULL语设定默认值
语法:
@AbapCatalog.sqlViewName: 'ZDEMO_CDS_SQL'
@AbapCatalog.preserveKey: true
define view ZDEMO_CDS_DDL
as select from sbook
{
key carrid,
key connid,
coalesce( smoker, 'unassigned') as somker_status
}
解释:
- coalesce(arg1, arg2): 如果arg1不为NULL则返回arg1的值,否则返回arg2的值
小结:
在本文中,我们介绍了SAP CDS View的最基本的运算语法,有关运算过程中遇到的问题,可以进一步根据ABAP 中的帮助文档,查看相关的用法。
更多推荐
已为社区贡献1条内容
所有评论(0)