LESSON 4 ABAP DATA OBJECTS AND STATEMENTS
Types describe the attributes of- Input and output fields on screens,
- Data objects and
Interface
parameters: Type checks are performed each time a function or
subroutine is called according to how the interface parameter is typed.
Type conflicts are already identified during the editing process and
appropriate syntax errors displayed.
Local types are used in programs
- If only technical attributes are needed and no semantic attributes are required, and
- If the types are only used locally within a program.
Global types ( = ABAP Dictionary types) are used
If you intend to use the types externally
(for
example, for typing the interface parameters of global functions or
with those data objects in the program that serve as the interface to
the database or the presentation server), If you need semantic information as well (for example, on screens with input and output fields).
More information on storing semantic information centrally can be found in this unit.
Elementary Dictionary types are called Data Elements. They contain semantic as well as technical information (technical type, length, number of decimal places).
A data element can contain the following semantic information:
Field Label: This
text appears on screens and selection screens to the left next to the
input or output fields. A field label can have one of three possible
lengths. You must select one of the different field labels when you
create a screen.
Field Documentation: The
field documentation tells the user what information should be entered
in the field. The user gets the field documentation for an input or
output field where the cursor is positioned by pressing function key F1.
Search Help: A
data element can be linked to a search help. This search help defines
the value help provided by function key F4 or the corresponding icon.
You can find more information on elementary ABAP Dictionary types
for screen fields : Using F1 -> Technical info. or by double-clicking on the output field next to the data element label
for local types in programs or data objects : By double-clicking on the type
Technical
types and technical domains may be directly assigned to data elements.
If you want more information on other data elements referencing the same
domain, you can navigate to the domain from the data element by double
-clicking on its name and then executing the function Where-used list.
You can search for data elements by using the application hierarchy and the Repository Information System.
In the application hierarchy, select the components to be scanned.
Go to the Information System.
Choose ABAP Dictionary --> Basic objects --> Data elements and restrict the search.
If
you go to the Information System from the application hierarchy, the
development classes of the selected application components are
automatically entered.
You can also go directly to the Information System. If you do not select a development class, the entire Repository is scanned.When
defining simple types or variables, you can refer to a pre-defined
type. For more information refer to the keyword documentation on TYPES or DATA.
- C Character
- N Numeric Text
- D Date (YYYYMMDD)
- T Time (HHMMSS)
- X Byte (heXadecimal)
- I Integer
- P Packed Number
- F Floating Point Number
- STRING Character String
- XSTRING String of Bytes (X String)
Assign
data object types by referring your object to either a built-in ABAP
type, a user defined type or an ABAP Dictionary object.
If a variable v2 refers to variable v1 using the addition LIKE ( DATA v2 LIKE v1. ), then v2 inherits its type from v1.Up to Release 4.0B, you could only refer to Dictionary types using LIKE. Only structure fields could be used as elementary types up to that point. Only flat structures were provided as structure types.Elementary data objects appear in the program object list under the 'Fields' node.From the object list, you can use the right mouse button to navigate to the part of the source code where the data object is defined.You can use the Where-used list function to display all lines of source code where the data object is used.
Rules for naming data objects:
A name can consist of 30 characters maximum (letters, numbers, or symbols).
The following symbols ARE NOT allowed: ( ) + . , :
SPACE is a predefined field.
For compatibility reasons, it is still possible to construct data objects in the DATA statement without first having to define the type locally in the program with a TYPES statement. Default values are also defined in addition to the type information for the following generic types:
With data types P,N,C, and X you
may enter a length (in bytes) in parentheses after the type name.If no
length is entered, the default length for this data type is used. You
can find the standard lengths in the keyword documentation for TYPES and DATA.
With data type P you can use the DECIMALS addition
to determine the number of decimal places that should be used (up to a
maximum of 14). If this addition is missing, the number of decimal
places is set to zero.If no type is entered, then the field is
automatically a type C field.
You define constants using the ABAP keyword CONSTANTS. The VALUE addition is required for constants. It defines their value.ABAP recognizes two types of literals: number literals and text literals. The latter is always enclosed in inverted commas (').Whole numbers (with preceding minus sign if they are negative) are stored as number literals with either type I (up to and including nine digits) or type P (ten or more digits).
All other literals (character, numbers with decimal places, floating point numbers) are stored as text literals wit h data type C. If a literal is assigned to a variable that does not have type C, then a type conversion is carried out. The conversion rules are described in the keyword documentation about MOVE.If you want to include an inverted comma (') in a text literal, you must enter it twice.
You can create translatable text literals, or text symbols, for all ABAP programs. Use the Program object types dialog box to get to the maintenance screen for the text symbols.
When
a program is started, the program context is loaded into a storage area
of the application server and made available for all the data
objects.Each elementary field comes as standard with an initial value
appropriate to its type. You can set a start value for an elementary
field yourself using the VALUE addition. After VALUE you may only specify a fixed data object.You can copy the field contents of a data object to another data object with the MOVE statement.
If the two data objects have different types, the type is automatically
converted if there is a conversion rule. If you want to copy the field
contents of variable var1 to a second variable var2, you can choose one of two syntax variants:
. MOVE var1 TO var2.
. var2 = var1.
The CLEAR statement resets the field contents of a variable to the initial value for the particular type.You can precede calculations with the COMPUTE statement.
This statement is optional. You can use either of the following two
syntax variants to calculate percentage occupancy using the variable v_occupancy for 'current occupancy'‚ v_maximum for 'maximum occupancy'‚ and v_percentage for 'percentage occupancy':
. COMPUTE v_percentage = v_occupancy * 100 / v_maximum.
. v_percentage = v_occupancy * 100 / v_maximum.
You can find detailed information on the operations and functions available in the keyword documentation on COMPUTE.IF and CASE statements allow you to make case distinctions:
CASE ... ENDCASE:
. Only one of the sequences of statements is executed.
. The WHEN OTHERS statement is optional.
IF ... ENDIF:
. The logical expressions that are supported are described in the keyword documentation about IF.The ELSE and ELSEIF statements are optional.If the logical expression is fulfilled, the following sequence of statements is executed.If the logical expression is not fulfilled, the ELSE or ELSEIF section is processed. If there is no ELSE or no further ELSEIF statement, the program continues after the ENDIF statement.You can include any number of ELSEIF statements between IF and ENDIF. A maximum of one of the sequences of statements will be executed.You
can trace the field contents of up to eight data objects in debugging
mode by entering the field names on the left side or by creating the
entry by double -clicking on a field name.You can change field values at runtime by overwriting the current value and choosing the 'Change' icon.
From Release 4.6, you are allowed to set up to 10 watchpoints and link them using the logical operators AND and OR. Watchpoints are breakpoints that are field-dependent. You can create the following types of watchpoints:
Variable
value: The system stops processing once the logical condition is
fulfilled. The 'Comparison field' flag is not selected and the value is
inserted at 'Comp. field/value'.
Variable1
variable2: The system stops processing once the logical condition is
fulfilled. The 'Comparison field' flag is selected and variable2 is
inserted at 'Comp. field/value'.
Variable: The system stops processing each time the variable's value changes.
You
can define structured data objects (also called structures) in ABAP.
This allows you to combine variables that belong together into one
object. Structures can be nested. This means that other structures or
even tables can be sub-objects of your original structure.
There are two different kinds of structures in ABAP programs:
. Structures defined using DATA TYPE .
These
kinds of structures serve as the target fields for database accesses or
for calculations performed locally within the program. You can declare
these types of structures either in the ABAP Dictionary or locally
within your program. For more information on how to declare local
structures, refer to the keyword documentation on TYPES.
Structures defined using TABLES .
These types of structures are technically administered in their own area. From Release 4.0,TABLES structures only need to be used as interface structures for screens.
Fields of a structure are always addressed with -.
The MOVE-CORRESPONDING TO statements transports values field by field between structures and . This only works if the components have identical names.The system looks for all fields in whose names also occur in and transports field - to - in all cases where it finds a match.All other fields remain unchanged.
You
can trace the field contents of a structure by entering the name of the
structure in the left column. The field view of the structure is
displayed if you double -click on this entry.You must define the following information in order to fully specify a table type:
Line Type: You can store the information about the required columns, their names and types, by defining a structure type as line type.
Key: A fully specified key must define: Which columns should be key columns? In what order?
Should
the key uniquely specify a record of the internal table (unique key)?
Unique keys cannot be defined for all the table types.
Table Kind: There are three table kinds: standard tables, sorted tables and hashed tables. The estimated access type is mainly used to choose the table type.
Access type defines
how ABAP accesses individual table entries. There are two different
types of data access in ABAP, access using the index and access using a
key.
Access using the index involves using the data record index that the system maintains to access data.
Example :
Read access to a data record with index 5 delivers the fifth data
record of your internal table (Access quantity: one single data record).
Access using a key involves using a search term, usually either the table key or the generic table key, to access data.Another
internal table attribute is the table type. Internal tables can be
divided into three table types according to the way they access data:
Standard tables maintain a linear index internally. This kind of table can be accessed using either the table index or keys.
Sorted tables are
sorted according to key and saved. Here too, a linear index is
maintained internally. This kind of table can also be accessed using
either the table index or keys.
Hashed tables do not maintain a linear index internally. Hashed tables can only be accessed using keys.
Which
table type you use depends on how that table's entries are normally
going to be accessed. Use standard tables when entries will normally be
accessed using the index, use a sorted table when entries will normally
be made using keys, and use hashed tables when entries will exclusively
be made with keys.
You can perform the following operations on single records in internal tables:
. APPEND appends the contents of a structure having the same type as the line to an internal table.
This operation can only be used with standard tables.
. INSERT inserts the contents of a structure having the same type as the line in an internal table.
This
causes a standard table to be appended and a sorted table to be
inserted in the right place; a hashed table is inserted according to the
hashing algorithm.READ copies the contents of a line of the internal table to a structure having the same type as the line.
. MODIFY overwrites a line of the internal table with the contents of a structure having the same type as the line.
. DELETE deletes a line of the internal table.
. COLLECT inserts
the contents of a structure having the same type as the line in an
internal table into an internal table in compressed form.. This
statement may only be used for tables whose non key fields are all
numeric. The numeric values are added for identical keys.
n You
can find detailed information about the ABAP statements described here
in the keyword documentation for the relevant ABAP keywords.
n You can perform the following operations on sets of records in internal tables:
. LOOP ... ENDLOOP The LOOP places the lines of the internal table in the structure
specified in the INTO clause
one-by-one. The structure must have the same type as the line of the
internal table. All single -record operations can be executed within the
loop. In this case the system provides the information about the line
to be edited in the single -record operation.
. DELETE deletes the lines of the internal table that satisfy the condition .
. INSERT copies the contents of several lines of an internal table to another internal table.
. APPEND appends the contents of several lines of an internal table to another standard
table.
n You
can find detailed information about the ABAP statements described here
in the keyword documentation for the relevant ABAP keywords.
n You can perform the following operations on internal tables:
. SORT You can sort tables by any column or columns in ascending or descending order
Sorted tables cannot be resorted.
. CLEAR Sets the contents of the internal table to the right initial value for the column type.
. REFRESH works like CLEAR.
. FREE Deletes the internal table and releases the memory allocated to the table.
n You
can add lines to a standard ta ble by first filling a structure with
the required values and then appending it to the internal table with the
APPEND statement. This statement is only meaningful with standard tables.
n Use the INSERT statement to insert lines in sorted and hashed tables.
n INSERT works like an APPEND in standard tables.
n You can read and edit the contents of an internal table with a LOOP statement.
In this example, one line is copied each time from internal table
it_flightinfo to structure wa_flightinfo. The fields of the structure
can then be edited. A list is built here from the fields with a WRITE statement.
n If
you want to change the contents of the internal table, first change the
value of the structure fields within the loop and then overwrite the
line of the internal table with the MODIFY statement.
in With the INDEX addition
you can restric t access to certain line numbers. You may only perform
index operations on index tables. Both standard and sorted tables are
supported here.
in The above example shows the syntax for loop editing that only scans the first five lines of the internal table.
in The example below shows the syntax for reading the third line of the internal table.
in With the WHERE addition
you can restrict access to lines with certain values in key fie lds.
Key operations are supported for all table types. Key access to sorted
or hashed tables is more efficient than key access to standard tables.
in The above example shows the syntax for loop editing that only scans the lines of the internal table whose 'carrid' field has the value 'LH'. The sorted table is most suitable for this type of editing. Loop editing with the WHERE addition is supported for sorted and standard tables.
in The
example below shows the syntax for reading a line of the internal table
with a fully specified key. The return code sy-subrc is set to zero if
the internal table contains this line. The hashed table is most suitable
for single -record access by key. This type of access is supported for
all table types.
Note that all the key fields must be defined in key accesses with the WITH TABLE KEY addition. It is easy to confuse this addition with the WITH KEY addition,
which already permitted key access to standard tables prior to Release
4.0, when it was not yet possible to define key columns explicitly.
n You
can trace the contents of an internal table in debugging mode by
choosing 'Table' and entering the name of the internal table.
n Internal tables can be defined with or without a header line. An internal table with header line consists of a work area (the header line) and the actual body of table, both of which are addressed with the same name. How this name is interpreted depends on the context in which it is used. For example: at MOVE the name is interpreted to mean the header line, while at SORT it is interpreted as the table body.
n You can declare an internal table with a header line using the addition WITH HEADER LINE.
n In order to avoid confusion, it is recommended that you create internal tables without header lines.
However, in internal tables with header lines you can often use a shortened syntax for certain
operations.
n A
number of ABAP statements support a return code. Various exceptions are
detected, depending on the statement. If such an exception occurs, a
value is stored in field sy-subrc and
the function for the statement is terminated. The keyword documentation
for the particular statement describes the exceptions that are
supported and their values. When you start a program, a structure named sy is
automatically provided as data object. This structure contains various
fields that are filled by the system.. You can access these fields from
the program. One of the fields of this structure is field subrc. You therefore do not have to create a data object for the return code.
n In this example a line should be read from internal table itab with key access. There is no line with the required key at runtime. The Basis function for the READ statement is therefore terminated and the value 4 is placed in field sy-subrc. Field sy-subrc is queried in the program immediately after the READ statement.
in There is a special dialog type called the user message for error situations. Messages are triggered with the MESSAGE statement.
in Messages can be found in table T100. They are organized according to language, a two-digit ID, and a three-digit number.
in Messages can contain up to 4 variables, identified as &1, &2, &3, and &4.
If you want to output the character & and do not want to use it as a
variable, double it, for example: 'This is a message with an &&'.
in In message long texts use &v1&, &v2&, &v3&, and &v4& for their corresponding variable.
in You can create your own messages using ID numbers that begin with Y (for the head office) or Z (for branch offices).
in Send messages with the MESSAGE statement.
The language for messages in table T100 is automatically set to the
user's logon language. You can define the message ID following the
parameter MESSAGE-ID in the REPORT statement. The message ID is now set for the entire report.
Enter the message number at the MESSAGE statement.
in Enter
the message type directly in front of the three-digit message number;
this letter determines how the report user reacts to dialog messages
(see next slide).
in Set values for variables (up to a maximum of four) following the parameter WITH.
Fields and literals are also allowed. The field at level i thus
replaces message variable &i. If the variables in the message are
identified with & or $, these placeholders are supplied with values
independent of the position of the fields of the message statement.
n In addition to using message ID with the REPORT statement, you can also add a different message ID to the command MESSAGE by entering the ID in parenthesis directly after the message number.
This deviant message ID is only valid for a single message, however. Example: MESSAGE
E004(UD).
n Use the following syntax, whenever you want to send a dynamic message: MESSAGE ID
TYPE NUMBER WITH .
n System fields SY-MSGID, SY-MSGTY and SY-MSGNO are supplied with the message ID, message type, and message number respectively and system fields SY-MSGV1 to SY-MSGV4 with the fields for the placeholders.
n There are six different types of message: A, X, E, I, S or W. The runtime behavior of the messages depends on the context. The letters have the following meaning:
A Termination Processing is terminated, the user must restart the transaction
X Exit Like a termination message, but with short dump
MESSAGE_TYPE_X E Error Runtime behavior depends on context
W Warning Runtime behavior depends on context
I Information Processing is interrupted, the message is displayed in a dialog
box and the program is continued when the message has been confirmed
with ENTER.
S Set The message appears in the status bar on the next screen.
n You can find a program for testing the runtime behavior in the sample programs of the
documentation. You can find the sample programs with transaction code ABAPDOCU or in the Editor with the ‘Information’ icon and radio button ABAP Docu and Examples.
No comments:
Post a Comment