Wednesday, June 10, 2009

Data Types in the ABAP Dictionary

The ABAP Dictionary allows you to define global data types. You can use the TYPE addition of an appropriate ABAP statement to refer to these data types in any ABAP program in the system.

You define these data types using the Structure linkABAP Dictionary. The following input fields are relevant to data types:

This graphic is explained in the accompanying text

There are three groups on the initial screen:

Database Tables and Views

One of the most important tasks of the Structure linkABAP Dictionary is to administer database tables in the R/3 database. The Dictionary contains meta-descriptions of the database tables, and uses these to create the physical tables in the database. A view is a "virtual table" containing fields from one or more tables.

In the description of a database table, the table lines consist of single fields or columns. An elementary data type must be assigned to each column. The elementary types in the ABAP Dictionary are data elements. Like data objects in ABAP programs, database tables and views have data types as attributes. A line of a database table or view has the data type of a flat structure, which consists of individual data elements.

In ABAP programs, you can use the TYPE addition with the data type of a database table or view. You may refer to the whole structure or to individual components:

... TYPE ...

refers to the complex data type of the structure,

... TYPE -i> ...

refers to the elementary data type of component i>.

If you define a complex data type as a structure using

TYPES TYPE .

the components of the data type inherit the names of the components of the database table or view, and can be addressed in the program using -i>.

To ensure compatibility with previous releases, you can still use the LIKE addition to refer to database tables or views, except within classes. The reason for this is that in earlier releases, the physical presence of the database tables as objects was emphasized, even though the Dictionary only contains meta-descriptions and data types.

Defining program-local data types by referring to database tables and views is one of the essential techniques for processing data from database tables in ABAP. Data objects that you define in this way always have the right type to contain data from the corresponding database table. ABAP Open SQL allows you to read a single field, a range of fields, or an entire database table or view into an internal table.

Example

TYPES: city type spfli-cityfrom,
spfli_type TYPE STANDARD TABLE OF spfli WITH DEFAULT KEY.

DATA: wa_city TYPE city,
wa_spfli TYPE spfli_type.

...

SELECT SINGLE cityfrom FROM spfli
INTO wa_city
WHERE carrid = 'LH' AND connid = '400'.

...

SELECT * FROM spfli INTO TABLE wa_spfli.

...

This example defines an elementary data type CITY that refers to a single field of the database table SPFLI and an internal table SPFLI_TYPE, whose line type is the same as the structure of the database table. The SELECT statement reads data from the database into the corresponding data objects.

Data types

Data types are the actual type definitions in the ABAP Dictionary. They allow you to define elementary types, reference types, and complex types that are visible globally in the system. The data types of database tables are a subset of all possible types, namely flat structures. Global object types (classes and interfaces) are not stored in the ABAP Dictionary, but in the class library. You create them using the Class Builder.

For a detailed description of data types and their definitions, refer to the Structure linkTypes section of the ABAP Dictionary documentation. The following descriptions mention the types only briefly, along with how you can refer to them from ABAP programs.

Data Elements

Data elements in the ABAP Dictionary describe individual fields. They are the smallest indivisible units of the complex types described below, and are used to specify the types of columns in the database. Data elements can be elementary types or reference types.

  • Elementary Types

Elementary types are part of the dual-level domain concept for fields in the ABAP Dictionary. The elementary type has semantic attributes, such as texts, value tables, and documentation, and has a data type. There are two different ways to specify a data type:

    • By directly assigning an ABAP Dictionary type.

You can assign a predefined ABAP Dictionary type and a number of characters to an elementary type. The ABAP Dictionary has considerably more predefined types than the ABAP programming language. The number of characters here is not the field length in bytes, but the number of valid characters excluding formatting characters. The data types are different because the predefined data types in the ABAP Dictionary have to be compatible with the external data types of the database tables supported by the R/3 System.

When you refer to data types from the ABAP Dictionary in an ABAP program, the predefined Dictionary types are converted to ABAP types as follows:

