
Classic ABAP offers untyped literals , which are interpreted as either character literals or as numeric literals. The new kids on the block are typed literals. They are available in almost all data types, but are only supported as operands in ABAP SQL and ABAP CDS. This blog post provides an overview of literals in ABAP at one glance. Literals are part of the basic ABAP ABC, and ABAP developers should be familiar with them.
Definition
A literal is a direct, character-like specification of a value in the ABAP source code of an ABAP program or in a CDS object. There are two types of literals: typed and untyped literals. Typed literals specify their data type explicitly, while untyped literals only specify a value and the data type is derived implicitly.
The classic ABAP programming language offers only untyped literals.
Type of literal | Details |
Numeric literal |
|
Character literal |
|
For all other data types, the ABAP Assignment and Conversion Rules are applied, which may lead to unexpected results.
The literal operator & can be used to concatenate literals. It is available in ABAP and ABAP SQL, but not in ABAP CDS.
Untyped literals in ABAP SQL are the same as ABAP literals, and the same rules apply.
As of ABAP release 7.80 (quarterly release), 7.55 (on-prem), ABAP SQL also supports typed literals:
Type of literal | Syntax |
Integer literal |
|
Packed number literal |
|
Decimal floating point literal |
|
Binary floating point literal |
|
Text field literal |
|
Text string literal |
|
Byte field literal |
|
Byte string literal |
|
Numeric text literal |
|
Client literal |
|
Language key literal |
|
Date literal |
|
Time literal |
|
Time stamp literal |
|
Currency field literal |
|
Currency key literal |
|
Quantity field literal |
|
Unit key literal |
|
Example:
The following example uses typed literals of types UTCLONG, DATN, and INT8 in different operand positions.
SELECT SINGLE
FROM demo_expressions
FIELDS
utcl_add_seconds( utclong`2020-04-01T12:01:01,2`,50 ) AS utcl,
datn_add_months( datn`17890101`,15 ) AS add_months,
int8`9999999999999999` AS int8
INTO @DATA(result).
Without typed literals, there would be the following problems:
The value of typed literals is checked at compile time. A value that is not within the value range of the specified data type results in a syntax check error.
Example:
The following typed literal specifies a value that is not valid for the data type UTCLONG. A syntax check error occurs at compile time.
...
utcl_add_seconds( utclong`2020-13-01T12:01:01,2`,50 ) AS utcl
...
The following error message occurs:
In contrast, untyped literals in classic ABAP are checked at runtime, and incorrect values lead to runtime errors. Typed literals are therefore more convenient for developers.
Similar to classic ABAP, classic CDS DDIC-based views offer untyped numeric and untyped character literals:
Type of literal | Details |
Character literal |
|
Numeric literal |
|
As of ABAP release 7.83 (quarterly release), 7.56 (on-prem), typed literals are also available in ABAP CDS. They are supported in CDS view entities and in CDS hierarchies.
Type of literal | Syntax |
Integer literal |
|
Packed number literal |
|
Decimal floating point literal |
|
Binary floating point literal |
|
Text field literal |
|
Text string literal |
|
Byte field literal |
|
Byte string literal |
|
Numeric text literal |
|
Client literal |
|
Language key literal |
|
Date literal |
|
Time literal |
|
Time stamp literal |
|
Currency field literal |
|
Currency key literal |
|
Quantity field literal |
|
Unit key literal |
|
Example
The following example uses typed literals of types INT1, DEC, DECFLOAT34, and D16N in different operand positions.
@EndUserText.label: 'CDS view entity, numeric typed literals'
define view entity DEMO_CDS_NUMERIC_TYPED_LIT
as select from demo_ddic_types
{
key id,
abap.int1'1' as int1,
abap.dec'.15' as dec_literal,
abap.decfloat34'123.456E789' * 100 as ArithmField
}
where
d16n = abap.d16n'123.45'
Typed literals make life easier for developers and reduce they the potential for errors. Unfortunately, they are not available in the classic ABAP programming language, but only in ABAP SQL and ABAP CDS.
All of this information can also be found in the ABAP Keyword Documentation. This blog post only highlights the topic for those who are interested.
Docu:
You can acquire more skills on the modern ABAP programming available on SAP BTP and SAP S/4HANA in the SAP Learning Journeys, such as
More role-based learning resources and opportunities to get certified are available in one place on the SAP Learning site: SAP Learning
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
3 | |
3 | |
3 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 |