Since ABAP release 7.80, ABAP SQL offers typed literals. Typed literals cover far more data types than untyped literals, they clearly define their data type and type compatibility is checked in your IDE during design time. Overall, typed literals reduce the complexity of your coding. Start using them now!
A typed literal is a literal whose data type is defined by specifying an ABAP Dictionary type explicitly.
datatype specifies the required data type. Almost all built-in ABAP Dictionary types are possible: INT1, INT2, INT4, INT8, DEC, DECFLOAT16, DF16_DEC, DF16_RAW, DECFLOAT34, DF34_DEC, DF34_RAW, FLTP, CHAR, SSTRING, STRING, RAW, RAWSTRING, DATN, DATS, TIMN, TIMS, UTCLONG, NUMC, CLNT, LANG, CURR, CUKY, QUAN, UNIT.
<literal> specifies the value in single backquotes. The value must match the data type specified under datatype. Otherwise, a syntax check error occurs.
The maximum length of the content of a typed literal is 255 and this can be achieved only by using the literal operator &. This literal operator can be used in typed literals of all categories and data types to concatenate two literals of the same type.
Example: int8`123` & `456`.
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:
- Untyped literals are available only as character literals of type c or string, or as numeric literals of type i or p. The data types UTCLONG and DATN are not available as untyped literals. The ABAP SQL cast expression is also not possible in the shown example, because UTCLONG and DATN are no valid target types.
- There are no numeric untyped literals of type INT8. Without a typed literal, 9999999999999999 would be interpreted as packed number.
With typed literals, you can specify type-compatible literals. This makes literals available in more operand positions than before, with fewer type conversions being required.
Hopefully, this blog post could demonstrate how typed literals can reduce the complexity of your coding and make your code simpler, shorter, and more precise. Typed literals cover almost all built-in ABAP Dictionary data types and therefore, can be used in far more operand positions. They explicitly and unequivocally define their data type and thus reduce ambiguity. Finally, they reduce the risk of runtime errors since type compatibility is checked during design time.
Currently, typed literals are available only in ABAP SQL, not in the ABAP Programming Language.
For further details, see the ABAP Keyword Documentation, topic ABAP SQL – Typed Literals.