From release 7.57, the new addition ELSE UNASSIGN can be specified in various cases of the statement
ASSIGN, like dynamic assignments/access, assignments of dynamic components or table expressions.
An assignment passes the content of a source to a target data object. The general syntax is
ASSIGN mem_area TO <fs> [ELSE UNASSIGN].
Which assigns the memory area specified using
mem_area to the field symbol
<fs>. The reference in a field symbol is set using an assignment of a memory area to the field symbol by the statement “ASSIGN” and is deleted by the statement “UNASSIGN” which initializes the field symbol
<fs>. The newly defined addition
ELSE UNASSIGN to the statement ASSIGN, unassigns the field symbol if the assignment is not successful, meaning, in case sy-subrc is not initial.
The field symbol “points to” the contents of the field
mem_area at runtime, i.e. every change to the contents of
mem_area is reflected in and vice versa. If the field symbol is not typed (see
FIELD-SYMBOL ), the field symbol adopts the type and atrributes of the field
mem_area at runtime, particularly the conversion exit. Otherwise, when the assignment is made, the system checks whether the type of the field
mem_area matches the type of the field symbol. After a successful assignment, the field symbol refers to the assigned memory area and can be used in operand positions.
The memory area
mem_area can be specified
statically, in which a statically known data object or part of such an object is assigned, or
dynamically, which is used to dynamically access data objects especially for
dynamic access to attributes of classes and interfaces or
components of structures. In such variants, the statement ASSIGN sets the return code "sy-subrc". If the assignment is successful, sy-subrc is set to 0, otherwise to 4. In these variants, no exception occurs in case of an unsuccessful assignment.
Besides, the variant for specifying the memory area of the statement ASSIGN can also assigns the result of
the table expression table_exp or table expression chaining to the field symbol. The result of a table expression in these positions is always a temporary field symbol. In this variant, the statement ASSIGN sets the return code sy-subrc. If the specified line is found, sy-subrc is set to 0. If the assignment is not successful, sy-subrc is set to 4, except when the end of the table is reached in binary searches in sorted tables. In this case, sy-subrc is set to 8.
The variants differ as to how the system behaves after an unsuccessful assignment. In such case, the behaviour depends on the assigned
mem_area and on the addition
ELSE UNASSIGN:
- If a static assignment is not successful, sy-subrc is not set. No memory area is assigned to the field symbol. The field symbol has the state unassigned after the ASSIGN statement. The addition ELSE UNASSIGN is used implicitly and must not be specified.
- If a dynamic assignment, an assignment of dynamic components, a dynamic access or an assignment of a table expression is not successful, sy-subrc is set to 4 or 8 and:
- If ELSE UNASSIGN is not specified, the field symbol keeps its previous state.
- If ELSE UNASSIGN is specified, no memory area is assigned to the field symbol. The field symbol has the state unassigned after the ASSIGN statement.
Some invalid dynamic specifications in the assignment of dynamic components do not set sy-subrc but raise an exception.
- An assignment of the constructor operators NEW or CASE is either successful or leads to an exception and the addition ELSE UNASSIGN must not be used.
If an assignment would lead to illegal memory accesses, an exception is raised for both static and dynamic ASSIGN statements.
Example
After a successful assignment, the next assignment is not successful because of a wrong dynamic specification.
FINAL(field) = `exists`.
ASSIGN ('field') TO FIELD-SYMBOL(<fs>) ELSE UNASSIGN.
ASSERT sy-subrc = 0 AND <fs> IS ASSIGNED.
ASSIGN ('exists') TO <fs> ELSE UNASSIGN.
ASSERT sy-subrc = 4 AND <fs> IS NOT ASSIGNED.
IF <fs> IS NOT ASSIGNED.
cl_demo_output=>display( 'The field symbol is unassigned' ).
ENDIF.
here sy-subrc is set to 4 and the field symbol that was assigned before is unassigned.
it is more clear in the debugging mode:
As the field symbol is successfully unassigned by 'ELSE UNASSIGN' command, we get the pop up as expected:
if you omit this command from line 14, although the sy-subrc is 4, the field symbol still contains the amount from the previous step:
and the runtime error occurs.
So, the addition 'ELSE UNASSIGN' guarantees the correct assignment of the field symbol in the program.
For more information regarding assignment in ABAP, check
ABAP Keyword Documentation and the links provided in the text.