ABAP has the types a business system needs. That amounts and quantities have a type designed for exact decimal arithmetic says a lot about the language.
Built-in types
| Type | Content | Default length | Initial value |
|---|---|---|---|
| C | Fixed-length text | 1 | Space |
| N | Numeric text, zero padded | 1 | '0' |
| D | Date, YYYYMMDD | 8 | '00000000' |
| T | Time, HHMMSS | 6 | '000000' |
| I | Integer, 4 bytes | โ | 0 |
| INT8 | Integer, 8 bytes | โ | 0 |
| P | Packed decimal | 8 | 0 |
| F | Floating point | 8 | 0 |
| STRING | Variable-length text | Variable | Empty |
| XSTRING | Variable-length bytes | Variable | Empty |
N compared with C
DATA lv_num TYPE n LENGTH 5.
DATA lv_chr TYPE c LENGTH 5.
lv_num = 42. " gives '00042' โ zero padded
lv_chr = 42. " gives '42 ' โ space paddedDocument numbers and material codes in SAP are usually N fields, which is why they display with leading zeros.
Declaring variables
" Built-in type directly
DATA lv_name TYPE c LENGTH 30.
DATA lv_amount TYPE p LENGTH 8 DECIMALS 2.
" Referencing a Data Dictionary type (preferred)
DATA lv_matnr TYPE matnr.
" Referencing a table field
DATA lv_bukrs TYPE bkpf-bukrs.
" With an initial value
DATA lv_count TYPE i VALUE 10.
" Constant
CONSTANTS lc_status TYPE c LENGTH 1 VALUE 'A'.Structures
TYPES: BEGIN OF ty_item,
matnr TYPE matnr,
maktx TYPE maktx,
menge TYPE i,
END OF ty_item.
DATA ls_item TYPE ty_item.
ls_item-matnr = 'MAT-001'.
" A Dictionary structure
DATA ls_mara TYPE mara.Internal table types
DATA lt_items TYPE STANDARD TABLE OF ty_item.
DATA lt_mara TYPE STANDARD TABLE OF mara.
DATA lt_sorted TYPE SORTED TABLE OF ty_item
WITH UNIQUE KEY matnr.Inline declarations
SELECT SINGLE * FROM mara INTO @DATA(ls_mara) WHERE matnr = @lv_matnr.
LOOP AT lt_items INTO DATA(ls_item).
WRITE: / ls_item-matnr.
ENDLOOP.
DATA(lt_numbers) = VALUE int_tab( ( 1 ) ( 2 ) ( 3 ) ).Declaring at the point of use keeps declarations near the logic that needs them and narrows variable scope, which makes code easier to read.