10.3 Data Types
By using the Data Types radio button in the ABAP Data Dictionary initial screen, we can create global data types such as data elements, structures, and table types. You’ve seen how to create these global types at various points in previous chapters, so this section will focus on the additional options that can be maintained for these global types centrally and can then be inherited into programs automatically.
10.3.1 Data Elements
A data element describes an elementary data type or a reference data type. It allows you to maintain both technical and semantic attributes. Semantic attributes are significant for screen fields that refer to this data element. For example, the screen field can automatically import input help ((F4) help) or field help ((F1) help) from a data element. For ABAP data objects that refer to the data element, the semantic attributes hold no significance.
For an elementary data type, technical specifications are derived from a domain or directly maintained in the data element. Similarly, for reference data types, a reference to a global class or interface can be maintained or a reference to a predefined type can be maintained.
Figure 10.52 shows the Data Type tab of the data element, where the technical attributes can be maintained by selecting the relevant radio button (i.e., Elementary Type, Predefined Type, or Reference Type).
There are various semantic attributes that can be maintained for the data elements. Under the Data Type tab and in the application toolbar of the Dictionary: Change Data Element page, the following attributes can be maintained:
-
Short text
Short text can be maintained for the data element under the Data Type tab. The short text appears as a header in the field texts for screen fields and table/structure fields. -
Documentation
Documentation can be maintained for a data element, to be shown automatically as (F1) help for screen fields that refer to this data element. To maintain documentation, click the Documentation button in the application toolbar.
A documentation status can be set by following the menu path GoTo • Documentation • Status. This path will open a window with self-explanatory options, as shown in Figure 10.53. -
Supplementary Documentation
Additional supplementary documentation can be created for the data element to be be assigned to specific program screen fields as field help. Each piece of supplementary documentation is assigned a four-digit number. Click the Supplementary Documentation button in the application toolbar to maintain the supplementary documentation.
Under the Further Characteristics tab, as shown in Figure 10.54, the following options can be maintained:
-
Search Help
A search help can be assigned to a data element with an elementary data type; it will be available automatically as (F4) help to screen fields that refer to this data element. A search help can’t be assigned to a reference type. -
Parameters
A parameter that is passed to the search help is entered in this field. The (F4) help for this field can be used to select the parameter based on the assigned search help. -
Parameter ID
The data element can be linked with SAP memory using the SPA/GPA parameter ID. SAP memory is the memory area that is accessible by all SAP GUI sessions in the user session. SAP memory can be used to exchange data between programs running in different sessions (GUI windows). You’ll learn more about memory organization in Chapter 13. The Parameter ID is only used if the data element is an elementary data type. The screen fields that refer to this data element are automatically filled with the contents of the parameter from SAP memory unless the field itself is manually associated with a Parameter ID. Data elements of a reference type can’t be linked to the Parameter ID. -
Default Component Name
For table and structure fields, a naming suggestion can be maintained to be proposed when the structure or table field references this data element. English names are generally maintained as default component names (for example, MATERIAL for MATNR). Structures (such as BAPIs) that are used in APIs should follow this convention. -
Change Document
This flag specifies whether changes to fields in database tables should be logged by change objects when the field refers to this data element. -
No Input History
This flag determines if the input history in SAP GUI is active for screen fields that refer to this data element. This allows you to turn off SAP GUI history for sensitive fields. -
Bi-Directional Options
This option defines the behavior of screen fields when using bidirectional fonts such as Hebrew or Arabic. There are two options:- Basic Direction Is Set to LTR: This sets the writing direction as left to right.
- No BIDI Filtering: This option sets the bidirection filtering that prevents the Unicode formatting characters that specify the writing direction of bidirectional fonts in data transport between SAP GUI and the application server.
Most of the semantic attributes of data elements are significant for UI elements (screen elements), but some may be useful for database fields (such as documentation). We recommend creating separate data elements for UI fields and database table fields.
Field labels can be maintained for the data element under the Field Label tab (see Figure 10.55); these labels are used as column headings for lists on screens. You can maintain short, medium, and long headings, plus a header with a freely definable length field.
10.3.2 Structures
We discussed creating structures in Chapter 5, and most of the steps involved in creating structures are similar to those for creating database tables, discussed earlier in this chapter in Section 10.1.1.
A structure can include elementary data types, other structures, or table types. Structures can be nested to any depth. Structures allow you to centrally maintain the fields when similar fields are used in multiple objects (e.g., programs, function module interfaces, etc.).
The components of a structure can include elementary types, reference types, or boxed types, as shown in the available typing methods in Figure 10.56.
The following list defines each of these types:
-
Elementary types
Elementary types can be defined by referring to a data element from which the type can take the technical and semantic attributes or to a predefined type for which the data type, length, and any decimals are manually assigned. -
Referenced types
These can be defined as either data references or object references. Data references can be specified by using a data element that defines a data reference or by using a predefined ABAP Data Dictionary type, length, and any decimals. Object references can be specified by using a data element with a global class or interface reference. -
Boxed types
These can be defined in the structure as of SAP NetWeaver 7.0 EHP 2. Boxed types help to reduce the memory footprint in ABAP programs and improve performance. When a boxed type is used, the system doesn’t allocate initial memory. Memory is allocated to one structure, and all data of a similar type points to that memory, unlike a structure with elementary types in which each data object of the type takes up its own initial memory space. This is called initial value sharing. Memory is shared across multiple data objects that use this type.
Reference types don’t take any initial memory space, so boxed types fall somewhere between elementary types and reference types. In other words, the initial memory of boxed types is much lower than elementary types but greater than reference types.
Currently, only static boxes are supported. Static types are boxed components for which components are known statically. Only a substructure of a structure or attributes of classes or interfaces can be defined as boxed. Boxed types are defined in programs by adding BOXED to the TYPES statement, as shown in Listing 10.4.
TYPES : BEGIN OF ty_lfa1
Lfa1 TYPE lfa1 BOXED,
END OF ty_lfa1.
The efficiency of a boxed structure is due to the initial value sharing in which all occurrences of similar data are referenced to the same initial memory. However, the initial value sharing is revoked under the following circumstances:
- Write access is made to a static box or its components.
- The static box or its components are assigned to a field symbol.
- The static box or its components are addressed using a data reference.
- The static box or its components are used as an actual parameter of a procedure.
Once the initial value sharing is revoked, the internal reference will reference an instance of the structure.
Boxed types reduce the memory requirement of structures that are used more than once. For example, if an internal table has a substructure, then each row will increment the memory store even if the row is initial. If the substructure is defined as boxed type, then it won’t require multiple memory stores, because each row can point to the same initial memory as long as only read accessed is permitted.
Figure 10.57 shows the initial memory allocation of structures, as shown in the debugger window, using the normal types, boxed types, and reference types defined in Listing 10.5. In Listing 10.5, we’ve defined three structures of each type to highlight how the memory is allocated individually when the object of the same type is repeated.
As you can see in Figure 10.57, boxed types have a much smaller memory footprint then normal types. Boxed types are deep structures—that is, structures that contain table types.
*Normal type
TYPES : BEGIN OF ty_kna1,
kna1 TYPE kna1,
END OF ty_kna1.
*Boxed type
TYPES : BEGIN OF ty_kna1_b,
kna1 TYPE kna1 BOXED,
END OF ty_kna1_b.
*Data declarations using normal type
DATA : st_kna1_1 TYPE ty_kna1.
DATA : st_kna1_2 TYPE ty_kna1.
DATA : st_kna1_3 TYPE ty_kna1.
*Data declarations using boxed type
DATA : st_kna1_b_1 TYPE ty_kna1_b.
DATA : st_kna1_b_2 TYPE ty_kna1_b.
DATA : st_kna1_b_3 TYPE ty_kna1_b.
*Data declaration using reference type
DATA : st_kna1_o_1 TYPE REF TO ty_kna1.
DATA : st_kna1_o_2 TYPE REF TO ty_kna1.
DATA : st_kna1_o_3 TYPE REF TO ty_kna1.
BREAK-POINT.
10.3.3 Table Types
Table types describe the structure and functional attributes of internal tables. You learned how to create a global table type in ABAP Data Dictionary in Chapter 7, but we limited our discussion there in order to quickly define a table type with default options. In this section, we’ll discuss table types in greater detail and go over the various options that are available while defining a table type in ABAP Data Dictionary.
The Line Type tab of the Dictionary: Change Table Type screen in ABAP Data Dictionary (see Figure 10.58) allows you to maintain the line type for the table type. This line type then defines the components of the table type. Internal tables that refer to this table type will have a row type as defined by the line type of the table type.
There are multiple options to define the line type of the table type:
-
Line Type
In this field, you can enter an existing type such as a data element, structure, table type, table, view, and so on; its properties are then copied as properties of the table line. -
Predefined Type
You can select this option to manually input the ABAP Data Dictionary type, length, and any decimals. -
Reference Type
Using this option, you can specify an object reference (class or interface) or data reference (elementary data type).
Under the Initialization and Access tab (see Figure 10.59), attributes such as the type of table and initial size can be maintained. The available options are as follows:
-
Initial Line Number
This value is used for internal memory management to allocate the initial size of the table. This is similar to using the INITIAL SIZE addition in ABAP programs while defining the internal table. -
Access
You can select the kind of table that is defined when a data object is referred to this table type. The following options are found under the Access section:- Standard Table: Defines a standard internal table.
- Sorted Table: Defines a sorted internal table. The key should be specified in the Primary Key and Secondary Key tabs.
- Hashed Table: Defines a hashed internal table. The key should be specified in the Primary Key and Secondary Key tabs.
- Index Table: Index tables define a generic table category for standard and sorted tables (because they have an index).
- Not Specified: Defines a generic category for any table (all table categories).
The Primary Key tab (see Figure 10.60) allows you to define the primary key. This property will then be set for internal tables that refer to this table type. The available options on this screen are as follows:
-
Key definition
Under the Key definition section, you can define one of the following for the primary key:- Standard Key: This sets the standard key in which all the fields of the row are part of the key. This option can be selected only if the line type of the table type is not a table type itself or the line type doesn’t contain any components that are table types.
- Line Type: The primary key is the line type used for the table type.
- Key Components: The primary key fields can be selected manually by clicking the Choose Components button.
- Not Specified: This is used for generic table types for which a key is not defined.
-
Key Category
Key Category defines whether the key is unique or nonunique. If the key fields uniquely define a row, the key can be set to Unique; otherwise, Non-Unique. You can select Not Specified for generic types.
Beyond the primary key, additional secondary keys can be specified under the Secondary Key tab (see Figure 10.61). You can define up to fifteen secondary keys for the table type. Each secondary key should have a unique name. The name of the secondary key can’t be PRIMARY_KEY or LOOP_KEY. The secondary key can be either a sorted key or a hash key. If it’s sorted key, it can be unique or non-unique; a hash key is always unique. The key definition for a secondary key is defined by the entire table row if the line type is not a table type or doesn’t contain a table type as a component.
In this section, we discussed using data types, including data elements, structures, and table types. In the next section, we’ll see how type groups are used and maintained.