很多同学不太清楚ABAP语言中的基础数据类型(ABAP Type),本篇文章总结了,在ABAP语言中,预定义的数据类型以及其初始值和长度。
总的来说,在ABAP语言中,系统预定义了以下这些数据类型:
b, c, d, decfloat16, decfloat34, f, i, int8, n, p, s, string, t, x, xstring.
按照种类的不同,这些数据类型可以分为4大类:
- Numeric Types - 数字类型
- Character-like Types - 字符类类型
- Byte Types - 字节类型
- Date Types and Time Types - 时间和日期类型
1. Numeric types(数字类型)
用途:用于处理数字类型的数据
Type | Length | Standard Length | Name | value range | initial value |
---|---|---|---|---|---|
b | 1 byte | 1-byte integer (internal) | 0 to 255 | 0 | |
s | 2 byte | 2-byte integer (internal) | -32,768 to +32,767 | 0 | |
i | 4 byte | 4-byte integer | -2,147,483,648 to +2,147,483,647 | 0 | |
int8 | 8 byte | 8-byte integer | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 | 0 | |
p | 1 to 16 bytes | 8 byte | Packed number | (-10^(2len-1) +1) / (10^(+dec)) to (+10^(2len-1) -1) /(10^(+dec)) | 0 |
decfloat16 | 8 byte | Decimal floating point number with 16 places | Valid values are numbers between 1E385(1E-16 - 1) and -1E-383 for the negative range, 0 and +1E-383 to 1E385(1 - 1E-16) for the positive range. | 0 | |
decfloat34 | 16 byte | Decimal floating point number with 34 places | Valid values are numbers between 1E6145(1E-34 - 1) and -1E-6143 for the negative range, 0 and +1E-6143 and 1E6145(1 - 1E-34) for the positive range. | 0 | |
f | 8 byte | Binary floating point number with 17 places | Valid values are numbers between -1.7976931348623157E+308 and -2.2250738585072014E-308 for the negative range and between +2.2250738585072014E-308 and +1.7976931348623157E+308 for the positive range, plus 0. | 0 |
用法举例:
DATA: num1 TYPE i,
num2 TYPE i,
result TYPE decfloat34.
...
result = num1 / num2.
2. Character-Like Types(字符类类型)
用途:用于处理字符和字符串类型的数据
Type | Length | Default Length | Name | value range | initial value |
---|---|---|---|---|---|
c | 1 to 262,143 characters | One character | Text field | Any alphanumeric characters | " " for every place |
n | 1 to 262,143 characters | One character | Numeric text field | Any alphanumeric characters, but the only valid values are the digits 0 to 9 | “0” for every place |
string | Variable | Text string | As for type c | Empty string with length 0 |
用法举例:
DATA: flag TYPE c LENGTH 1,
html TYPE string.
3. Byte-Like Types(字节类型)
用途:用于字节类型的数据
Type | Length | Default Length | Name | value range | initial value |
---|---|---|---|---|---|
x | 1 to 524,287 bytes | 1 byte | Byte field | Any byte values, hexadecimal 00 to FF | Hexadecimal 00 |
xstring | Variable | Byte string | As for type x | Empty string with length 0 |
用法举例:
DATA hex TYPE xstring.
hex = cl_abap_codepage=>convert_to( `...` ).
4. Date Types and Time Types(日期和时间类型)
用途:用于日期和时间类型的数据
Type | Length | Default Length | Name | value range | initial value |
---|---|---|---|---|---|
d | 8 characters | Date field | t “yyyymmdd”: “yyyy” (year): 0001 to 9999, “mm” (month): 01 to 12, “dd” (day): 01 to 31 | “00000000” | |
t | 6 characters | Time field | “hhmmss”. “hh” (hours): 00 to 23, “mm” (minutes): 00 to 59, “ss” (seconds): 00 to 59. | “000000” |
用法举例:
DATA: tomorrow TYPE d,
next_hour TYPE t.
DATA(today) = sy-datlo.
DATA(now) = sy-timlo.
tomorrow = today + 1.
next_hour = ( now + 3600 ) / 3600 * 3600.
5. 数据字典中的内置数据类型(Built-In Data Types)
ABAP中的基础数据类型,可以在程序中直接使用。
基于ABAP中的基本数据类型,SAP在数据字典SE11中提供了一系列的内置数据类型,我们可以使用这些内置的数据类型,来定义更加有业务含义的类型,这也便是domain的data element的概念了。
SAP在数据字典中的内置数据类型(built-in data type),是基于基础数据类型(abap type) 定义的。
在ABAP的数字字典中,提供了37种预制类型。下图展示了Domain ----- 预制数据类型-----基础数据类型建的关系。
数据字典中的37种预制数据类型,可分为以下几类。
5.1 Numeric Types相关的
Type | Valid Places m | Decimal Places n | Initial Value | Meaning | ABAP Type |
---|---|---|---|---|---|
INT1 | 3 | - | 0 | 1-byte integer, 0 to 255 | b |
INT2 | 5 | - | 0 | 2-byte integer, -32,768 to 32,767 | s |
INT4 | 10 | - | 0 | 4-byte integer, -2,147,483,648 to +2,147,483,647 | i |
INT8 | 19 | - | 0 | 8-byte integer, -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 | int8 |
DEC | 1-31 | 0-m, maximum 14 | 0 | Packed number in BCD format | p, length m DIV 2 + 1, decimal places n |
DF16_DEC | 1-15 | 0-m, maximum 14 | 0 | Decimal floating point number stored in BCD format | decfloat16 |
DF16_RAW | 16 | - | 0 | Decimal floating point number stored in binary format | decfloat16 |
DF34_DEC | 1-31 | 0-m, maximum 14 | 0 | Decimal floating point number stored in BCD format | decfloat34 |
DF34_RAW | 34 | - | 0 | Decimal floating point number stored in binary format | decfloat34 |
FLTP | 16 | 16 | 0 | Binary floating point number | f |
5.2 Character-Like Types相关的
Type | Valid Places m | Initial Value | Meaning | ABAP Type |
---|---|---|---|---|
CHAR | 1-30000, maximum of 1333 for table fields | m blanks | Character string | c, length m |
LCHR | 256-32000 | None | long character string | c, length m |
SSTRING | 1-1333 | Empty string | Character string | string |
STRING | 256-… | Empty string | Character string (CLOB) | string |
5.3 Byte-Like Types相关的
Type | Valid Places m | Initial Value | Meaning | ABAP Type |
---|---|---|---|---|
RAW | 1-32000 maximum of 255 for table fields | None | Byte string | x, length m |
LRAW | 256-32000 | None | Long byte string | x, length m |
RAWSTRING | 256-… | Empty string | Byte string (BLOB) | xstring |
5.4 Date Types/Time Types相关的
Type | Valid Places m | Initial Value | Meaning | ABAP Type |
---|---|---|---|---|
DATS | 8 | 00000000 | Date in the format YYYYMMDD | d |
TIMS | 6 | 000000 | Time in the format HHMMSS | t |
ACCP | 6 | 6 blanks | Posting period in the format YYYYMM | n, length 6 |
5.5 一些有特定含义的Character-Like Types:
Type | Valid Places m | Initial Value | Meaning | ABAP Type |
---|---|---|---|---|
NUMC | 1-255 | m zeroes | Numeric text | n, length m |
CLNT | 3 | 000 | Client | c, length 3 |
LANG | 1 | Blank | Language key | c, length 1 |
5.6 金额、数量相关的类型(Currency Fields and Quantity Fields)
Type | Valid Places m | Decimal Places n | Initial Value | Meaning | ABAP Type |
---|---|---|---|---|---|
CURR | 1-31 | 1-m, maximum 14 | 0 | Currency field in BCD format | p, length m DIV 2 + 1, decimal places n |
CUKY | 5 | - | 5 blanks | Currency key for currency fields | c, length 5 |
QUAN | 1-31 | 0-m, maximum 14 | 0 | Quantity field in BCD format | p, length m DIV 2 + 1, decimal places n |
UNIT | 2-3 | - | 2 or 3 blanks | Unit key of a quantity field | c, length m |
希望在你对ABAP数据类型有疑问的时候,本篇博客可以帮助到你 😃
本博客专注于技术分享,干货满满,持续更新。 欢迎关注❤️、点赞👍、转发📣!