TYPES is a keyword used in SAP ABAP programming.This tutorial covers its introduction & syntax details.
TYPES
Variants
1. TYPES typ.
2. TYPES typ(len).
3. TYPES: BEGIN OF rectyp,
…
END OF rectyp.
Effect
The
TYPES statement introduces user-defined data types . As with standard
data types, you can use them when creating data objects and when
assigning types to formal parameters and field symbols. User-defined
data types are an essential component of the ABAP/4 type concept .
Variant 1
TYPES f.
Additions
1. … TYPE typ1
2. … LIKE f
3. … TYPE typ1 OCCURS n
4. … LIKE f OCCURS n
5. … TYPE LINE OF itabtyp
6. … LIKE LINE OF itab
7. … DECIMALS n
Effect
Creates a new type. If the TYPE addition is not used, the new type points to the standard type C .
The
type name typ can be up to 30 characters long. Apart from the special
characters ‘(‘, ‘)’, ‘+’, ‘.’, ‘,’, ‘:’, ‘-‘, ‘<' and '>‘, you
can use any characters. Numbers are allowed, but the name cannot
consist of numbers alone.
Recommendations for type names:
Always use a letter as the first character.
Use the underscore as the link in multiple word names (e.g. NEW_PRODUCT ).
Addition 1
… TYPE typ1
Effect
Defines
the new type with the type typ1 . typ1 can be one of the predefined
types specified below or a type you define yourself with TYPES .
The length (SL) of the type typ is the same as the type typ1 .
Type Description Std len. Initial value
C Text (character) 1 Blank
N Numeric text 1 ’00…0′
D Date (YYYYMMDD) 8 ‘00000000’
T Time (HHMMSS) 6 ‘000000’
X Hexadecimal 1 X’00’
I Whole number (integer) 4 0
P Packed number 8 0
F Floating point number 8 ‘0.0’
Example
TYPES NUMBER TYPE I.
This defines the type NUMBER NUMBER with the type I . It can then be used in the program.
Notes
The
data type I is the whole number type for the hardware you are using.
Its value range is -2**31 to 2**31-1 (-2.147.483.648 to 2.147.483.647).
While
type P is used for money amount fields, you should use type I for
number fields, index fields, position specifications, etc.
Apart
from zero, type F allows you to display positive and negative numbers
in the range from 1E-307 to 1E+307 with 15 decimal places. (The ABAP/4
processor uses the floating point operations of the relevant hardware
and does not attempt to standardize them.) Floating point literals must
be enclosed in quotation marks. The standard output length is 22.
Input in type F fields can be formatted differently:
Decimal number with or without sign, with or without decimal point.
In
the form E, where the mantissa is a decimal number and the exponent can
be specified with or without a sign. (Examples of floating point
literals: ‘1’, ‘-12.34567’, ‘-765E-04’, ‘1234E5’, ‘+12E+34’,
‘+12.3E-4’, ‘1E160’).
Floating point arithmetic is fast on our
hardware platforms. It is ideal when you require a large value range
and can take rounding errors into account when making calculations.
Such rounding errors can occur when converting from external (decimal)
format to internal format (base 2 or 16) or vice-versa (see ABAP/4
number types ).
Addition 2
… LIKE f
Effect
Defines the type typ with the type of the field f . f may be a database field or an already defined internal field.
Example
TYPES TABLE_INDEX_TYP LIKE SY-TABIX.
The type TABLE_INDEX_TYP now points to the type of the field SY-TABIX (index for internal tables).
Note
This
addition is useful in a number of cases, since any field type changes
are automatically known to the program. Also, any unnecessary and
unwanted conversions are not performed.
Addition 3
… TYPE typ1 OCCURS n
Effect
Defines
the type of an internal table without a header line. An internal table
without a header line consists of any number of table lines that have
the same structure as that specified by TYPE .
You fill and process an internal table with statements such as APPEND , READ TABLE , LOOP and SORT .
The
OCCURS parameter n specifies how many table lines of storage is
required. This storage reservation process does not happen until the
first line is inserted in the table. The value n of the OCCURS
specification has no effect on type checking, i.e. data objects which
have types with different OCCURS specifications are type-compatible.
Example
TYPES: TAB_TYPE TYPE I OCCURS 20.
DATA: TAB TYPE TAB_TYPE,
TAB_WA TYPE I.
TAB_WA = 1.
APPEND TAB_WA TO TAB.
TAB_WA = 2.
APPEND TAB_WA TO TAB.
The internal table TAB now consists of two table entries.
Addition 4
… LIKE f OCCURS n
Effect
Defines
the type of an internal table without a header line. This table
consists of any number of table lines which have the structure
specified by the data object f . Processing is the same as for addition
3.
Example
DATA: BEGIN OF PERSON,
NAME(20),
AGE TYPE I,
END OF PERSON.
TYPES TYPE_PERSONS LIKE PERSON OCCURS 20.
DATA PERSONS TYPE TYPE_PERSONS.
PERSON-NAME = ‘Michael’.
PERSON-AGE = 25.
APPEND PERSON TO PERSONS.
PERSON-NAME = ‘Gabriela’.
PERSON-AGE = 22.
APPEND PERSON TO PERSONS.
The internal table PERSONS now consists of two table entries.
Addition 5
… TYPE LINE OF itabtyp
Effect
The
specified type itabtyp must be the type of an internal table with or
without a header line. The statement creates a type corresponding to
the line type of the specified table type.
Example
TYPES TAB_TYP TYPE I OCCURS 10.
TYPES MY_TYPE TYPE LINE OF TAB_TYP.
The type MY_TYPE now has the same attributes as a line of the table type TAB_TYP and is thus type I .
Addition 6
… LIKE LINE OF itab
Effect
The
data object itab must be an internal table with or without a header
line. The statement defines a type which corresponds to the line type
of the specified table.
Example
DATA TAB TYPE I OCCURS 10.
TYPES MY_TYPE LIKE LINE OF TAB.
The type MY_TYPE now has the same attributes as the line type of the table TAB and thus has the type I .
Addition 7
… DECIMALS n
Effect
This
addition only makes sense with the field type P . When making
calculations and outputting data, a field of this type has n decimal
places. n must be a value between 0 and 14.
Normally, the
attribute for fixed point arithmetic is set with newly created
programms. If you switch this attribute off, the DECIMALS
-specification is taken into account on output, but not when making
calculations. In this case, the programmer must take care that the
decimal point is in the right place by multiplying or dividing (COMPUTE
) by the appropriate power of ten.
When making calculations, you
should always have fixed point arithmetic switched on. Then, even
intermediate results (division!) are calculated with the greatest
possible accuracy (31 decimal places).
To decide whether the fixed point type P or the floating point type F is more suitable, see also “ABAP/4 number types “.
Variant 2
TYPES typ(len).
Additions
Similar to variant 1
Effect
Creates the type typ with the length len .
This
variant should only be used with the types C , N , P and X . Other
types can only be created in the standard length (see table under
effect of variant 1).
The permitted lengths depend on the type being pointed to:
Type Permitted lengths
C 1 – 65535
N 1 – 65535
P 1 – 16
X 1 – 65535
Note
For
each byte, you can display one character, two decimal digits or two
hexadecimal digits. With P fields, one place is reserved for the
leading sign, so that a P field of the length 3 can contain 5 digits,
while an X field of the length 3 can contain 6 digits. Both have an
output length of 6.
Variant 3
TYPES: BEGIN OF rectyp,
…
END OF rectyp.
Effect
Defines
the field string type rectyp by grouping together all fields of the
type rectyp defined between ” BEGIN OF rectyp ” and ” END OF rectyp “.
Each name is prefixed by ” rectyp- “.
Example
TYPES: BEGIN OF PERSON,
NAME(20) TYPE C,
AGE TYPE I,
END OF PERSON.