sap-abap-cds
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSAP ABAP CDS (Core Data Services)
SAP ABAP CDS(Core Data Services)
Related Skills
相关技能
- sap-abap: Use for ABAP programming patterns used with CDS or when implementing EML statements in ABAP
- sap-btp-cloud-platform: Use for CDS deployment scenarios on BTP or ABAP Environment configurations
- sap-fiori-tools: Use when building Fiori Elements applications that consume CDS views or working with UI annotations
- sap-cap-capire: Use for comparing CDS syntax between ABAP and CAP or when integrating ABAP CDS with CAP services
- sap-api-style: Use when documenting CDS-based OData services or following API documentation standards
- sap-abap:适用于与CDS搭配使用的ABAP编程模式,或在ABAP中实现EML语句的场景
- sap-btp-cloud-platform:适用于BTP上的CDS部署场景或ABAP环境配置
- sap-fiori-tools:适用于构建消费CDS视图的Fiori Elements应用,或处理UI注解的场景
- sap-cap-capire:适用于对比ABAP与CAP的CDS语法,或集成ABAP CDS与CAP服务的场景
- sap-api-style:适用于编写基于CDS的OData服务文档,或遵循API文档规范的场景
Table of Contents
目录
- 1. CDS View Fundamentals
- 2. Essential Annotations
- 3. Expressions and Operations
- 4. Built-in Functions
- 5. Joins
- 6. Associations
- 7. Input Parameters
- 8. Aggregate Expressions
- 9. Access Control (DCL)
- 10. Data Retrieval from ABAP
- 11. Common Errors and Solutions
- 12. Useful Transactions and Tables
- Bundled Resources
- Source Documentation
1. CDS View Fundamentals
1. CDS视图基础
View Types
视图类型
| Type | Syntax | Database View | Since |
|---|---|---|---|
| CDS View | | Yes | 7.4 SP8 |
| CDS View Entity | | No | 7.55 |
Recommendation: Use CDS View Entities for new development.
| 类型 | 语法 | 是否生成数据库视图 | 始于版本 |
|---|---|---|---|
| CDS View | | 是 | 7.4 SP8 |
| CDS View Entity | | 否 | 7.55 |
推荐:新开发请使用CDS View Entities。
Basic CDS View Syntax
基础CDS视图语法
sql
@AbapCatalog.sqlViewName: 'ZCDS_EXAMPLE_V'
@AbapCatalog.compiler.CompareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Example CDS View'
define view ZCDS_EXAMPLE
as select from db_table as t
{
key t.field1,
t.field2,
t.field3 as AliasName
}sql
@AbapCatalog.sqlViewName: 'ZCDS_EXAMPLE_V'
@AbapCatalog.compiler.CompareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Example CDS View'
define view ZCDS_EXAMPLE
as select from db_table as t
{
key t.field1,
t.field2,
t.field3 as AliasName
}CDS View Entity Syntax (7.55+)
CDS View Entity语法(7.55+)
sql
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Example View Entity'
define view entity Z_CDS_EXAMPLE
as select from db_table as t
{
key t.field1,
t.field2,
t.field3 as AliasName
}Key Difference: View entities omit - no SQL view generated.
@AbapCatalog.sqlViewNamesql
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Example View Entity'
define view entity Z_CDS_EXAMPLE
as select from db_table as t
{
key t.field1,
t.field2,
t.field3 as AliasName
}核心差异:View Entity无需配置,不会生成SQL视图。
@AbapCatalog.sqlViewNameEclipse ADT Setup
Eclipse ADT设置步骤
- File → New → Other → Core Data Services → Data Definition
- Enter name, description, and package
- Select template (view, view entity, etc.)
- 文件 → 新建 → 其他 → Core Data Services → 数据定义
- 输入名称、描述和包名
- 选择模板(视图、视图实体等)
2. Essential Annotations
2. 核心注解
Core Annotations
基础注解
Essential annotations for CDS development:
- - SQL view name (max 16 chars)
@AbapCatalog.sqlViewName - - Optimize WHERE clauses
@AbapCatalog.compiler.CompareFilter - - Set to #NOT_REQUIRED, #CHECK, #MANDATORY, or #NOT_ALLOWED
@AccessControl.authorizationCheck - - User-facing description
@EndUserText.label - - Allow view extensions
@Metadata.allowExtensions
Complete Reference: See for 50+ annotations with examples.
references/annotations-reference.mdCDS开发必备注解:
- - SQL视图名称(最多16个字符)
@AbapCatalog.sqlViewName - - 优化WHERE子句
@AbapCatalog.compiler.CompareFilter - - 可选值:#NOT_REQUIRED、#CHECK、#MANDATORY、#NOT_ALLOWED
@AccessControl.authorizationCheck - - 面向用户的描述
@EndUserText.label - - 允许视图扩展
@Metadata.allowExtensions
完整参考:查看获取50+带示例的注解说明。
references/annotations-reference.mdSemantics Annotations (Currency/Quantity)
语义注解(货币/数量)
Required for CURR and QUAN data types to avoid error SD_CDS_ENTITY105:
sql
-- Currency fields
@Semantics.currencyCode: true
waers,
@Semantics.amount.currencyCode: 'waers'
amount,
-- Quantity fields
@Semantics.unitOfMeasure: true
meins,
@Semantics.quantity.unitOfMeasure: 'meins'
quantity处理CURR和QUAN数据类型时必须配置,避免出现SD_CDS_ENTITY105错误:
sql
-- 货币字段
@Semantics.currencyCode: true
waers,
@Semantics.amount.currencyCode: 'waers'
amount,
-- 数量字段
@Semantics.unitOfMeasure: true
meins,
@Semantics.quantity.unitOfMeasure: 'meins'
quantityUI Annotations (Fiori Elements)
UI注解(Fiori Elements)
sql
@UI.lineItem: [{ position: 10 }]
@UI.identification: [{ position: 10 }]
@UI.selectionField: [{ position: 10 }]
field1,
@UI.hidden: true
internal_fieldsql
@UI.lineItem: [{ position: 10 }]
@UI.identification: [{ position: 10 }]
@UI.selectionField: [{ position: 10 }]
field1,
@UI.hidden: true
internal_fieldConsumption Annotations (Value Help)
消费注解(值帮助)
sql
@Consumption.valueHelpDefinition: [{
entity: { name: 'I_Currency', element: 'Currency' }
}]
waersFor complete annotation reference, see .
references/annotations-reference.mdsql
@Consumption.valueHelpDefinition: [{
entity: { name: 'I_Currency', element: 'Currency' }
}]
waers完整注解参考请查看。
references/annotations-reference.md3. Expressions and Operations
3. 表达式与运算
CASE Expressions
CASE表达式
Simple CASE (single variable comparison):
sql
case status
when 'A' then 'Active'
when 'I' then 'Inactive'
else 'Unknown'
end as StatusTextSearched CASE (multiple conditions):
sql
case
when amount > 1000 then 'High'
when amount > 100 then 'Medium'
else 'Low'
end as AmountCategory简单CASE(单变量比较):
sql
case status
when 'A' then 'Active'
when 'I' then 'Inactive'
else 'Unknown'
end as StatusText搜索CASE(多条件判断):
sql
case
when amount > 1000 then 'High'
when amount > 100 then 'Medium'
else 'Low'
end as AmountCategoryComparison Operators
比较运算符
Standard operators: , , , , ,
Special operators: , , ,
=<><><=>=BETWEEN x AND yLIKEIS NULLIS NOT NULLComplete Reference: See for all operators and expressions.
references/expressions-reference.md标准运算符:, , , , ,
特殊运算符:, , ,
=<><><=>=BETWEEN x AND yLIKEIS NULLIS NOT NULL完整参考:查看获取所有运算符和表达式说明。
references/expressions-reference.mdArithmetic Operations
算术运算
sql
quantity * price as TotalAmount,
amount / 100 as Percentage,
-amount as NegatedAmountsql
quantity * price as TotalAmount,
amount / 100 as Percentage,
-amount as NegatedAmountSession Variables
会话变量
Available system variables (SY fields equivalent):
- (SY-UNAME) - Current user
$session.user - (SY-MANDT) - Client
$session.client - (SY-LANGU) - Language
$session.system_language - (SY-DATUM) - Current date
$session.system_date
Complete Reference: See for all system variables.
references/expressions-reference.mdsql
$session.user as CurrentUser,
$session.system_date as Today可用系统变量(等价于SY字段):
- (SY-UNAME) - 当前用户
$session.user - (SY-MANDT) - 客户端
$session.client - (SY-LANGU) - 系统语言
$session.system_language - (SY-DATUM) - 当前日期
$session.system_date
完整参考:查看获取所有系统变量说明。
references/expressions-reference.mdsql
$session.user as CurrentUser,
$session.system_date as Today4. Built-in Functions
4. 内置函数
CDS provides comprehensive built-in functions for string, numeric, and date operations.
CDS提供了完善的内置函数,支持字符串、数值和日期运算。
Key Function Categories
核心函数分类
- String Functions: concat(), length(), substring(), upper(), lower(), replace()
- Numeric Functions: abs(), ceil(), floor(), round(), division()
- Date Functions: dats_add_days(), dats_add_months(), dats_days_between()
- CAST Expression: Convert between ABAP data types
Complete Reference: See for all 50+ functions with examples.
references/functions-reference.md- 字符串函数:concat()、length()、substring()、upper()、lower()、replace()
- 数值函数:abs()、ceil()、floor()、round()、division()
- 日期函数:dats_add_days()、dats_add_months()、dats_days_between()
- CAST表达式:用于ABAP数据类型之间的转换
完整参考:查看获取50+带示例的函数说明。
references/functions-reference.mdQuick Examples
快速示例
sql
-- String operations
concat(first_name, last_name) as FullName,
upper(name) as UpperName,
substring(description, 1, 10) as ShortDesc
-- Numeric operations
abs(amount) as AbsoluteAmount,
round(value, 2) as RoundedValue,
division(10, 3, 2) as PreciseDivision
-- Date operations
dats_add_days(current_date, 7) as NextWeek,
dats_days_between(start_date, end_date) as Duration
-- Type conversion
cast(field as abap.char(10)) as TextField,
cast(amount as abap.curr(15,2)) as CurrencyField
**ABAP Types**: `abap.char()`, `abap.numc()`, `abap.int4`, `abap.dats`, `abap.tims`, `abap.curr()`, `abap.cuky`, `abap.quan()`, `abap.unit()`
---sql
-- 字符串操作
concat(first_name, last_name) as FullName,
upper(name) as UpperName,
substring(description, 1, 10) as ShortDesc
-- 数值操作
abs(amount) as AbsoluteAmount,
round(value, 2) as RoundedValue,
division(10, 3, 2) as PreciseDivision
-- 日期操作
dats_add_days(current_date, 7) as NextWeek,
dats_days_between(start_date, end_date) as Duration
-- 类型转换
cast(field as abap.char(10)) as TextField,
cast(amount as abap.curr(15,2)) as CurrencyField
**ABAP类型**:`abap.char()`, `abap.numc()`, `abap.int4`, `abap.dats`, `abap.tims`, `abap.curr()`, `abap.cuky`, `abap.quan()`, `abap.unit()`5. Joins
5. 连接查询
Join Types
连接类型
sql
-- INNER JOIN (matching rows only)
inner join makt as t on m.matnr = t.matnr
-- LEFT OUTER JOIN (all from left, matching from right)
left outer join marc as c on m.matnr = c.matnr
-- RIGHT OUTER JOIN (all from right, matching from left)
right outer join mvke as v on m.matnr = v.matnr
-- CROSS JOIN (cartesian product)
cross join t001 as cosql
-- INNER JOIN(仅返回匹配行)
inner join makt as t on m.matnr = t.matnr
-- LEFT OUTER JOIN(返回左表所有行,右表匹配行)
left outer join marc as c on m.matnr = c.matnr
-- RIGHT OUTER JOIN(返回右表所有行,左表匹配行)
right outer join mvke as v on m.matnr = v.matnr
-- CROSS JOIN(笛卡尔积)
cross join t001 as co6. Associations
6. 关联关系
Associations define relationships between entities (join-on-demand):
关联用于定义实体之间的关系(按需连接):
Defining Associations
定义关联
sql
define view Z_ASSOC_EXAMPLE as select from scarr as c
association [1..*] to spfli as _Flights
on $projection.carrid = _Flights.carrid
association [0..1] to sairport as _Airport
on $projection.hub = _Airport.id
{
key c.carrid,
c.carrname,
c.hub,
// Expose associations
_Flights,
_Airport
}sql
define view Z_ASSOC_EXAMPLE as select from scarr as c
association [1..*] to spfli as _Flights
on $projection.carrid = _Flights.carrid
association [0..1] to sairport as _Airport
on $projection.hub = _Airport.id
{
key c.carrid,
c.carrname,
c.hub,
// 暴露关联
_Flights,
_Airport
}Cardinality Notation
基数表示
Syntax mapping:
- or
[0..1]→[1](LEFT OUTER MANY TO ONE)association to one - →
[1..1](exact match)association to one - or
[0..*]→[*](LEFT OUTER MANY TO MANY)association to many - →
[1..*](one or more)association to many
Complete Reference: See for detailed cardinality guide.
references/associations-reference.md语法映射:
- 或
[0..1]→[1](左外连接多对一)association to one - →
[1..1](精确匹配)association to one - 或
[0..*]→[*](左外连接多对多)association to many - →
[1..*](一对多,至少一个匹配)association to many
完整参考:查看获取详细的基数指南。
references/associations-reference.mdNew Cardinality Syntax (Release 2302+)
新基数语法(2302版本及以上)
sql
association to one _Customer on ... -- [0..1]
association to many _Items on ... -- [0..*]sql
association to one _Customer on ... -- [0..1]
association to many _Items on ... -- [0..*]Using Associations
使用关联
sql
-- Expose for consumer use
_Customer,
-- Ad-hoc field access (triggers join)
_Customer.name as CustomerNamesql
-- 暴露给消费者使用
_Customer,
-- 临时字段访问(触发连接)
_Customer.name as CustomerNamePath Expressions with Filter
带过滤的路径表达式
sql
-- Filter with cardinality indicator
_Items[1: Status = 'A'].ItemNoFor complete association reference, see .
references/associations-reference.mdsql
-- 带基数标识的过滤
_Items[1: Status = 'A'].ItemNo完整关联参考请查看。
references/associations-reference.md7. Input Parameters
7. 输入参数
Defining Parameters
定义参数
sql
define view Z_PARAM_EXAMPLE
with parameters
p_date_from : dats,
p_date_to : dats,
@Environment.systemField: #SYSTEM_LANGUAGE
p_langu : spras
as select from vbak as v
{
key v.vbeln,
v.erdat,
v.erzet
}
where v.erdat between :p_date_from and :p_date_tosql
define view Z_PARAM_EXAMPLE
with parameters
p_date_from : dats,
p_date_to : dats,
@Environment.systemField: #SYSTEM_LANGUAGE
p_langu : spras
as select from vbak as v
{
key v.vbeln,
v.erdat,
v.erzet
}
where v.erdat between :p_date_from and :p_date_toParameter Reference
参数引用
Use colon notation or
:p_date_from$parameters.p_date_fromCalling from ABAP:
abap
SELECT * FROM z_param_example(
p_date_from = '20240101',
p_date_to = '20241231',
p_langu = @sy-langu
) INTO TABLE @DATA(lt_result).使用冒号语法 或
:p_date_from$parameters.p_date_from从ABAP调用示例:
abap
SELECT * FROM z_param_example(
p_date_from = '20240101',
p_date_to = '20241231',
p_langu = @sy-langu
) INTO TABLE @DATA(lt_result).8. Aggregate Expressions
8. 聚合表达式
Aggregate Functions
聚合函数
sql
define view Z_AGG_EXAMPLE as select from vbap as i
{
i.vbeln,
sum(i.netwr) as TotalAmount,
avg(i.netwr) as AvgAmount,
max(i.netwr) as MaxAmount,
min(i.netwr) as MinAmount,
count(*) as ItemCount
}
group by i.vbeln
having sum(i.netwr) > 1000sql
define view Z_AGG_EXAMPLE as select from vbap as i
{
i.vbeln,
sum(i.netwr) as TotalAmount,
avg(i.netwr) as AvgAmount,
max(i.netwr) as MaxAmount,
min(i.netwr) as MinAmount,
count(*) as ItemCount
}
group by i.vbeln
having sum(i.netwr) > 10009. Access Control (DCL)
9. 访问控制(DCL)
Basic DCL Structure
基础DCL结构
sql
@MappingRole: true
define role Z_CDS_EXAMPLE_DCL {
grant select on Z_CDS_EXAMPLE
where (bukrs) = aspect pfcg_auth(F_BKPF_BUK, BUKRS, ACTVT = '03');
}sql
@MappingRole: true
define role Z_CDS_EXAMPLE_DCL {
grant select on Z_CDS_EXAMPLE
where (bukrs) = aspect pfcg_auth(F_BKPF_BUK, BUKRS, ACTVT = '03');
}Authorization Check Options
权限检查选项
Available values:
- - No authorization check
#NOT_REQUIRED - - Warning if no DCL exists
#CHECK - - Error if no DCL exists
#MANDATORY - - DCL ignored if exists
#NOT_ALLOWED
Complete Reference: See for detailed DCL patterns.
references/access-control-reference.md可选值:
- - 无权限检查
#NOT_REQUIRED - - 无DCL时抛出警告
#CHECK - - 无DCL时抛出错误
#MANDATORY - - 忽略已有的DCL
#NOT_ALLOWED
完整参考:查看获取详细的DCL模式说明。
references/access-control-reference.mdCondition Types
条件类型
PFCG Authorization:
where (field) = aspect pfcg_auth(AUTH_OBJECT, AUTH_FIELD, ACTVT = '03')Literal Condition:
where status <> 'DELETED'User Aspect:
where created_by ?= aspect userCombined:
where (bukrs) = aspect pfcg_auth(...) and status = 'ACTIVE'For complete access control reference, see .
references/access-control-reference.mdPFCG权限:
where (field) = aspect pfcg_auth(AUTH_OBJECT, AUTH_FIELD, ACTVT = '03')字面量条件:
where status <> 'DELETED'用户维度:
where created_by ?= aspect user组合条件:
where (bukrs) = aspect pfcg_auth(...) and status = 'ACTIVE'完整访问控制参考请查看。
references/access-control-reference.md10. Data Retrieval from ABAP
10. 从ABAP中获取数据
Standard SELECT
标准SELECT查询
abap
SELECT * FROM zcds_example
WHERE field1 = @lv_value
INTO TABLE @DATA(lt_result).abap
SELECT * FROM zcds_example
WHERE field1 = @lv_value
INTO TABLE @DATA(lt_result).SALV IDA (Integrated Data Access)
SALV IDA(集成数据访问)
abap
cl_salv_gui_table_ida=>create_for_cds_view(
CONV #( 'ZCDS_EXAMPLE' )
)->fullscreen( )->display( ).abap
cl_salv_gui_table_ida=>create_for_cds_view(
CONV #( 'ZCDS_EXAMPLE' )
)->fullscreen( )->display( ).11. Common Errors and Solutions
11. 常见问题与解决方案
SD_CDS_ENTITY105: Missing Reference Information
SD_CDS_ENTITY105:缺失参考信息
Problem: CURR/QUAN fields without reference
Solution: Add semantics annotations
sql
@Semantics.currencyCode: true
waers,
@Semantics.amount.currencyCode: 'waers'
netwrOr import currency from related table:
sql
inner join t001 as c on ...
{
c.waers,
@Semantics.amount.currencyCode: 'waers'
v.amount
}问题:CURR/QUAN字段未配置参考字段
解决方案:添加语义注解
sql
@Semantics.currencyCode: true
waers,
@Semantics.amount.currencyCode: 'waers'
netwr或从关联表引入货币字段:
sql
inner join t001 as c on ...
{
c.waers,
@Semantics.amount.currencyCode: 'waers'
v.amount
}Cardinality Warnings
基数警告
Problem: Cardinality doesn't match actual data
Solution: Define cardinality matching data model
sql
association [0..1] to ... -- Use for optional relationships
association [1..*] to ... -- Use for required one-to-manyFor complete troubleshooting guide, see .
references/troubleshooting.md问题:基数与实际数据不匹配
解决方案:定义与数据模型匹配的基数
sql
association [0..1] to ... -- 用于可选关系
association [1..*] to ... -- 用于必填的一对多关系完整排查指南请查看。
references/troubleshooting.md12. Useful Transactions and Tables
12. 常用事务码与表
Key Transactions
核心事务码
- SDDLAR - Display/repair DDL structures
- RSRTS_ODP_DIS - TransientProvider preview
- RSRTS_QUERY_CHECK - CDS query metadata validation
- SE63 - Translation (EndUserText)
- SE11 - ABAP Dictionary
- SU21 - Authorization objects
- SDDLAR - 展示/修复DDL结构
- RSRTS_ODP_DIS - 瞬态提供程序预览
- RSRTS_QUERY_CHECK - CDS查询元数据校验
- SE63 - 翻译(EndUserText)
- SE11 - ABAP字典
- SU21 - 权限对象
Important Tables
重要表
- DDHEADANNO - Header-level annotations
- CDSVIEWANNOPOS - CDS view header annotations
- CDS_FIELD_ANNOTATION - Field-level annotations
- ABDOC_CDS_ANNOS - SAP annotation definitions
- DDHEADANNO - 头层级注解
- CDSVIEWANNOPOS - CDS视图头注解
- CDS_FIELD_ANNOTATION - 字段层级注解
- ABDOC_CDS_ANNOS - SAP注解定义
API Class
API类
CL_DD_DDL_ANNOTATION_SERVICE- - Get all annotations
get_annos() - - Get @EndUserText.label
get_label_4_element()
CL_DD_DDL_ANNOTATION_SERVICE- - 获取所有注解
get_annos() - - 获取@EndUserText.label值
get_label_4_element()
Bundled Resources
配套资源
Reference Documentation
参考文档
For detailed guidance, see the reference files in :
references/- - Complete annotation catalog
annotations-reference.md - - All built-in functions with examples
functions-reference.md - - Associations and cardinality guide
associations-reference.md - - DCL and authorization patterns
access-control-reference.md - - Expressions and operators
expressions-reference.md - - Common errors and solutions
troubleshooting.md
如需详细指导,请查看目录下的参考文件:
references/- - 完整注解目录
annotations-reference.md - - 所有内置函数及示例
functions-reference.md - - 关联与基数指南
associations-reference.md - - DCL与权限模式
access-control-reference.md - - 表达式与运算符
expressions-reference.md - - 常见错误与解决方案
troubleshooting.md
Templates
模板
For templates, see :
templates/- - Standard CDS view template
basic-view.md - - View with input parameters
parameterized-view.md - - Access control definition
dcl-template.md
如需模板,请查看目录:
templates/- - 标准CDS视图模板
basic-view.md - - 带输入参数的视图
parameterized-view.md - - 访问控制定义模板
dcl-template.md
Source Documentation
源文档
Update this skill by checking:
- https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/abencds.html (ABAP Cloud)
- https://help.sap.com/docs/SAP_NETWEAVER_AS_ABAP_752/f2e545608079437ab165c105649b89db/7c078765ec6d4e6b88b71bdaf8a2bd9f.html (NetWeaver 7.52 User Guide)
- https://github.com/SAP-samples/abap-cheat-sheets
- https://community.sap.com/t5/tag/CDS%20Views/tg-p
Last Verified: 2025-11-23
更新本技能请参考:
- https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/abencds.html (ABAP Cloud)
- https://help.sap.com/docs/SAP_NETWEAVER_AS_ABAP_752/f2e545608079437ab165c105649b89db/7c078765ec6d4e6b88b71bdaf8a2bd9f.html (NetWeaver 7.52 用户指南)
- https://github.com/SAP-samples/abap-cheat-sheets
- https://community.sap.com/t5/tag/CDS%20Views/tg-p
最后验证时间:2025-11-23