原创 无峰 [ ABAP 技巧与实战 ](javascript:void\(0\);)
一
前言
数据字典是ABAP中最基本的知识,充分使用数据字典可以给你的程序提供一些基本功能(帮助,搜索帮助,数据校验等)
这些功能可以改善程序对用户的友好度
**本文主要介绍数据字典提供的基本功能**
希望本文对你的ABAP编程习惯能有一些积极的影响.
二
基本功能
数据字典给ABAP程序提供如下基本功能
• F1帮助
• F4搜索帮助
• 数据校验
• 日志记录
• 填入之前使用的值
• 多语言支持
• SAP逻辑锁
• ......
三
F1帮助
通过数据元素文档获取
定义的表或者结构字段使用数据元素提供字段的基本属性.可以在数据元素的文档功能中维护该字段的详细解释.
这样,所有用到这个字段的界面(SCREEN,ALV等),都可以直接通过F1获取到字段的帮助.
给字段添加详细描述是很好的习惯.用户或其它开发可以通过字段帮助了解到这个字段在程序中的具体作用.
可以在帮助中把这个字段对业务的影响,相关配置点,相关功能,维护方式,维护示例,对程序作用等等信息详细的写出来.
• 图一是维护数据元素文档的方式
• 图二是在SE16N界面(ALV界面)中在该字段按F1功能键获取的帮助信息
图一
图二
四
F4搜索帮助
SAP系统数据字典通过以下层级提供搜索帮助功能,
在表维护界面(SE11)可以看到字段搜索帮助的来源(优先级4>3>2>1)
在通过外键表获取输入帮助的时候, 外键表最好维护一个文本表,否则搜索帮助中只有关键字,没有描述信息.
• 图三:搜索帮助来源是域值,数据元素搜索帮助,字段搜索帮助
• 图四:搜索帮助来源是外键表
• 图五:文本表和主表外键关系
使用数据字典的搜索帮助是程序获取搜索帮助的基本方法,程序中也可以通过语句设置特定的搜索帮助.
图三
图四
图五
五
数据校验
通过域值限制或外键表获取数据校验
• 域定义中,可以通过固定值,范围值或值表校验数据内容(图六)
• 可以通过表中定义外键关系限制校验数据内容(图七)
在SCREEN或者ALV中如果输入值超出限定,系统会给出报错信息.
如图八,通过SE16N(ALV)维护EKKO时, 输入凭证类型ZP99超出了限定,系统报错
需要注意的是,外键有可能是联合主键, 如果定义外键时没有把相关表的联合主键都定义进去, 则会出现数据无法维护的情况(通过SE16N无法维护)
需要说明的是,这些校验在输入界面上自动触发(输入界面上的字段必须参考数据字典字段). 通过SQL语句写入数据不会触发这些校验
图六
图七
图八
六
日志记录
设置数据元素change documnet 记录的开关(图九). 勾选更改文档标记后,
通过SAP标准变更文档对象(SCDO),就能记录字段级别的内容变更日志.
表技术设计中的日志数据记录.(图十). 勾选日志数据更改后, 同时设置系统参数 rec/client
系统就会把表内容的变更记录下来,通过事务代码SCU3可以查看这些变更的日志
注意: 尽量只对配置表勾选日志数据记录,对业务表勾选并激活参数将导致数据库资源的大量占用.
图九
图十
图十一
七
填入之前使用的值
通过数据元素的参数ID 可以为屏幕字段设置参数ID. 参数ID可以保留上次录入的内容,下次在该字段上自动带出.
八
多语言支持
数据元素的描述在界面上会自动带出, 并且可以通过标准翻译功能翻译到其它语言中,切换登录语言后,系统会自动切换对应语言的描述显示在屏幕上.
标准翻译功能 (SE63)
> 快速翻译功能 详见连接 无峰,公众号:ABAP 技巧与实战 [ SAP开发框架系列之 快速翻译功能
> ](http://mp.weixin.qq.com/s?__biz=Mzg4MjA1MjE4NQ==&mid=2247484821&idx=1&sn=33c30a82c2cfc70d740662938061466b&chksm=cf5dd8a3f82a51b5dfd8befe5a1c52d3a8893c903a06c90674b04c6a3b149c824a32088dedb6#rd)
九
SAP逻辑锁
定义锁对象,会自动生成相应的加锁,解锁函数. 通过对加锁函数的调用, 会生成一个逻辑锁(通过SM12可以查看系统中的所有逻辑锁).
十
总结
SAP数据字典给ABAP程序提供了很多基本功能.
因此在定义数据字典的时候遵照以下规则
• 尽量使用数据元素/域定义字段属性
• 尽量通过域值或者外键表限定字段内容
• 尽量使用文本表定义编码的描述
• 尽量在数据元素中添加帮助文本
• 尽量在数据元素中绑定搜索帮助
• 勾选数据元素的更改文档标记
• ......
在开发程序的时候,更多的使用数据字典对象,可以让程序附带数据字典提供的基本功能, 尤其是帮助功能. 更多的帮助信息可以让程序更易懂,更容易被用户使用.
** THE **
** END **
**约定**
如果你对这篇文章感兴趣,请帮忙点赞,在看,分享.
( **如果你真的喜欢这篇文章,请记得回来打个赏,作为支持我继续下去的动力,这是一个正反馈过程.
越多的人打赏,作者越有动力分享,读者就能享受更多的福利.毕竟打赏的金额富不了我,穷不了你,却能支持这个公众号长久发文.** )
**公众号 : syjf1976_abap**
**ABAP开发技巧**
**微信号 : 392077**
syjf1976
sharry_xlp
Yannick_Duan
申请进入公众号讨论群提问或者参与话题讨论
以下内容整理自 SAP 官方 ABAP 速查表(主题:ABAP Dictionary),作为本文的扩展补充资料。
This cheat sheet ...
[!NOTE] - While several DDIC objects are still supported in ABAP Cloud (excluding, for example, classic DDIC views), it is recommended to use their ABAP CDS-based successors for new developments.
TYPES statements. [!NOTE] - DDIC provides a centralized location for defining and managing types that are frequently (re)used in DDIC, ABAP CDS, or ABAP programs. This means that a type change in DDIC will automatically update in all relevant places, potentially causing inconsistencies and making adjustments necessary. To find the users of a particular type in ADT, right-click the type and select Get Where-Used List. - Global access to types is only possible if the package concept does not specify otherwise. A package, which encapsulates repository objects into self-contained units, can be set to disallow external access. Find more information here.
DDIC supports the following data types:
abap.type.| Built-in Dictionary Type | Details |
|---|---|
int4 | Represents 4-byte integers Mapped to the ABAP type i |
char | Represents strings of fixed length Mapped to the ABAP type c with a dedicated length For example, in database tables and fields of CDS entities, the maximum length is 1333 characters. The built-in dictionary type clnt is a special character-like type and has special semantics. It denotes the client column in client-dependent DDIC database tables. The type is mapped to the ABAP type c, length 3. |
string | Represents strings of variable length In DDIC, it is handled like a character large object/CLOB. A maximum length can be specified, but there is no upper limit. The type cannot be used to specify key fields in database tables. In the tables, a maximum length of at least 256 can be specfied to restrict the length of database table fields. Mapped to the identically named ABAP type string Another built-in dicitionary type mapped to the ABAP type string is sstring, which represents shorter text strings having a maximum length of 1333. It is handled like char and can be used as type for a database table key field (unlike string; note that trailing blanks are removed). |
datn/timn | Represent date and time formats Mapped to d/t dats/tims are older types. datn/timn are preferred because dats/tims require conversion to actual date and time types. |
utclong | Represents time stamps Mapped to the identically named ABAP data type utclong |
numc | Represents numeric text Mapped to n |
raw | Represents byte strings Mapped to x |
[!NOTE] - There are restrictions when using strings in ABAP CDS and ABAP SQL. For more information, see here. - Built-in dictionary types cannot be used directly in ABAP, e.g. for typing local data objects. However, the types can be used in ABAP SQL, and also ABAP CDS, in the context of typed literals. Find more information in the ABAP SQL cheat sheet.
They can be ...
TYPES and DATA statements.data. Object references can refer to classes and interfaces, or be the generic type object. Reference types cannot be used to type database table fields.dec type used for packed numbers, a length of 5 with 2 decimals means the output length is 6, including a decimal point. If a sign is selected, the first character is reserved for the + or - sign, making the output length 7.[!NOTE] - CDS simple types represent successor artifacts of DDIC data elements. - As is true for all the global types discussed, locally defined types hide identically named global types in ABAP programs.
TYPES and DATA in ABAP programs. They cannot be reused in repository objects other than data elements.[!NOTE] In modern ABAP, CDS enumerated types offer a similar functionality for fixed values.
Example: DDIC Data Elements/Domains
"Using data elements for local types and data objects
"Data element based on a built-in dicitionary type
TYPES ty_dtel1 TYPE zdemo_abap_dtel_pr.
DATA char3_dtel TYPE zdemo_abap_dtel_pr.
"Data element based on a domain
TYPES ty_dtel2 TYPE zdemo_abap_dtel_do.
DATA char1_dtel TYPE zdemo_abap_dtel_do.
"Data element based on a reference type
TYPES ty_dtel3 TYPE zdemo_abap_dtel_ref.
DATA char10_dtel_ref TYPE zdemo_abap_dtel_ref.DEFINE STRUCTURE.#NOT_EXTENSIBLE indicates that the object cannot be enhanced). Find more information here.abap.type (technical properties such as length are specified in parentheses).REFERENCE TO someType.INCLUDE; optionally, a three-character suffix can be specified.[!NOTE] DDIC database tables and several CDS entities also represent structured types that can be used as such in ABAP programs.
Example: DDIC Structures
The following example creates two DDIC structures exploring several options:
@EndUserText.label : 'Demo flat DDIC structure'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
define structure zdemo_abap_struc_flat {
chars : abap.char(3);
num : abap.int4;
cuky : abap.cuky;
@Semantics.amount.currencyCode : 'zdemo_abap_struc_flat.cuky'
curr : abap.curr(8,2);
id : zdemo_abap_dtel_pr;
flag : zdemo_abap_dtel_do;
} @EndUserText.label : 'Demo deep DDIC structure'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
define structure zdemo_abap_struc_deep {
bt_elem1 : abap.char(5);
bt_elem2 : abap.int4;
bt_elem3 : abap.dec(8,2);
bt_elem4 : abap.cuky;
@Semantics.amount.currencyCode : 'zdemo_abap_struc_deep.bt_elem4'
bt_elem5 : abap.curr(5,2);
dtel_elem1 : land1;
dtel_elem2 : tzntstmpl;
dref1 : reference to abap.char(3);
dref2 : reference to abap_boolean;
dref3 : reference to data;
oref1 : reference to cl_abap_math;
oref2 : reference to object;
iref : reference to if_oo_adt_classrun;
struc1 : zdemo_abap_tab1;
struc2 : include zdemo_abap_carr;
struc3 : include zdemo_abap_fli with suffix _in;
tab1 : string_table;
tab2 : xstring_table;
}bt_elem* represents components with elementary types using built-in DDIC typesbt_elem5 represents a type that requires additional specifications. The type alone is not possible since it is a currency field that must relate to a currency code field. The annotation can be added using an ADT quick fix.dtel_elem* represents components with elementary types using data elements. The data elements used are all released for ABAP for Cloud Development.dref* represents components with data references. The example covers references to built-in types, data elements and the generic type data. oref*/iref represents components with object/interface references. Random released classes/interfaces and the generic type object are used.struc* represent components with structured types. The example does not cover DDIC structures, but DDIC database tables. Tables from the ABAP cheat sheet repository are used. Two structures represent include structures. One of them is specified with suffix. The following would not be possible in the example case since both tables contain some identically named components that would prevent an activation: struc2: include zdemo_abap_carr;
struc3: include zdemo_abap_fli;tab* represents components with table types. The example uses released table types with elementary line types.Example: DDIC Table Type
The following example creates three DDIC table types exploring several options:
string_table. DATA a TYPE int1.
DATA b TYPE int2.
DATA c TYPE int4.
DATA d TYPE d16n.
DATA e TYPE d34n.
DATA f TYPE datn.
DATA g TYPE timn.
DATA h TYPE utcl.
"Respective built-in DDIC types cannot be specified directly
"in ABAP programs. So, the following data object declaration
"using a built-in DDIC type is not possible.
"DATA i type rawstring.
"Note: The following type definition emphasizes that local
"types hide global type names.
TYPES rawstring TYPE xstring.
DATA j TYPE rawstring.
"Programmatically retrieving information about which released APIs are available
"The following statement retrieves released data elements and domains.
SELECT ReleasedObjectType, ReleasedObjectName, ReleaseState
FROM i_apisforclouddevelopment
WHERE releasestate = 'RELEASED'
AND ( ReleasedObjectType = 'DTEL' OR ReleasedObjectType = 'DOMA' )
ORDER BY ReleasedObjectType, ReleasedObjectName
INTO TABLE @DATA(released_dtel_doma).abap_boolean data element. The released abap type pool provides abap_true ('X') and abap_false ('') to be used as constant values. "Using the data element abap_boolean as type in the data object declaration.
DATA is_true TYPE abap_boolean.
DATA(str) = `Hello#ABAP##World#`.
IF str CS `#`.
"Using abap_true/abap_false from the released abap type pool as constant values
is_true = abap_true.
ELSE.
is_true = abap_false.
ENDIF. DATA(some_string) = `Hello#ABAP##World#`.
"The RESULTS addition to the FIND statement can expect
"the table type match_result_tab that contains search results.
DATA findings TYPE match_result_tab.
FIND ALL OCCURRENCES OF `#` IN some_string RESULTS findings.
ASSERT lines( findings ) = 4.
"Dynamic method call and specifying a parameter table of
"type abap_parmbind_tab.
DATA(oref) = NEW zcl_demo_abap_objects( ).
"Creating parameter table
DATA(ptab) = VALUE abap_parmbind_tab( ( name = 'I_OP'
kind = cl_abap_objectdescr=>exporting
value = NEW i( 3 ) )
( name = 'R_TRIPLE'
kind = cl_abap_objectdescr=>returning
value = NEW i( ) ) ).
"The example method triplse the value specified for i_op.
CALL METHOD oref->('TRIPLE') PARAMETER-TABLE ptab.
ASSERT ptab[ name = 'R_TRIPLE' ]-value->* = 9.SELECT statements to retrieve database table entries into an internal table.string and rawstring.NOT NULL flag is always set for key fields, meaning they cannot have a null value.clnt, indicating the table is client-dependent. Tables in the delivery class A are generally client-dependent.curr (currency) or quan (quantity), require specific specifications and must have reference tables specified.| Specification | Purpose |
|---|---|
@EndUserText.label : 'Demo database table' | Providing a short description |
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE | Specifies whether the object can be enhanced Find more options using CTRL + Space after the # character. In this example, the table cannot be enhanced. |
@AbapCatalog.tableCategory : #TRANSPARENT | The default table category is transparent table The defined DDIC database table has an identically named variant with the same columns in the underlying database. |
@AbapCatalog.deliveryClass : #A | The delivery class controls the transport of table data in installations, upgrades, and so on. A stands for an application table for master and transaction data. |
@AbapCatalog.dataMaintenance : #RESTRICTED | Defines whether it is possible to display/maintain a database table or view its contents using tools. In the example, the table can be displayed using Data Preview in ADT. |
Example: DDIC Database Tables
The following example creates two DDIC database tables exploring several options:
@EndUserText.label : 'Demo DDIC database table'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zdemo_abap_tabl1 {
key client : abap.clnt not null;
key num : abap.int4 not null;
chars : abap.char(5);
id : zdemo_abap_dtel_pr;
flag : zdemo_abap_dtel_do;
str : abap.string(0);
cuky : abap.cuky;
@Semantics.amount.currencyCode : 'zdemo_abap_tabl1.cuky'
curr : abap.curr(8,2);
}abap.string(0); for a component with type string (and also rawstring) means to specify the maximum length of the component. 0 in abap.string(0); means there is no restriction, 256 in abap.string(256); means restricting the length to 256. This restriction is checked in read and write operations.include specification. In doing so, you can avoid an extra declaration of table fields. @EndUserText.label : 'Demo DDIC database table'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zdemo_abap_tabl2 {
key client : abap.clnt not null;
key key_field : abap.int4 not null;
include zdemo_abap_struc_flat;
}[!NOTE] - This cheat sheet only emphasizes ABAP CDS objects as global types to be used in ABAP. It does not cover use cases, data modeling aspects, annotations, syntax, and more. Refer to the documentation for the complete picture. The executable example of the CDS View Entities cheat sheet provides a demonstration of a selection of CDS-related syntax. - ABAP CDS objects are created in ADT from source code.
CDS built-in types...
abap.type, with optional or mandatory length and decimal specifications in parentheses. For example, abap.char(len) corresponds to the DDIC type char with a length specification.Example: CDS Simple Types
The following example creates CDS simple types:
@EndUserText.label: 'Demo CDS simple type'
define type ZDEMO_ABAP_CDS_TYPE : abap.char(5) @EndUserText.label: 'Demo CDS simple type'
define type ZDEMO_ABAP_CDS_TYPE : land1 @EndUserText.label: 'Demo CDS simple type'
define type ZDEMO_ABAP_CDS_TYPE : ZDEMO_ABAP_CDS_ST_BT"Using CDS simple types in ABAP, for example, to declare local
"types and data objects
"Elementary types/data objects
TYPES ty_cds_simple TYPE zdemo_abap_cds_type.
DATA dobj_w_simple_type TYPE zdemo_abap_cds_type.
"Typing a structure component with a CDS simple type
DATA: BEGIN OF struc_w_st,
comp1 type string,
comp2 type i,
comp3 type zdemo_abap_cds_type,
END OF struc_w_st.
"Internal table with elementary line type represented by the
"CDS simple type
DATA itab_w_st TYPE TABLE OF zdemo_abap_cds_type WITH EMPTY KEY.
"Retrieving type information using RTTI
DATA(tdo_cds_simple_type) = CAST cl_abap_elemdescr( cl_abap_typedescr=>describe_by_name( `ZDEMO_ABAP_CDS_TYPE` ) ).
DATA(type_category_cds_st) = tdo_cds_simple_type->kind.
DATA(type_kind_cds_st) = tdo_cds_simple_type->type_kind.
DATA(is_ddic_type_cds_st) = tdo_cds_simple_type->is_ddic_type( ).
"If you use abap.char(5) as type in the CDS simple type, the following data object contains X
TYPES c5 TYPE c LENGTH 5.
DATA(applies_to_data_cds_st) = tdo_cds_simple_type->applies_to_data( CONV c5( 'abcde' ) ).int1, int2, int4, char, and numc, with the last two having a maximum length of 8.[!NOTE] - Enumerated types can also be specified in ABAP using the syntax TYPES BEGIN OF ENUM. See examples in the Data Types and Data Objects cheat sheet. - Find more information about RTTI, which is used in this and the previous examples, in the Dynamic Programming cheat sheet.
Example: CDS Enumerated Types
The following example creates a CDS enumerated type:
@EndUserText.label: 'Demo CDS enumerated type'
define type ZDEMO_ABAP_CDS_ENUM : abap.char(1) enum
{
enum_constant_1 = initial;
enum_constant_2 = 'X';
enum_constant_3 = 'Y';
enum_constant_4 = 'Z';
}"Using CDS enumerated types in ABAP, for example, to declare
"local types and data objects
TYPES ty_cds_enum TYPE zdemo_abap_cds_enum.
DATA dobj_w_enum_type TYPE zdemo_abap_cds_enum.
"The technical data type of an enumerated value is the base type
"of the enumerated type. You can use the base type of an enumerated
"type in special conversions using the constructor operator CONV.
DATA dobj_c1 TYPE c LENGTH 1 VALUE 'Y'.
DATA(conv_enum_1) = CONV zdemo_abap_cds_enum( dobj_c1 ).
"An enumerated structure with the CDS enumerated type name is
"automatically declared. Therefore, you can access the constants
"with the component selector preceded by the CDS enumerated type
"name.
ASSERT conv_enum_1 = zdemo_abap_cds_enum-enum_constant_3.
"Using a value that is not available in the CDS enumerated type
dobj_c1 = 'A'.
TRY.
DATA(conv_enum_2) = CONV zdemo_abap_cds_enum( dobj_c1 ).
CATCH cx_sy_conversion_no_enum_value.
"This example raises an exception.
ENDTRY.
"Retrieving type information using RTTI
DATA(tdo_cds_enum) = CAST cl_abap_enumdescr( cl_abap_typedescr=>describe_by_name( `ZDEMO_ABAP_CDS_ENUM` ) ).
DATA(type_category_cds_enum) = tdo_cds_enum->kind.
DATA(type_kind_cds_enum) = tdo_cds_enum->type_kind.
DATA(is_ddic_type_cds_enum) = tdo_cds_enum->is_ddic_type( ).
DATA(base_kind_cds_enum) = tdo_cds_enum->base_type_kind.
DATA(members_cds_enum) = tdo_cds_enum->members."Creating structured types and data objects using ABAP cheat sheet
"repository objects
"CDS view entity
TYPES ty_struc_cds_ve TYPE zdemo_abap_fli_ve.
DATA dobj_struc_cds_ve TYPE zdemo_abap_fli_ve.
"CDS abstract entity
TYPES ty_struc_cds_abstr TYPE zdemo_abap_abstract_ent.
DATA dobj_struc_cds_abstr TYPE zdemo_abap_abstract_ent.
"CDS table function
TYPES ty_struc_cds_tabfunc TYPE zdemo_abap_table_function.
DATA dobj_struc_cds_tabfunc TYPE zdemo_abap_table_function.
"Using CDS entities as data souces in ABAP SQL statements
DATA itab_cds_ve TYPE TABLE OF zdemo_abap_fli_ve WITH EMPTY KEY.
DATA itab_cds_tabfunc TYPE TABLE OF zdemo_abap_table_function WITH EMPTY KEY.
SELECT * FROM zdemo_abap_fli_ve INTO TABLE @itab_cds_ve.
SELECT * FROM zdemo_abap_table_function INTO TABLE @itab_cds_tabfunc.[!NOTE] - Some of the CDS entities can also be used as data sources in ABAP SQL statements, some cannot (for example, CDS custom entities). - DDIC-based views are obsolete and should not be used anymore.
In an SAP BTP ABAP environment and using ADT, you can find released repository objects in the Project Explorer view under Released Objects:

Programmatically retrieving information about which repository objects are released and available in the system:
SELECT ReleasedObjectType, ReleasedObjectName, ReleaseState
FROM i_apisforclouddevelopment
WHERE releasestate = 'RELEASED'
ORDER BY ReleasedObjectType, ReleasedObjectName
INTO TABLE @DATA(released_apis).Using the XCO library, you can retrieve ABAP repository objects information and create the objects programmatically. The executable example (zcl_demo_abap_cloud_excursion) of the ABAP for Cloud Development cheat sheet includes demo code snippets. The Released ABAP Classes cheat sheet also includes a small selection of code snippets in that context. For more information, refer to the documentation.
[!NOTE] - The executable example uses both the created repository objects as described in this cheat sheet and some repository objects of the ABAP cheat sheet repository. The example explores and uses code snippets of this cheat sheet, emphasizing the use of global types in ABAP programs. - Disclaimer
Expand the following collapsible section for example code. To try it out, create a demo class named zcl_demo_abap. If it already exists, reuse it. Otherwise, create a new class with a different name. Paste the code into it. If you choose a different class name, update the class name in the code snippet accordingly. After activation, choose F9 in ADT to execute the class. The example is set up to display output in the console.
🟢 Click to expand for example code
CLASS zcl_demo_abap DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_demo_abap IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
"Populating demo database tables from the ABAP cheat sheet repository
zcl_demo_abap_aux=>fill_dbtabs( ).
"---------------------------------------------------
"------------ Built-in dictionary types ------------
"---------------------------------------------------
"Built-in dictionary types are not directly usable in ABAP,
"except for types specified in ABAP SQL.
"The following example shows miscellaneous typed literals in
"an ABAP SQL statement. Typed literals can be specified in read
"positions where host variables are possible.
DATA(tmstamp) = CONV timestamp( '20240808112517' ).
DATA(str) = `Some string`.
SELECT SINGLE
FROM zdemo_abap_fli
FIELDS
carrid,
@str AS host_var,
char`X` AS flag,
int8`32984723948723` AS int8,
raw`11` AS raw,
numc`1234` AS numc,
utclong`2024-01-01T10:01:02,2` AS utc,
tims`101507` AS tims,
curr`173.95` AS curr,
"Multiple cast expressions splitting a time stamp into date and time parts
CAST( CAST( div( @tmstamp, 1000000 ) AS CHAR ) AS DATS ) AS date,
CAST( substring( CAST( @tmstamp AS CHAR ), 9, 6 ) AS TIMS ) AS time,
"Untyped literal
'ABAP' AS txt
WHERE fldate = datn`20240102`
INTO @DATA(misc_typed_literals).
out->write( data = misc_typed_literals name = `misc_typed_literals` ).
"-----------------------------------------------------------
"--------------- DDIC data elements/domains ----------------
"-----------------------------------------------------------
"Using DDIC data elements for local types and data objects
"Data element based on a built-in dicitionary type
TYPES ty_dtel1 TYPE zdemo_abap_dtel_pr.
DATA char3_dtel TYPE zdemo_abap_dtel_pr.
"Data element based on a domain
TYPES ty_dtel2 TYPE zdemo_abap_dtel_do.
DATA char1_dtel TYPE zdemo_abap_dtel_do.
"Data element based on a reference type
TYPES ty_dtel3 TYPE zdemo_abap_dtel_ref.
DATA char10_dtel_ref TYPE zdemo_abap_dtel_do.
"Using a data element in an explicit conversion
DATA(char3_dtel_inl) = CONV zdemo_abap_dtel_pr( '1234567890' ).
out->write( data = char3_dtel_inl name = `char3_dtel_inl` ).
"Using RTTI to demonstrate the type mapping of built-in dicitionary
"types (that serve as the types of the DDIC data elements) to ABAP types
"The example data element ZDEMO_ABAP_DTEL_PR is typed with numc, length 5.
DATA(tdo_a) = CAST cl_abap_elemdescr( cl_abap_typedescr=>describe_by_name( 'ZDEMO_ABAP_DTEL_PR' ) ).
DATA(type_kind_a) = tdo_a->type_kind.
DATA(output_length_a) = tdo_a->output_length.
TYPES n5 TYPE n LENGTH 5.
DATA(applies_a) = tdo_a->applies_to_data( CONV n5( '12345' ) ).
"The example data element ZDEMO_ABAP_DTEL_PR is based on a domain. The type used is char, length 1.
DATA(tdo_b) = CAST cl_abap_elemdescr( cl_abap_typedescr=>describe_by_name( 'ZDEMO_ABAP_DTEL_DO' ) ).
DATA(type_kind_b) = tdo_b->type_kind.
DATA(output_length_b) = tdo_b->output_length.
TYPES c1 TYPE c LENGTH 1.
DATA(applies_b) = tdo_b->applies_to_data( CONV c1( 'A' ) ).
out->write( data = type_kind_a name = `type_kind_a` ).
out->write( data = output_length_a name = `output_length_a` ).
out->write( data = applies_a name = `applies_a` ).
out->write( data = type_kind_b name = `type_kind_b` ).
out->write( data = output_length_b name = `output_length_b` ).
out->write( data = applies_b name = `applies_b` ).
"-----------------------------------------------------------
"-------------------- DDIC structures ----------------------
"-----------------------------------------------------------
"Using DDIC structures to declare local types and data objects
TYPES ty_struc_flat TYPE zdemo_abap_struc_flat.
TYPES ty_struc_deep TYPE zdemo_abap_struc_deep.
DATA flat_struc1 TYPE ty_struc_flat.
DATA flat_struc2 TYPE zdemo_abap_struc_flat.
DATA deep_struc1 TYPE ty_struc_deep.
DATA deep_struc2 TYPE zdemo_abap_struc_deep.
"Populating the demo structures
"Flat structure
flat_struc1 = VALUE #( chars = 'abc'
num = 123
cuky = 'EUR'
curr = '123.45'
id = '98765'
flag = 'X' ).
"Deep structures including structures
"Addressing the components of nested structures via the structure name
"Note struc3: The include structure component in the DDIC structure is specified
"with a suffix. When addressing the components via the structure name, the suffix
"is not needed. Check the F2 information, which lists the individual components
"with suffix.
deep_struc2 = VALUE #( bt_elem1 = 'abcde'
bt_elem2 = 123
"...
dref1 = NEW #( 'abc' )
"...
struc2 = VALUE #( carrid = 'XY' carrname = 'XY Airways' )
struc3 = VALUE #( carrid = 'YZ' connid = '1234' )
"...
).
"Addressing the components of included structures individually
DATA(deep_struc3) = VALUE zdemo_abap_struc_deep( bt_elem1 = 'abcde'
bt_elem2 = 123
"...
dref1 = NEW #( 'abc' )
"...
"Components referring to struc2
carrid = 'XY'
carrname = 'XY Airways'
"Components referring to struc3 (with suffixes)
carrid_in = 'YZ'
connid_in = '1234'
"...
).
DATA(comp_val1) = deep_struc2-carrid.
DATA(comp_val2) = deep_struc2-struc2-carrid.
DATA(comp_val3) = deep_struc2-struc3-carrid.
ASSERT comp_val1 = comp_val2.
ASSERT comp_val1 <> comp_val3.
DATA(comp_val4) = deep_struc3-carrid.
DATA(comp_val5) = deep_struc3-struc2-carrid.
DATA(comp_val6) = deep_struc2-struc3-carrid.
DATA(comp_val7) = deep_struc3-carrid_in.
ASSERT comp_val4 = comp_val5.
ASSERT comp_val6 = comp_val7.
ASSERT comp_val4 <> comp_val6.
"----------------------------------------------------
"------------------ DDIC table types ----------------
"----------------------------------------------------
"Declaring internal tables and local table types based on
"DDIC table types
TYPES ty_tab_elem TYPE zdemo_abap_tt_str.
TYPES ty_tab_struc TYPE zdemo_abap_tt_so.
DATA tab_elem1 TYPE ty_tab_elem.
DATA tab_elem2 TYPE zdemo_abap_tt_str.
DATA tab_struct1 TYPE ty_tab_struc.
DATA tab_struct2 TYPE zdemo_abap_tt_so.
"Using RTTI to check whether the technical properties of the demo table
"types are identical to those of the released table type string_table
DATA(applies) = CAST cl_abap_tabledescr( cl_abap_typedescr=>describe_by_name( 'ZDEMO_ABAP_TT_STR' ) )->applies_to_data( VALUE string_table( ) ).
ASSERT applies = 'X'.
"Using RTTI to retrieve table keys
"The example table type zdemo_abap_tt_so is specified with a primary table with components
"and secondary table key
DATA(tdo_table_type) = CAST cl_abap_tabledescr( cl_abap_typedescr=>describe_by_name( 'ZDEMO_ABAP_TT_SO' ) ).
DATA(table_keys) = tdo_table_type->get_keys( ).
out->write( data = table_keys name = `table_keys` ).
"The example also specifies an alias name
DATA(table_key_aliases) = tdo_table_type->get_key_aliases( ).
out->write( data = table_key_aliases name = `table_key_aliases` ).
"-------------------------------------------------------
"-------------- Predefined types in DDIC ---------------
"-------------------------------------------------------
"Multiple predefined domains and data elements are available in the system
"The following example demonstrates data elements for built-in dictionary types.
"In these cases, the data elements have the same names as the built-in dictionary
"types.
DATA a TYPE int1.
DATA b TYPE int2.
DATA c TYPE int4.
DATA d TYPE d16n.
DATA e TYPE d34n.
DATA f TYPE datn.
DATA g TYPE timn.
DATA h TYPE utcl.
"Respective built-in DDIC types cannot be specified directly
"in ABAP programs. So, the following data object declaration
"using a built-in DDIC type is not possible.
"DATA i type rawstring.
"Note: The following type definition emphasizes that local
"types hide global type names.
TYPES rawstring TYPE xstring.
DATA j TYPE rawstring.
"Programmatically retrieving information about which released APIs are available
"The following statement retrieves released data elements and domains.
SELECT ReleasedObjectType, ReleasedObjectName, ReleaseState
FROM i_apisforclouddevelopment
WHERE releasestate = 'RELEASED'
AND ( ReleasedObjectType = 'DTEL' OR ReleasedObjectType = 'DOMA' )
ORDER BY ReleasedObjectType, ReleasedObjectName
INTO TABLE @DATA(released_dtel_doma).
"Truth values
"Using the data element abap_boolean as type in the data object declaration.
DATA is_true TYPE abap_boolean.
DATA(some_string) = `Hello#ABAP##World#`.
IF some_string CS `#`.
"Using abap_true/abap_false from the released abap type pool as constant values
is_true = abap_true.
ELSE.
is_true = abap_false.
ENDIF.
out->write( data = is_true name = `is_true` ).
"Many predefined data types are used in operand positions of ABAP statements
"The RESULTS addition to the FIND statement can expect
"the table type match_result_tab that contains search results.
DATA findings TYPE match_result_tab.
FIND ALL OCCURRENCES OF `#` IN some_string RESULTS findings.
ASSERT lines( findings ) = 4.
out->write( data = findings name = `findings` ).
"Dynamic method call and specifying a parameter table of
"type abap_parmbind_tab.
DATA(oref) = NEW zcl_demo_abap_objects( ).
"Creating parameter table
DATA(ptab) = VALUE abap_parmbind_tab( ( name = 'I_OP'
kind = cl_abap_objectdescr=>exporting
value = NEW i( 3 ) )
( name = 'R_TRIPLE'
kind = cl_abap_objectdescr=>returning
value = NEW i( ) ) ).
"The example method triplse the value specified for i_op.
CALL METHOD oref->('TRIPLE') PARAMETER-TABLE ptab.
ASSERT ptab[ name = 'R_TRIPLE' ]-value->* = 9.
out->write( data = ptab name = `ptab` ).
"-------------------------------------------------------
"------------------ DDIC database tables ---------------
"-------------------------------------------------------
"Clearing demo database tables
DELETE FROM zdemo_abap_tabl1.
DELETE FROM zdemo_abap_tabl2.
"DDIC database tables represent structued types.
DATA struc_from_dbtab TYPE zdemo_abap_tabl1.
struc_from_dbtab = VALUE #( num = 1
chars = 'abcde'
id = '12345'
flag = 'X'
str = `ABAP`
cuky = 'EUR'
curr = '1.2' ).
MODIFY zdemo_abap_tabl1 FROM @struc_from_dbtab.
DATA itab_from_dbtab TYPE TABLE OF zdemo_abap_tabl1 WITH EMPTY KEY.
SELECT * FROM zdemo_abap_tabl1 INTO TABLE @itab_from_dbtab.
out->write( data = itab_from_dbtab name = `itab_from_dbtab` ).
"The following example DDIC database table specifies an included
"DDIC structure
DATA itab_from_dbtab2 TYPE TABLE OF zdemo_abap_tabl2 WITH EMPTY KEY.
itab_from_dbtab2 = VALUE #( ( key_field = 1
chars = 'abcde'
num = 2
cuky = 'EUR'
curr = '1.2'
id = '12345'
flag = 'X' )
( key_field = 3
chars = 'fghij'
num = 4
cuky = 'USD'
curr = '3.4'
id = '67890'
flag = 'A' )
( key_field = 5
flag = 'Y' ) ).
MODIFY zdemo_abap_tabl2 FROM TABLE @itab_from_dbtab2.
CLEAR itab_from_dbtab2.
SELECT * FROM zdemo_abap_tabl2 INTO TABLE @itab_from_dbtab2.
out->write( data = itab_from_dbtab2 name = `itab_from_dbtab2` ).
"----------------------------------------------------
"------------------ CDS simple types ----------------
"----------------------------------------------------
"Using CDS simple types in ABAP, for example, to declarie local
"types and data objects
"Elementary types/data objects
TYPES ty_cds_simple TYPE zdemo_abap_cds_type.
DATA dobj_w_simple_type TYPE zdemo_abap_cds_type.
"Typing a structure component with a CDS simple type
DATA: BEGIN OF struc_w_st,
comp1 TYPE string,
comp2 TYPE i,
comp3 TYPE zdemo_abap_cds_type,
END OF struc_w_st.
"Internal table with elementary line type represented by the
"CDS simple type
DATA itab_w_st TYPE TABLE OF zdemo_abap_cds_type WITH EMPTY KEY.
"Retrieving type information using RTTI
DATA(tdo_cds_simple_type) = CAST cl_abap_elemdescr( cl_abap_typedescr=>describe_by_name( `ZDEMO_ABAP_CDS_TYPE` ) ).
DATA(type_category_cds_st) = tdo_cds_simple_type->kind.
DATA(type_kind_cds_st) = tdo_cds_simple_type->type_kind.
DATA(is_ddic_type_cds_st) = tdo_cds_simple_type->is_ddic_type( ).
"If you use abap.char(5) as type in the CDS simple type, the following data object contains X.
TYPES c5 TYPE c LENGTH 5.
DATA(applies_to_data_cds_st) = tdo_cds_simple_type->applies_to_data( CONV c5( 'abcde' ) ).
out->write( data = type_category_cds_st name = `type_category_cds_st` ).
out->write( data = type_kind_cds_st name = `type_kind_cds_st` ).
out->write( data = is_ddic_type_cds_st name = `is_ddic_type_cds_st` ).
out->write( data = applies_to_data_cds_st name = `applies_to_data_cds_st` ).
"----------------------------------------------------
"------------------ CDS enumerated types ------------
"----------------------------------------------------
"Using CDS enumerated types in ABAP, for example, to declare
"local types and data objects
"The example CDS enumerated type's base type is specified with abap.char(1).
TYPES ty_cds_enum TYPE zdemo_abap_cds_enum.
DATA dobj_w_enum_type TYPE zdemo_abap_cds_enum.
"The technical data type of an enumerated value is the base type
"of the enumerated type. You can use the base type of an enumerated
"type in special conversions using the constructor operator CONV.
DATA dobj_c1 TYPE c LENGTH 1 VALUE 'Y'.
DATA(conv_enum_1) = CONV zdemo_abap_cds_enum( dobj_c1 ).
ASSERT conv_enum_1 = zdemo_abap_cds_enum-enum_constant_3.
out->write( data = conv_enum_1 name = `conv_enum_1` ).
"Using a value that is not available in the CDS enumerated type
dobj_c1 = 'A'.
TRY.
DATA(conv_enum_2) = CONV zdemo_abap_cds_enum( dobj_c1 ).
CATCH cx_sy_conversion_no_enum_value INTO DATA(enum_error).
out->write( data = enum_error->get_text( ) name = `enum_error->get_text( )` ).
ENDTRY.
"Retrieving type information using RTTI
DATA(tdo_cds_enum) = CAST cl_abap_enumdescr( cl_abap_typedescr=>describe_by_name( `ZDEMO_ABAP_CDS_ENUM` ) ).
DATA(type_category_cds_enum) = tdo_cds_enum->kind.
DATA(type_kind_cds_enum) = tdo_cds_enum->type_kind.
DATA(is_ddic_type_cds_enum) = tdo_cds_enum->is_ddic_type( ).
DATA(base_kind_cds_enum) = tdo_cds_enum->base_type_kind.
DATA(members_cds_enum) = tdo_cds_enum->members.
"Although abap.char(1) is used as base type in the example CDS enumerated type,
"the following data object is not type compliant.
DATA(applies_to_data_cds_enum1) = tdo_cds_enum->applies_to_data( abap_true ).
ASSERT applies_to_data_cds_enum1 IS INITIAL.
DATA(applies_to_data_cds_enum2) = tdo_cds_enum->applies_to_data( CONV zdemo_abap_cds_enum( 'X' ) ).
out->write( data = type_category_cds_enum name = `type_category_cds_enum` ).
out->write( data = type_kind_cds_enum name = `type_kind_cds_enum` ).
out->write( data = is_ddic_type_cds_enum name = `is_ddic_type_cds_enum` ).
out->write( data = base_kind_cds_enum name = `base_kind_cds_enum` ).
out->write( data = members_cds_enum name = `members_cds_enum` ).
out->write( data = applies_to_data_cds_enum1 name = `applies_to_data_cds_enum1` ).
out->write( data = applies_to_data_cds_enum2 name = `applies_to_data_cds_enum2` ).
"Some demo assertions regarding the example CDS enumerated type, which is usable as
"enumerated structure in ABAP.
FIELD-SYMBOLS <fs_enum> TYPE data.
LOOP AT CAST cl_abap_enumdescr( cl_abap_typedescr=>describe_by_name( `ZDEMO_ABAP_CDS_ENUM` ) )->members INTO DATA(enum_comps).
"Dynamically assigning the enumerated constant to a field symbol
ASSIGN zdemo_abap_cds_enum-(enum_comps-name) TO <fs_enum>.
CASE enum_comps-name.
WHEN 'ENUM_CONSTANT_1'.
ASSERT <fs_enum> = zdemo_abap_cds_enum-enum_constant_1.
ASSERT <fs_enum> = CONV zdemo_abap_cds_enum( '' ).
ASSERT enum_comps-value = ''.
WHEN 'ENUM_CONSTANT_2'.
ASSERT <fs_enum> = zdemo_abap_cds_enum-enum_constant_2.
ASSERT <fs_enum> = CONV zdemo_abap_cds_enum( 'X' ).
ASSERT enum_comps-value = 'X'.
WHEN 'ENUM_CONSTANT_3'.
ASSERT <fs_enum> = zdemo_abap_cds_enum-enum_constant_3.
ASSERT <fs_enum> = CONV zdemo_abap_cds_enum( 'Y' ).
ASSERT enum_comps-value = 'Y'.
WHEN 'ENUM_CONSTANT_4'.
ASSERT <fs_enum> = zdemo_abap_cds_enum-enum_constant_4.
ASSERT <fs_enum> = CONV zdemo_abap_cds_enum( 'Z' ).
ASSERT enum_comps-value = 'Z'.
ENDCASE.
ENDLOOP.
"----------------------------------------------------
"----- CDS entities usable as structured types ------
"----------------------------------------------------
"Several CDS entities represent structured types that can be
"used in ABAP. The example shows the creation of local data
"types and objects based on CDS entities. Demo artifacts from
"the ABAP cheat sheet repository are used.
"CDS view entity
TYPES ty_struc_cds_ve TYPE zdemo_abap_fli_ve.
DATA dobj_struc_cds_ve TYPE zdemo_abap_fli_ve.
"CDS abstract entity
TYPES ty_struc_cds_abstr TYPE zdemo_abap_abstract_ent.
DATA dobj_struc_cds_abstr TYPE zdemo_abap_abstract_ent.
"CDS table function
TYPES ty_struc_cds_tabfunc TYPE zdemo_abap_table_function.
DATA dobj_struc_cds_tabfunc TYPE zdemo_abap_table_function.
"Using CDS view entities and CDS table functions as data souces
"in ABAP SQL statements
DATA itab_cds_ve TYPE TABLE OF zdemo_abap_fli_ve WITH EMPTY KEY.
DATA itab_cds_tabfunc TYPE TABLE OF zdemo_abap_table_function WITH EMPTY KEY.
SELECT * FROM zdemo_abap_fli_ve INTO TABLE @itab_cds_ve.
SELECT * FROM zdemo_abap_table_function INTO TABLE @itab_cds_tabfunc.
out->write( data = itab_cds_ve name = `itab_cds_ve` ).
out->write( data = itab_cds_tabfunc name = `itab_cds_tabfunc` ).
ENDMETHOD.
ENDCLASS.26_ABAP_Dictionary.md。版权归原作者所有。