All ABAP programs can define their own data types. Within a program,procedures can also define local types.
You define local data types in a program using the
TYPES <t> ... [TYPE <type>|LIKE <obj>] ...
statement. The type name <t> may be up to 30 characters long. You can use any letters, digits, and theunderscore character. Do not create a name consisting entirely of numeric characters. You cannot use the special characters + . , : ( ) - < >. Other special characters are reserved for internal use. You cannot use the names of the predefined ABAP types (C, D, F, I, N, P, T, X, STRING, XSTRING) or the name of the generic type TABLE. You should not use names that are the same as an ABAP keyword or addition. You should:
You declare local data types in a program either by referring to an existing data type or constructing a new type.
An existing type can be
Known types must bevisible at the point where you define the new type. If the existing type is generic, you can use further additions to set the attributes of type <t> that are still undefined.
Elementary Data Types
Local elementary data types in a program are defined with reference to predefined elementary types. You use the following syntax:
TYPES <t>[(<length>)] [TYPE <type>|LIKE <obj>] [DECIMALS <dec>].
<type> is one of the predefined ABAP types C, D, F, I, N, P, T, X, STRING, or XSTRING, an existing elementary local type in the program, or a data element defined in the ABAP Dictionary. When you refer to a data element from the ABAP Dictionary, the system converts it into an elementary ABAP type. If you use a LIKE reference, <obj> can be an existing dataobject with an elementary data type.
If you do not use the TYPE or LIKE addition, the system uses the default predefined type C. If <type> is one of the generic elementary predefined types with fixed length (C, N, P, X), you should set a length using the <length> option. If you omit it, the field length is set to the relevant initial value in the table in thePredefined ABAP Types section. If <type> is P, you can specify the number of decimal places using the DECIMALS addition. If you omit this, the number of decimal places is set to 0.
Thus the implicit statement
defines a character variable <f> with length 1. It is a shortened form of the explicit statement
TYPES <t>(1) TYPE C.
However, you should always use the explicit statement. The short form is prohibited within ABAP Objects classes.
Elementary local data types in a program make your programs easier to read and understand. If you have used such a type to defineseveral data objects, you can change the type of all of those objects in one place, just be changing the definition in the TYPES statement. Alternatively, if you use a set of data types regularly in different programs but do not want to store them in the ABAP Dictionary, you can create an include program for the type definitions, and incorporate this into the relevant programs.
TYPES: number TYPE i,
length TYPE p DECIMALS 2,
code(3) TYPE c.
In this example, a data type called NUMBER is defined. It is the same as the predefined data type I, except it has a different name to make the program easier to read.
The program defines a data types LENGTH, based on the generic ABAP type P. LENGTH is defined with a given number of decimals. If it becomes necessary to change the accuracy of length specifications, for example, you only have to change the TYPES statement in the program.
A third data type, CODE, is also defined. CODE is based on the predefined generic ABAP type C. The length is defined as 3 bytes.
DATA counts TYPE i.
TYPES: company TYPE spfli-carrid,
no_flights LIKE counts.
This example shows how you can use the TYPE addition to refer to a column (CARRID) of a database table (SPFLI). The LIKE addition refers to an existing data object.
You can define reference types locally in your programs or globally in the ABAP Dictionary. You use the following syntax:
TYPES <t> TYPE REF TO ...
After TYPE, there is no reference to an existing data type. Instead, the type constructor occurs:
REF TO DATA
declares a reference <t> to a data object. Fields with type <t> can contain references (pointers) to data objects, that is, instances of data types (see alsoData References).
REF TO <class>|<interface>
defines a reference type <t> to the class <class> or interface <interface>. Fields with type <t> can contain references (pointers) to instances of the class <class> or of classes that implement the interface <interface> (see alsoObject Handling).
Complex local data types in programs always consist of the above elementary data types or reference types. In this case, the TYPES statement is a construction blueprint for the complex data type.
When you refer to a known complex type using
TYPES <t> [TYPE <type>|LIKE <obj>].
the new complex type is constructed using the known type as a template. When you refer to the complex ABAP Dictionary types structure or table type, which are based on ABAP Dictionary dataelements , the structure from the Dictionary is used, and the data elements are converted into elementary ABAP types.
The TYPES statement allows you to define new complex data types without referring to existing structures and tables.
To construct a new structure type in a program, use the following chained TYPES statement:
TYPES: BEGIN OF <structure>,
END OF <structure>.
This chained statement creates a structure containing all of the variables between
TYPES BEGIN OF <structure>.a nd TYPES END OF <structure>.
that occur in thedata types defined in
TYPES <ti>... .
The components <fi> can be elementary types, reference types, or, if you refer to known complex types, complex themselves. TYPES BEGIN OF... TYPES END OF blocks can also be nested, so you can create deep structure types.
The individual variables within a structure type are addressed in the program with a hyphen between the structure name and component name as follows: <structure>-<fi>.
TYPES: spfli_type TYPE spfli,
surname(20) TYPE c,
BEGIN OF address,
name TYPE surname,
street(30) TYPE c,
city TYPE spfli_type-cityfrom,
END OF address,
town TYPE address-city.
This example shows the definition of two structure types in a program - SPFLI_TYPE and ADDRESS. The structure of the data type SPFLI_TYPE is taken from the database table SPFLI in the ABAP Dictionary. The components of SPFLI_TYPE are the same as the columns of SPFLI. The individual data types of the components are the ABAP equivalents of the data types of the columns of the database table. The structure type ADDRESS is newly defined. The component ADDRESS-NAME takes the data type of the previously-defined type SURNAME, the component ADDRESS-STREET is newly-defined, ADDRESS-CITY takes the data type of column CITYFROM of the structure type SPFLI_TYPE.
TYPES: BEGIN OF struct1,
col1 TYPE i,
BEGIN OF struct2,
col1 TYPE i,
col2 TYPE i,
END OF struct2,
END OF struct1.
TYPES mytype TYPE struct1-struct2-col2.
The example shows how you can construct a nested structure type STRUCT1 with a complex component STRUCT2 by nesting TYPES BEGIN OF ... TYPES END OF blocks, and how you can address the inner components.
* local types in program
* referring to predefined ABAP types:
TYPES: surname(20) TYPE c,
street(30) TYPE c,
zip_code(10) TYPE n,
city(30) TYPE c,
phone(20) TYPE n,
date LIKE sy-datum.
* local structure in program
* referring to the above types
TYPES: BEGIN of address,
name TYPE surname,
code TYPE zip_code,
town TYPE city,
str TYPE street,
END OF address.
* local nested structure in program
* referring to the above types
TYPES: BEGIN of phone_list,
adr TYPE address,
tel TYPE phone,
END OF phone_list.
This example shows how to create complex data types from simple type definitions. After a set of simple data types are created with ABAP predefined types, a structured type ADDRESS is defined using the data types defined earlier. Finally, a nested structure type, PHONE_LIST, is created, whose first component has the type ADDRESS.
Local tables in a program are called internal tables. To construct a new internal table type, use the syntax:
TYPES <t> TYPE|LIKE <tabkind> OF <linetype> [WITH <key>].
After TYPE, there is no reference to an existing data type. Instead, the type constructor occurs: The type constructor
<tabkind> OF <linetype> [WITH <key>]
defines an internal table type with access type <tabkind>, line type <linetype> and key <key>. The line type <linetype> can be any known data type. Specifying the key is optional. Internal tables can thus be generic. For more information, refer toInternal Tables.