2. Validity and Visibility
• There are three contexts in which data type and
data objects can be declared
– Locally in Procedures
• Exists while a procedure is being executed
– As Components of Classes
• Static components exists for the internal session of the ABAP
program
• Instance attributes exists for the lifetime of the object (from
object instantiation until object deletion)
– Globally to the Framework Program
• Exists during the lifetime of the program
2
3. Date Type and Data Objects
• Date types are templates for creation of data objects
• Data object is an instance of a data type and occupies
as much memory as its type specifies [exception: the
length of text and byte string varies according to their
content]
• An ABAP program only works with the data this is
available as the content of the data object
• Data objects are either created implicitly
– as named data objects or
– as anonymous data objects using CREATE DATA command
– as literals
3
4. Date Types
• Type of Data Types
– Predefined Data Types
• Include predefined ABAP Types (b, c, d, f, i, n, p, string, etc),
generic ABAP Types (any, any table, c, clike, csequence,
data, n , numeric, object, etc) and Predefined ABAP
Dictionary Types (CHAR, DATS, DEC, INT4, CURR, CLNT,
etc.) – these were covered in an earlier workshop
– User Defined Data Types
• All ABAP programs can define their own data types. Within a
program, procedures can also define local types
• You declare local data types in a program either by referring
to an existing data type or constructing a new type
• You can declare program local data types in ABAP programs
that can be used for typing or declaring additional data types
and data objects according to their validity and visibility
4
5. User-Defined Data Types
• User Defined Data Types can be created using
– A predefined ABAP type to which you refer using the
TYPE addition
– An existing local type in the program to which you
refer using the TYPE addition
– The data type of a local object in the program to
which you refer using the LIKE addition
– A data type in the ABAP Dictionary to which you refer
using the TYPE addition. To ensure compatibility with
earlier releases, it is still possible to use the LIKE
addition to refer to database tables and flat structures
in the ABAP Dictionary. However, you should use the
TYPE addition in new programs.
5
7. Elementary Type Definition
Syntax for any data type:
TYPES dtype[(len)] TYPE existing_type | LIKE data_object [DECIMALS dec]
OR
TYPES dtype TYPE existing_type | LIKE data_object [LENGTH len]
[DECIMALS dec]
– existing_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
– data_object can be an existing data object with an elementary data type
When you refer to a data element from the ABAP Dictionary, the system
converts it into an elementary ABAP type. If no type and length is specified,
the type defaults to type character c of length 1
7
8. Elementary Type Definition
(examples)
• TYPES mynumber TYPE i.
• TYPES mydistance TYPE p DECIMALS 2.
• TYPES mycode(3) TYPE c.
• TYPES text10 TYPE c LENGTH 10.
• TYPES text20 TYPE c LENGTH 20.
• TYPES myresult TYPE p LENGTH 8 DECIMAL 2.
• TYPES mycompany TYPE spfli-carrid.
• DATA counts TYPE i.
• TYPES: company TYPE mycompany,
myname TYPE text20,
no_flights LIKE counts.
8
9. LINE OF addition
TYPES dtype TYPE [LINE OF] existing_type
| LIKE [LINE OF] data_object
– The optional LINE OF addition can be used if
existing_type is a table type or if data_object is an
internal table. If this addition is used dtype inherits the
properties of the line type of the internal table
Examples
TYPES event TYPE LINE OF event_table.
TYPES wa_type LIKE LINE OF TABLE123.
9
10. Complex Type Definition
Structures are complex types
Syntax
TYPES BEGIN OF struc_type.
…
TYPES | INCLUDE …
…
TYPES END OF struc_type.
The TYPES statement within the statements with BEGIN OF and END
OF define components of the structure struc_type. If a component is a
structured type or a new structure is defined within a structure using
BEGIN OF and END OF, this results in substructures or nested
structure
The statement INCLUDE defines components of the structured type
struc_type by copying the components of another structured type or an
existing structure at the same level.
10
11. Complex Type Definition (continued)
TYPES: BEGIN OF address,
name TYPE surname,
street(30) TYPE c, Nested structures
city TYPE spfli_type-cityfrom,
END OF address,
town TYPE address-city.
Accessing
TYPES BEGIN OF struct1, Level 2
col1 TYPE i, components
BEGIN OF struct2,
col1 TYPE i,
col2 TYPE i,
END OF struct2,
TYPES END OF struct1.
TYPES mytype TYPE struct1-struct2-col2.
11
12. Complex Type Definition (continued)
REPORT demo_structure.
TYPES: BEGIN OF myname,
firstname TYPE c LENGTH 10,
lastname TYPE c LENGTH 10,
END OF myname. Nested Structures
TYPES: BEGIN OF mylist,
client TYPE myname,
number TYPE i,
END OF mylist.
DATA list TYPE mylist.
list-client-firstname = 'John'.
list-client-lastname = 'Doe'. Different Levels
list-number = 1.
WRITE list-client-firstname.
WRITE list-client-lastname.
WRITE / 'Number'.
WRITE list-number.
12
13. Copying Structure Components
INCLUDE copies the components of an existing
structure within another structure’s definition
INCLUDE { {TYPE struc_type} | {STRUCTURE
struc} } [AS name [RENAMING WITH SUFFIX
suffix]]
Use [AS name] to alias and if multiple levels are okay
Use SUFFIX if including the same structure more than
once and need to be on the same LEVEL
13
14. Using Include to Copy Structure Type
REPORT demo_structure_with_include.
TYPES: BEGIN OF myname, DATA mailinglist TYPE mylist.
firstname(10) TYPE c,
lastname(10) TYPE c,
END OF myname. mailinglist-firstname = 'John'.
mailinglist-lastname = 'Doe'.
TYPES: BEGIN OF myaddress, mailinglist-street = '123 Some Street'.
street(20) TYPE c, mailinglist-city = 'Bay City'.
city(18) TYPE c, mailinglist-state = 'TX'.
state(2) TYPE c,
zip(5) TYPE n, mailinglist-zip = '12345'.
END OF myaddress. mailinglist-number = 1.
TYPES: BEGIN OF myindex, WRITE / mailinglist-firstname.
number TYPE i, WRITE mailinglist-lastname.
END OF myindex. WRITE / mailinglist-street.
WRITE / mailinglist-city.
TYPES BEGIN OF mylist. WRITE mailinglist-state.
INCLUDE TYPE myname AS s1.
INCLUDE TYPE myaddress AS s2. WRITE mailinglist-zip.
INCLUDE TYPE myindex AS s3. WRITE / 'Number'.
TYPES END OF mylist. WRITE mailinglist-number.
INCLUDE enables us to access all components at one level.
Note: mailinglist-firstname is same as mailinglist-s1-firstname, etc 14
15. Using Include to Copy Structure
…
Type Multiple Times
TYPES BEGIN OF mylist.
INCLUDE TYPE myname AS s1.
INCLUDE TYPE myaddress AS s2A RENAMING WITH SUFFIX _home.
INCLUDE TYPE myaddress AS s2B RENAMING WITH SUFFIX _office.
INCLUDE TYPE myindex AS s3.
TYPES END OF mylist.
DATA mailinglist TYPE mylist.
Same INCLUDEd
mailinglist-firstname = 'John'.
mailinglist-lastname = 'Doe'. structure different
SUFFIX
mailinglist-street_home = '123 Some Street'.
mailinglist-city_home = 'Bay City'.
mailinglist-state_home = 'TX'.
mailinglist-zip_home = '12345'.
mailinglist-street_office = ‘666 Main Street'.
mailinglist-city_office = 'Bay City'.
mailinglist-state_office = 'TX'.
mailinglist-zip_office = '99999'.
…
15
16. Internal Tables
• Internal tables provide a means of taking data from a fixed structure
and storing it in working memory in ABAP
• The data is stored line by line in memory, and each line has the
same structure
• In ABAP, internal tables fulfill the function of arrays
• Since they are dynamic data objects (i.e., data type defines all
properties statically with the exception of memory consumption),
they save us the task of dynamic memory management in our
programs
• Use internal tables whenever you want to process a dataset with a
fixed structure within a program and dynamic # of rows
• A particularly important use for internal tables is for storing,
processing and formatting data from a database table within a
program
• They are also a good way of including very complicated data
structures in an ABAP program
16
17. Table Types - Data Type of Internal
Tables
Similar to Structures, Table Types represent complex
ABAP data types
The data objects of table types are Internal Tables
The data type of an internal table is fully specified by its line
type, key, and table type
Syntax:
• TYPES dtype TYPE|LIKE tabkind OF linetype [WITH
key] ...
– This 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
17
18. Line Type of Internal Tables
• The line type of an internal table can be
any data type
• The data type of an internal table is
normally a structure
• Each component of the structure is a
column in the internal table
• However, the line type may also be
elementary or another internal table
18
19. Key of Internal Tables
… [UNIQUE | NON UNIQUE] { {KEY comp1
comp2 comp3 …} | {DEFAULT KEY} } …
• Use unique or non unique to specify whether the
table key is unique or not (key identifies table
rows, internal tables with a unique key cannot
contain duplicate entries)
• You can only use non unique key for standard
tables, unique key for hashed tables and both
types of keys for sorted tables
19
20. Key of Internal Tables (continued)
• Key types
– STANDARD KEY (DEFAULT KEY)
– USER-DEFINED KEY
• The are two kinds of keys are the standard key
[DEFAULT KEY] and a user-defined key
[specified individual components comp1 comp2
etc]. For tables with structured row type, the
standard key is formed from all character-type
columns [not numeric i, p, f nor table types] of the
internal table.
20
21. Key of Internal Tables (continued)
• If a table has an elementary line type [i.e., non
structured], the default key is the entire line (row). If the
row type is also a table, an empty key is defined
• The user-defined key can contain any columns of the
internal table that are not internal table themselves, and
do not contain internal tables. References are allowed as
table keys. Internal tables with a user-defined key are
called key tables
• For the generic table types ANY TABLE or INDEX
TABLE you can only specify a key without specifying the
uniqueness
21
22. Row Type of Internal Table
• Non generic data type from ABAP dictionary
• Non generic program local data type
• Any ABAP type
– generic (c, d, f, i, n, p, etc) or
– non generic (any, c, clike, numeric, etc)
• Using REF TO makes the row type a reference
type
• Instead of type an data object dobj from the
program specified. By doing so the type of the
object is adopted for the row type
22
23. Table Types of Internal Tables
… { { [STANDARD] TABLE} | SORTED TABLE | HASHED TABLE | ANY
TABLE | INDEX TABLE}} …
• The table type determines how ABAP will access individual table entries
• Standard tables are managed by a logical index. The system can access
records either by using the table index or the key. The key of a standard
table is always non-unique. You cannot specify a unique key.
• Sorted tables are managed by a logical index (similar to standard tables).
The entries are listed in ascending order according to table key
• Hashed tables are managed by hash algorithm. There is no logical index.
The entries are not ordered in the memory. The position of a row is
calculated by specifying a key using a hash function
• Index tables include both the standard tables and sorted tables
• To find out the access type of an internal table at runtime, use the statement
DESCRIBE TABLE KIND
23
24. Internal Table (example)
TYPES: BEGIN OF portfolio_type,
name(25) TYPE c,
socialsecurity(9) TYPE c,
assets TYPE p LENGTH 8 DECIMALS 2,
END OF portfolio_type.
TYPES mytab TYPE STANDARD TABLE OF portfolio_type WITH DEFAULT KEY.
DATA wa TYPE portfolio_type.
DATA itab TYPE mytab.
wa-name = 'John Doe'.
wa-socialsecurity = '123456789'.
wa-assets = '123456.50'.
APPEND wa TO itab.
wa-name = 'Harry Smith'.
wa-socialsecurity = '567565678'.
wa-assets = '50000.50'.
APPEND wa TO itab.
LOOP AT itab INTO wa.
WRITE: / wa-name, wa-socialsecurity, wa-assets.
ENDLOOP.
24
25. Internal Table (continued)
• The optional addition WITH HEADER
LINE declares an extra data object with
the same name and line type as the
internal table. This data object is known as
the header line of the internal table. You
use it as a work area when working with
the internal table
• But, the WITH HEADER LINE addition is
obsolete; you should no longer use it.
25
26. Internal Table (continued)
… INITIAL SIZE n …
Specify a number of rows n as a numeric literal or numeric
constant, to adjust the first block of memory reserved by
system for the internal table
Without specifying this value or specifying 0, the system
allocates an appropriate initial memory size
When required the next additional memory block twice the
initial size is reserved, as long as this size does not
exceed 8 KB
Additional memory blocks are created with a constant size
of 12 KB each
26
27. Reference Data Types
TYPES dtype { {TYPE REF TO type } |
{LIKE REF TO dobj} }
Use TYPE addition to define data types for
data and object reference variables
Use LIKE addition to define data type for
data reference variables
There are two types of references that are
possible, DATA Reference and OBJECT
Reference
27
28. DATA Reference
• If the specified data type data is predefined generic data
type, the system creates a data type for a data reference
variable from the static type data. Such reference
variables can refer to any data object but can only be
dereference in the statement ASSIGN
• If the specified data type data is any non-generic data
type from ABAP dictionary, locally defined type or a non
generic predefined type, the system creates a data type
for a data reference variable with the relevant static type.
Such reference variables can refer to all data objects of
the same type and can be dereferenced to matching
operand positions using the dereferencing operator ->*
28
29. DATA Reference (continued)
• By specifying a data object for dobj, the
system creates a data type for a data
reference variable whose static type is
adopted from the data type of the data
object. Such reference variables can refer
to all data objects of the dame type and
can be dereferenced at matching operand
positions using the dereferencing ->*.
Within a procedure, you cannot specify a
generic typed formal parameter for dobj
29
30. Object Reference
• By specifying a global or a local class for
type, the system creates a data type for a
class reference whose static type is the
specified class. Such reference variables
can refer to all instances of the class and
its subclass
30
31. TYPE-POOLS
Type pools are the precursors to general type definitions in
the ABAP Dictionary. Before release 4.0, only
elementary data types and flat structures could be
defined in the ABAP Dictionary. All other types that
should’ve been generally available had to be defined
with TYPES in type pools. As of release 4.0, type pools
were only necessary for constants. As of release 6.40,
constants can be declared in the public sections of
global classes and type pools can be replaced by global
classes. In other words TYPE-POOLS can be
considered obsolete.
31
32. Literals
• These are defined in the source code of a
program and are fully determined by their
value
• Literals may be of type
– Numeric Literals
– Text Field Literals
– String Literals
32
33. Numeric Literals
• Numeric Literals consists of continuous
sequence of numbers preceded by a sign
• Numeric Literals between -2147483648 and
214748648 have a build-in ABAP type i (4 byte
integer)
• Numeric Literals outside this range and up to 15
digits have build-in ABAP type p (packed) with
length of 8 bytes p(8)
• Numeric Literals having more than 15 digits
have build-in ABAP type p (packed) with length
of 16 bytes p(16)
33
34. Numeric Literals (continued)
• When passing a numeric literal to a formal parameter of a function,
the check made are based on
• f all numeric literals are allowed
• i, b, s all numeric literals are allowed
• n the value of the numeric literals must not be negative and the number of
digits are smaller or equal to the length of the formal parameter
• p if the formal parameter is generic, its length is set to 16 and number of
decimals to 0. If the program attribute ‘Fixed point arithmetic’ is not set, the
formal parameter must not have any decimal places or the literal must have
the value zero
34
35. Text Field Literals
• Text field Literals are character strings included
in single inverted commas (‘)
• They have data type of c
• There are no empty text field literals; ‘‘ is same
as text field literal ‘ ‘ of length 1
• The length lie between 1 and 255 characters
• To represent an inverted comma you must enter
2 consecutive inverted commas
• Text Symbols can be used by appending its
three-digit identifier ### where applicable
35
36. Text Field Literals (continued)
• What is a Text Symbols?
A text symbol is a named data object that is generated when you start the
program from the (predefined) texts in the text pool of the ABAP program
… ‘Literal’(###) or ‘Literal’(123)… ### or 123 or ABC is text symbol defined
below and is used in place of the ‘Literal’ if the is (text symbol) is pre-
defined Note1: there is no space between the Literal and the text symbol
Note2: to directly access the text symbol use (text-###) i.e., … = text-123.
36
37. String Literals
• String Literals are character strings
included in single back quotes (`) and
have the data type of string
• Empty string literal `` represents string of
length zero
• To represent a back quote within a string,
enter two consecutive back quotes
• A string literal can be up to 255 characters
• There are no literals for byte fields/strings
37
38. Create Data
All of the data objects that you define in the declaration part of a
program using statements such as DATA are created statically, and
already exist when you start the program. To create a data object
dynamically during a program, you need a data reference variable
and the following statement:
CREATE DATA dref {TYPE type}|{LIKE dobj}.
This statement creates a data object in the internal session of the
current ABAP program. After the statement, the data reference in
the data reference variable dref points to the object. The data object
that you create does not have its own name. You can only address it
using a data reference variable. To access the contents of the data
object, you must dereference the data reference.
38
39. FIELD-SYMBOLS
FIELD-SYMBOL <fs> typing | structure
• Field symbols are placeholders or symbolic names for other fields.
They do not physically reserve space for a field, but point to its
contents. A field symbol can point to any data object. The data
object to which a field symbol points, is assigned to it after it has
been declared in the program.
• Whenever you address a field symbol in a program, you are
addressing the field that is assigned to the field symbol. After
successful assignment, there is no difference in ABAP whether you
reference the field symbol or the field itself. You must assign a field
to each field symbol before you can address the latter in programs.
39
40. FIELD-SYMBOLS
• All operations programmed with field symbols are
applied to the field assigned to it. For example, a MOVE
statement between two field symbols moves the
contents of the field assigned to the first field symbol to
the field assigned to the second field symbol. The field
symbols themselves point to the same fields after the
MOVE statement as they did before
• You can create field symbols either without or with type
specifications. If you do not specify a type, the field
symbol inherits all of the technical attributes of the field
assigned to it. If you do specify a type, the system
checks the compatibility of the field symbol and the field
you are assigning to it during the ASSIGN statement
40
41. ASSIGN Statement
• If you already know the name of the field that you want
to assign to the field symbol when you write a program,
use the static ASSIGN statement:
• ASSIGN <f> TO <FS>.
• When you assign the data object, the system checks
whether the technical attributes of the data object <f>
correspond to any type specifications for the field symbol
<FS>. The field symbol adopts any generic attributes of
<f> that are not contained in its own type specification.
After the assignment, it points to <f> in memory
41
42. Field Symbol and ASSIGN
• REPORT demo_field_symbols_stat_assign .
• FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i.
• DATA: text(20) TYPE c VALUE 'Hello, how are you?',
• num TYPE i VALUE 5,
• BEGIN OF line1,
• col1 TYPE f VALUE '1.1e+10',
• col2 TYPE i VALUE '1234',
• END OF line1,
• line2 LIKE line1.
• ASSIGN text TO <f1>.
• ASSIGN num TO <f2>.
• DESCRIBE FIELD <f1> LENGTH <f2> IN CHARACTER MODE.
• WRITE: / <f1>, 'has length', num.
• ASSIGN line1 TO <f1>.
• ASSIGN line2-col2 TO <f2>.
• MOVE <f1> TO line2.
• ASSIGN 'LINE2-COL2 =' TO <f1>.
• WRITE: / <f1>, <f2>.
42
43. Absolute Type Names
• Local data Types hide global data types of the
same name
• The same applies to the classes and interfaces
• Absolute type name can be used to override the
hidden global type by a local type
• TYPE=name
• CLASS=name
• PROGRAM=name
• FUNCTION-POOL=name
• TYPE-POOL=name, etc
43