SAP ASSIGN ABAP Statements



Get Example source ABAP code based on a different SAP table
  


ID ASSIGN
• ASSIGN ABAP Statement
• TO ASSIGN

ASSIGN
Short Reference

ABAP_SYNTAX
ASSIGN mem_area TO <(><)> casting_spec
range_spec
$[ELSE UNASSIGN $].

What does it do?
This statement assigns the memory area specified using mem_area to the field symbol <(><)>. A data object or a memory area calculated from the address of a data object can be assigned. After a successful assignment, the field symbol refers to the assigned memory area and can be used in operand positions. When used in a statement, it works like a dereferenced data reference, meaning that the statement works with the content of the memory area.
The following can be specified for <(><)>:
An existing field symbol with appropriate typing.
An inline declaration FIELD-SYMBOL( <(><)>). The typing depends on the mem_area specified.
The data type used to handle the assigned memory area depends on the specification in casting_spec. Either an explicit casting can be performed, or the field symbol inherits the data type of the data object specified in the assignment. In both cases, the data type used must match the typing of the field symbol. A field symbol to which a memory area is assigned, has this data type after the assignment and behaves like a data object of this type.
The assigned memory area mem_area must be at least as long as the data type specified in casting_spec and must have at least the same alignment. If the data type determined in casting_spec is deep, the deep components must correspond exactly in their type and position.
The specification in range_spec is used to define the memory area that can be assigned to the field symbol.
If the assignment is not successful. the behavior 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 ABAP Alternative 3@3@> 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 ABAP Alternative 1@1@>NEW ABAP Alternative 2@2@> 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.
System Fields
The ASSIGN statement sets the return code for a dynamic assignment, an assignment of dynamic components, a dynamic access or an ABAP Alternative 3@3@> assignment of a table expression. For an ABAP Alternative 1@1@>assignment of the constructor operator NEW, the return code is set by the constructor operator. The return code is not set for a static assignment and an ABAP Alternative 2@2@>assignment of the constructor operator CAST. sy-subrcMeaning 0Assignment was successful. 4Assignment was not successful. 8Assignment of table expression was not successful.



Latest notes:

The state of the field symbol can be checked with the predicate expression <(><)> IS ASSIGNED.
When setting field symbols using ASSIGN, permission to access the assigned data object is only checked at the position of the statement. The field symbol can then be passed on and used to access the assigned data object in any position. To prevent access to private and read-only attributes using field symbols outside classes, field symbols for these attributes should not be published externally. A constant a read-only input parameter or an immutable variable, however, can never be made modifiable by passing a field symbol.
NON_V5_HINTS
An obsolete form of the statement ASSIGN is ASSIGN LOCAL COPY.
ABAP_HINT_END

ABAP_EXAMPLE_VX
Three field symbols are assigned the subfields for year, month, and day of the system field sy-datlo. The subfields can then be addressed using field symbols.
ABEXA 00015
ABAP_EXAMPLE_END

ABAP_EXAMPLE_V5
Three field symbols are assigned the subfields for year, month, and day of the system field sy-datlo. The subfields can then be addressed using field symbols.
ABEXA 01615
ABAP_EXAMPLE_END