SAP ASSIGN MEM AREA WRITABLE EXP ABAP Statements



Get Example source ABAP code based on a different SAP table
  



ASSIGN, writable_exp
Short Reference

ABAP_SYNTAX_FORMS

Constructor Expressions
1 ... NEW class( ... )->attr$|(attr_name) ...
2 ... CAST type( ... )->*$| dobj$|(dobj_name) ...

Table Expressions
3 ... table_exp ...

ABAP_ALTERNATIVES:
1 ... NEW class( ... )->attr$|(attr_name)
2 ... CAST type( ... )->...
3 ... table_exp

What does it do?
These variants for specifying the memory area mem_area of the statement ASSIGN exploit the fact that the operand position after ASSIGN is a result position in which writable expressions can be specified.



Latest notes:

Writable expressions can be specified for the memory area but no other expressions, because only writable expressions can have a non-temporary result. Assigning a temporary data object to a field symbol would not make sense.
Writable expressions cannot be specified dynamically in a data object name.
NON_V5_HINTS
ABAP_HINT_END

ABAP Alternative 1 ... NEW class( ... )->attr$|(attr_name)

What does it do?
This variant for specifying the memory area mem_area of the statement ASSIGN assigns an attribute of an instance of class class to a field symbol. The instance of the class is created inline with the constructor expression NEW class( ... ). The object component selector -> following the constructor expression selects the attribute.
The attribute can be specified statically as attr or dynamically as the content of a character-like data object attr_name in parentheses.
For static specification attr, the same rules apply as to statically specifying the memory area, but no offsets/lengths can be specified and sy-subrc is not set by the ASSIGN statement itself.
For dynamic specification, the rules of a dynamic target behind the object component selector apply.
In this variant, the return code sy-subrc is not set by the statement ASSIGN itself, but by the constructor expression. The instance operator NEW sets the return code sy-subrc to 0, if the object is created successfully.
If the attribute specified dynamically in attr_name is not found, an exception of class CX_SY_ASSIGN_ILLEGAL_COMPONENT is raised. An assignment of the constructor operator NEW is either successful or leads to an exception and the addition ELSE UNASSIGN cannot be used. In case of an exception, an existing field symbol keeps it previous state.



Latest notes:

Assigning an attribute attr of an object created using NEW to a field symbol persists this object as long as the field symbol points to the attribute.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Constructor expression with NEW in the specified memory area of statement ASSIGN statements. The assignment of the attribute attr to a field symbol persists the object. In the first assignment the attribute is specified statically, in the following it is specified dynamically. The third assignment raises an exception since the attribute is not found.
ABEXA 00036
ABAP_EXAMPLE_END

ABAP Alternative 2 ... CAST type( ... )->...

What does it do?
This variant for specifying the memory area mem_area of the statement ASSIGN assigns a data object that is referenced by the result of the constructor expression CAST type( ... ) to a field symbol. The object component selector -> following the constructor expression selects the data object. For type, a data type or an object type (class or interface) can be specified.
The data object assigned to the field symbol can be specified with the following CAST expressions combined with the object component selector:
CAST dtype( ... dref )->*
CAST dtype( ... dref )->comp|('comp_name')
CAST class|intf( ... oref )->attr|('attr_name')
The same rules apply as described for CAST with the exception that no offset/length specification can be specified. Especially, the system behavior in case of an error is governed by the CAST expression. The return code sy-subrc is not set. An assignment of the constructor operator CAST is either successful or leads to an exception and the addition ELSE UNASSIGN must not be used. If an exception occurs, an existing field symbol keeps it previous state.



Latest notes:

In the ASSIGN statement, the CAST operator cannot be used for calling a method.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
This example is the same as one shown under CAST. Instead of assigning CAST expressions to data objects, they are assigned to field symbols here. The example shows that the system behavior in the case of an error in the CAST expression is the same in both examples.
ABEXA 00037
ABAP_EXAMPLE_END

ABAP Alternative 3 ... table_exp

What does it do?
This variant for specifying the memory area mem_area of the statement ASSIGN 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.
If a single table expression is specified, or a chaining whose last position is a table expression, the entire line found is assigned to the field symbol.
If a chaining is specified whose last position is a structure component after a structure component selector, this component is assigned to the field symbol. No offsets/lengths, however, can be specified for the structure component here.
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. If sy-subrc is set to 4 or 8, the state of the field symbol depends on the addition ELSE UNASSIGN:
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.
Unlike in other use cases of table expressions, the system field sy-tabix is set here in the same way as in a corresponding READ TABLE statement.
In this variant of the statement ASSIGN, the addition CASTING can only be specified in assignments to an existing field symbol and not in inline declarations, and only as a standalone addition. The addition RANGE cannot be specified.



Latest notes:

This variant of the statement ASSIGN can be regarded as a different form of READ TABLE ... ASSIGNING ....
More specifically, the value of sy-subrc and sy-tabix is set as in the statement READ TABLE and
the addition CASTING cannot be specified after an inline declaration for the field symbol. Unlike READ TABLE, chainings can also be used to assign components of read lines or lines of nested internal tables.
BEGIN_SECTION SAP_INTERNAL_HINT
After setting a breakpoint at statement READ TABLE the debugger does stop here!
END_SECTION SAP_INTERNAL_HINT
The constructor operators VALUE and REF used to control the result of the table expression cannot be used here.
If the specified line is not found, an exception is not raised, unlike in other uses of table expressions.
If ELSE UNASSIGN is not specified, it is not sufficient to evaluate the predicate expression <(><)> IS ASSIGNED, but sy-subrc must be checked. If ELSE UNASSIGN is specified, the predicate expression as well as sy-subrc can be evaluated.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
This example works in the same way as the example for READ TABLE ... ASSIGNING .... Here, the READ statement is replaced by an ASSIGN statements and the required component is assigned directly.
ABEXA 00038
ABAP_EXAMPLE_END

Return to menu