Dictionary type

Meaning

Maximum length n

ABAP type

DEC

Calculation/amount field

1-31, 1-17 in tables

P((n+1)/2)

INT1

Single-byte integer

3

Internal only

INT2

Two-byte integer

5

Internal only

INT4

Four-byte integer

10

I

CURR

Currency field

1-17

P((n+1)/2)

CUKY

Currency key

5

C(5)

QUAN

Amount

1-17

P((n+1)/2)

UNIT

Unit

2-3

C(n)

PREC

Accuracy

16

Internal only

FLTP

Floating point number

16

F(8)

NUMC

Numeric text

1-255

N(n)

CHAR

Character

1-255

C(n)

LCHR

Long character

256-max

C(n)

STRING.

String of variable length

1-max

STRING.

RAWSTRING

Byte sequence of variable length

1-max

XSTRING

DATS

Date

8

D

ACCP

Accounting period YYYYMM

6

N(6)

TIMS

Time HHMMSS

6

T

RAW

Byte sequence

1-255

X(n)

LRAW

Long byte sequence

256-max

X(n)

CLNT

Client

3

C(3)

LANG

Language

internal 1, external 2

C(1)

("max" in LCHR and LRAW is the value of a preceding INT2 field. The "internal" length of a LANG field is in the Dictionary, the "external" length refers to the display on the screen.

    • Assigning a domain

The technical attributes are inherited from a domain. Domains are standalone Repository objects in the ABAP Dictionary. They can specify the technical attributes of a data element. One domain can be used by any number of data elements. When you create a domain, you must specify a Dictionary data type (see above table) and the number of characters.

  • Reference Types

Reference types describe single fields that can contain references to global classes and interfaces from the ABAP class library.

In an ABAP program, you can use the TYPE addition to refer directly to a data element. The predefined Dictionary data types of the domain are then converted into the corresponding ABAP types.

If you define a local data type in a program by referring to a data element as follows:

TYPES TYPE .

the semantic attributes of the data element are inherited and will be used, for example, when you display a data object with type on the screen. Since all data types in the ABAP Dictionary are based on data elements, they all contain the corresponding semantic attributes.

Example

TYPES company TYPE s_carr_id.

DATA wa_company TYPE company.

wa_company = 'UA '.

WRITE: 'Company:', wa_company.

This example defines a local type COMPANY that refers to the data element S_CARR_ID. The data element is linked to the identically-named domain S_CARR_ID. The domain defines the technical attributes as data type CHAR with length 3. The local data type COMPANY in the program therefore has the ABAP type C(3). COMPANY also adopts the semantic attributes of the data element. In the above example, we declare a data object WA_COMPANY with this type and display it on a list. If the user chooses F1 help for the output field, the help text stored in the ABAP Dictionary will appear in a dialog box.

This graphic is explained in the accompanying text

structures

A structure is a sequence of any other data types from the ABAP Dictionary, that is, data elements, structures, table types, or database tables. When you create a structure in the ABAP Dictionary, each component must have a name and a data type.

In an ABAP program, you can use the TYPE addition to refer directly to a structure.

If you define a local data type in a program by referring to a structure as follows:

TYPES TYPE .

the construction blueprint of the structure is used to create a local structure in the program. The predefined Dictionary data types of the domains used by the data elements in the structure are converted into the corresponding ABAP types. The semantic attributes of the data elements are used for the corresponding components of the structure in the program. The components of the local structure have the same names as those of the structure in the ABAP Dictionary.

To ensure compatibility with previous releases, it is still possible to use the LIKE addition in an ABAP program to refer to a structure in the ABAP Dictionary (except in classes).

Example

Suppose the structure STRUCT is defined as follows in the ABAP Dictionary:

Field name

Type name

Description

COL1

CHAR01

Character field with length 1

COL2

CHAR08

Character field with length 8

COL3

CHAR10

Character field with length 10

The types CHAR01 to CHAR10 are data elements with corresponding domains. We can refer to this structure in ABAP:

TYPES struct_type TYPE struct.

DATA wa TYPE struct_type.

wa-col1 = '1'.
wa-col2 = '12345678'.
wa-col3 = '1234567890'.

This program creates a local structure in the program - STRUCT_TYPE - and a corresponding data object WA. We can address the components using the component names from the original structure.

Table Types

Table types are construction blueprints for internal tables that are stored in the ABAP Dictionary. When you create a table type in the ABAP Dictionary, you specify the line type, access type, and key. The line type can be any data type from the ABAP Dictionary, that is, a data element, a structure, a table type, or the type of a database table. You can also enter a predefined Dictionary type directly as the line type, in the same way that you can with a domain.

In an ABAP program, you can use the TYPE addition to refer directly to a table type.

If you define a local data type in a program by referring to a table type as follows:

TYPES TYPE

.

the construction blueprint of the table type is used to create a local internal table in the program. The predefined Dictionary data types of the domains used by the data elements in the structure are converted into the corresponding ABAP types. The semantic attributes of the data elements are used for the corresponding components of the internal table in the program.

Example

Suppose the table type STRUCT_TABLE is defined in the Dictionary with the line type STRUCT from the previous example. We can refer to this in ABAP:

TYPES table_type TYPE struct_table.

DATA: table_wa TYPE table_type,
line_wa LIKE LINE OF table_wa.

...

LOOP AT table_wa INTO line_wa.
...
WRITE: line_wa-col1, line_wa-col1, line_wa-col1.
...
ENDLOOP.

This program defines an internal table type TABLE_TYPE. From it, we define data objects TABLE_WA and LINE_WA. LINE_WA corresponds to the line type of the table type in the Dictionary, and is therefore compatible with the structure STRUCT.

Type Groups

Before Release 4.5A, it was not possible to define standalone types in the ABAP Dictionary to which you could refer using a TYPE addition in an ABAP program. It was only possible to refer to flat structures. Structures in programs corresponded to the structures of database tables or structures in the ABAP Dictionary. In ABAP programs, you could only refer to database tables and structures in the ABAP Dictionary using LIKE. It was, however, possible to refer to individual components of the Dictionary type. Complex local data types such as internal tables or deep structures had no equivalent in the ABAP Dictionary. The solution to this from Release 3.0 onwards was to use type groups. Type groups were based on the include technique, and allowed you to store any type definitions globally in the Dictionary by defining them using TYPES statements.

The definition of a type group is a fragment of ABAP code which you enter in the ABAP Editor. The first statement for the type group is always:

TYPE-POOL .

After this came the definitions of data types using the TYPES statement, as described in Local Data Types in Programs. It was also possible to define global constants using the CONSTANTS statement. All the names of these data types and constants must begin with the name of the type group and an underscore:

In an ABAP program, you must declare a type group as follows before you can use it:

TYPE-POOLS .

This statement allows you to use all the data types and constants defined in the type group in your program. You can use several type groups in the same program.

Example

Let the type group HKTST be created as follows in the ABAP Dictionary:

TYPE-POOL hktst.

TYPES: BEGIN OF hktst_typ1,
col1(10) TYPE c,
col2 TYPE i,
END OF hktst_typ1.

TYPES hktst_typ2 TYPE p DECIMALS 2.

CONSTANTS hktst_eleven TYPE i VALUE 11.

This type group defines two data types HKTST_TYP1 and HKTST_TYP2, as well as a constant HKTST_ELEVEN with the value 11.

Any ABAP program can use this definition with the TYPE-POOLS statement:

TYPE-POOLS hktst.

DATA: dat1 TYPE hktst_typ1,
dat2 TYPE hktst_typ2 VALUE '1.23'.

WRITE: dat2, / hktst_eleven.

The output is:

1,23

11

The data types defined in the type group are used to declare data objects with the DATA statement and the value of the constant is, as the output shows, known in the program.

Leaving content frame

No comments:

Blog Archive