This blog post explains the new ABAP CDS enumerations.
Release info
- 2308 SAP BTP ABAP Environment
- ABAP Release 7.58
- SAP S/4HANA 2023
Creating a CDS enumerated type
A template for enumerated types is also available.
Syntax
A CDS enumerated type is defined in the ABAP development tools for Eclipse (ADT) as a CDS type definition with the following syntax:
Explanation
- The CDS enumerated type can have header annotations that add domain-specific logic.
- The name of the CDS enumerated type EnumType is defined after the DEFINE TYPE
- Defining a base type BaseType is mandatory. The following data types are possible as base type: INT1, INT2, INT4, CHAR with length 1 to 8, NUMC with length 1 to 8.
- Enumerated constants and enumerated values are defined in a list in curly brackets.
- You can add text labels and headings to the enumerated constants. This allow you to define longer, more user-friendly names for user interfaces.
- Exactly one of the enumerated constants must have the enumerated value INITIAL. It generates the initial value of the base type.
Example
Definition of an enumerated type DEMO_CDS_ENUM_WEEKDAY. The base type is abap.int1 and the enumerated values of the enumerated constants MON, TUE, … are 0 to 6.
@EndUserText.label: 'Days of the week'
define type DEMO_CDS_ENUM_WEEKDAY : abap.int1 enum
{
@EndUserText.label: 'Monday'
MON = initial;
@EndUserText.label: 'Tuesday'
TUE = 1;
@EndUserText.label: 'Wednesday'
WED = 2;
@EndUserText.label: 'Thursday'
THU = 3;
@EndUserText.label: 'Friday'
FRI = 4;
@EndUserText.label: 'Saturday'
SAT = 5;
@EndUserText.label: 'Sunday, bloody Sunday'
SUN = 6;
}
In ABAP, an equivalent enumerated type can be defined as follows:
"ABAP enumerated type with enumerated structure
TYPES:
BEGIN OF ENUM abap_wd STRUCTURE abap_wd BASE TYPE int1,
mon,
tue,
wed,
thu,
fri,
sat,
sun,
END OF ENUM abap_wd STRUCTURE abap_wd.
But please note that you of course cannot make assignments between the two enumerated types, because an enumerated type is compatible only with itself.
Differences between ABAP enums and CDS enums
- In ABAP, the base type is optional. If no base type is specified, the default is i.
- In ABAP, the enumerated values are optional. If no values are specified, the enumerated constants are numbered implicitly, starting from 0.
- A CDS enumerated type behaves like an enumerated structure in ABAP. This means that a CDS enumerated type is always addressed by its name plus the name of an enumerated constant, for example: EnumName-EnumConstant in ABAP and EnumName.#EnumConstant in ABAP CDS.
Domains with fixed values and CDS enums
In ABAP Dictionary, you can define domains with fixed values. The fixed values are a set of allowed values, similar to the enumerated constants. However, the domain fixed values are evaluated for the input help of dynpro fields. CDS enumerated types make enumerations globally available and reusable in different contexts. CDS enumerated types are intended to replace domains with fixed values.
Using CDS enumerated types
CDS enumerated types can be used as follows:
- In ABAP CDS for typing and casting, as operands in expressions, and in comparisons.
- In ABAP for typing after the TYPES statement.
- In ABAP SQL as elementary operands and in cast expressions.
Using CDS enumerated types in ABAP
The enumerated type can be used for declaring an enumerated variable wd in an ABAP program. By doing so, an enumerated structure named demo_cds_enum_weekday is implicitly declared in the current context and can be used there. wd initially contains the content of the initial enumerated constant demo_cds_enum_weekday-mon.
DATA wd TYPE demo_cds_enum_weekday.
ASSERT wd = demo_cds_enum_weekday-mon.
cl_demo_output=>display( demo_cds_enum_weekday ).
The result looks as follows:
Using CDS enumerated types in ABAP CDS
The CDS enumerated type can be used in CDS entities for typing of elements or parameters, as operands in expressions, and in comparisons.
Here’s an example:
@EndUserText.label: 'Cast to enum'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view entity demo_cds_enum1
with parameters
p_weekday :DEMO_CDS_ENUM_WEEKDAY
as select from DEMO_CDS_ENUM_2
{
key id,
int1,
cast(int1 as DEMO_CDS_ENUM_WEEKDAY) as weekday1,
DEMO_CDS_ENUM_CHAR.#first_value as EnumConstant
}
where
weekday = $parameters.p_weekday
The elements weekday and EnumConstant both have the data type ENUM. You can see this in the debugger and in the code element information in ADT.
Using CDS enumerated types in ABAP SQL
In ABAP SQL, CDS enumerated constants can be used in operand positions such as the WHERE clause and the ORDER BY clause. The enumerated constants of a CDS enumerated type can be accessed with a component selector: EnumName-EnumConstant.
The following code snippet demonstrates accessing the CDS view entity using ABAP SQL. It first inserts data into the database table DEMO_DDIC_TYPES and then it selects all elements from the CDS view entity. The columns weekday and EnumConstant of the inline declared table result both have an enumerated type.
Note: If the field INT1 contains a value that is not contained in the list of enumerated values of the CDS enumerated type DEMO_CDS_ENUM_WEEKDAY, a runtime error occurs.
*fill database table
DELETE FROM demo_ddic_types.
INSERT demo_ddic_types FROM TABLE @( VALUE #(
( id = 'A' int1 = 1 )
( id = 'B' int1 = 6 )
*( id = 'C' int1 = 7 ) -> runtime error because 7 is not an allowed value
) ).
*SELECT from cds view entity
SELECT *
FROM demo_cds_enum_2
INTO TABLE @DATA(result)
WHERE EnumConstant = @demo_cds_enum_char-first_value.
*display result
cl_demo_output=>display( result ).
The result looks as follows:
The following screenshot shows the debugger view of the variable result. You can see that the columns Weekday and EnumConstant of the result table declared inline both have an enumerated type.
Outlook
It is currently not possible to define database tables with enumerated fields. The ENUM data type is not available for typing in ABAP Dictionary. However, the ABAP CDS team is currently preparing a new CDS entity as a successor to DDIC database tables, called CDS table entity. The fields of CDS table entities can have the data type ENUM.
No comments:
Post a Comment