SAP ASSIGN MEM AREA DYNAMIC DOBJ ABAP Statements



Get Example source ABAP code based on a different SAP table
  


• ( ) ASSIGN
• dref->* ASSIGN
• INCREMENT ASSIGN

ASSIGN, dynamic_dobj
Short Reference

ABAP_SYNTAX
... ${ (dobj_name) $}
$| ${ dref->* $}
$| ${ dobj INCREMENT inc $} ...

ABAP_ALTERNATIVES:
1 ... (dobj_name)
2 ... dref->*
3 ... dobj INCREMENT inc

What does it do?
These variants for specifying the memory area mem_area of the statement ASSIGN dynamically are used to dynamically access data objects.
In an inline declaration of the field symbol using FIELD-SYMBOL(<(><)>), its typing is performed with the generic type data.
In these 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. If the assignment is not successful, 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.



Latest notes:

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 Alternative 1 ... (dobj_name)
BEGIN_SECTION ID ASSIGN-DYNAMIC

What does it do?
In this dynamic variant of mem_area, the memory area is not specified directly, but as content of a character-like data object (dobj_name) in parentheses. The following can be specified for dobj_name:
Literal or constants If the data object dobj_name is specified as a character literal or as a constant, it can be evaluated statically, and the specified data object is recognized as the used object.
Variable If the data object dobj_name is specified as a variable, it is specified only dynamically, and the content is not evaluated statically.
When the statement is executed, dobj_name is not evaluated until runtime in both cases. The name in dobj_name is structured in exactly the same way as if specified directly: When executing the statement, the content of dobj_name must be the name of a data object which may contain offsets and lengths, structure component selectors, and other component selectors for assigning structure components, attributes in classes or attributes in objects. The content of dobj_name does not have to be in uppercase letters.
dobj_name can contain a chain of names consisting of component selectors. For an individual name or if the first name is followed by an object component selector (->), the specified data object is searched for according to the following hierarchy: If the statement is located in a procedure , the system searches for the local data objects of the procedure. If the statement is located in a method, the system searches in the visible attributes of a class in the method. In instance methods, this means a search of the static type of me (special case of cref->(attr_name) in dynamic_access). The system searches in the global data of the current program.
BEGIN_SECTION VERSION 5 OUT The system searches in the interface work areas of the main program of the current program group declared using TABLES.
END_SECTION VERSION 5 OUT If the statement is in an instance method, the system searches in the dynamic type of me (special case of cref->(attr_name) in dynamic_access).
If the data object is found and the name is followed by an object component selector (->), the search for the following names is continued from left to right, as described under dynamic_access.
If the first name is followed by a class component selector (=> ), the specified class is searched for, as described under dynamic_access, and the search is then continued accordingly from left to right.



Latest notes:

This variant should not be used any more. Especially for accessing structure components, other variants are preferable.
Writable expressions writable_exp cannot be specified dynamically in dobj_name .
For internal use only, the name in dobj_name can also have the form (PROG)DOBJ, where PROG is the name of an ABAP program and DOBJ the name of a global data object of this program, whereby the name is not case-sensitive. If the program PROG is loaded into the same ABAP_ISESS as the current program when the statement ASSIGN is executed, the data object DOBJ is searched for in this program and the field symbol points to this data object if the assignment was successful.
BEGIN_SECTION SAP_INTERNAL_HINT
DOBJ can also be a class name and there can be component selectors -, ->, => and chainings of those behind DOBJ to address visible components or attributes.
END_SECTION SAP_INTERNAL_HINT
Dynamically specifying a structure component using a structure component selector is less effective than using the addition COMPONENT OF STRUCTURE.
NON_V5_HINTS
For the latter see the executable example.
In an obsolete variant, the addition TABLE FIELD can be specified before dobj_name to restrict the search to table work areas.
If an attribute of a class of a different program is specified in dobj_name using an absolute type name and followed by the class component selector (=>), it is loaded into a new additional program group or into the current program group, depending on the program type, if it has not yet been loaded. Any existing program constructors are not executed, however, unlike in a real dynamic_access.
ABAP_HINT_END



Example ABAP Coding

Dynamic output of the content of any system field. The validity of the input is checked before it is dynamically assigned with (name) to field symbol syfield using classes of RTTI for structure SYST
ABEXA 00028
ABAP_EXAMPLE_END
END_SECTION ID ASSIGN-DYNAMIC

ABAP Alternative 2 ... dref->*
BEGIN_SECTION ID ASSIGN-DYNAMIC-DREF

What does it do?
When specifying a dereferenced data reference dref for mem_area using the dereferencing operator ->*, the memory area of the data object is assigned to the field symbol to which dref points. If the reference variable dref does not reference a data object, the assignment is not performed and sy-subrc is set to 4.
Unlike all other operand positions, where a data reference that does not point to a data object raises an exception, no exception occurs in the statement ASSIGN and sy-subrc is set to 4.
BEGIN_SECTION SAP_INTERNAL_HINT
The exception is either the uncatchable DATREF_NOT_ASSIGNED or the catchable CX_SY_ASSIGN_ILLEGAL_COMPONENT. The latter is raised for a dynamic target.
END_SECTION SAP_INTERNAL_HINT

ABAP_EXAMPLE_VX5
Assigning a dereferenced data reference that is initial. Here, no exception occurs.
ABEXA 00029
ABAP_EXAMPLE_END
END_SECTION ID ASSIGN-DYNAMIC-DREF

ABAP Alternative 3 ... dobj INCREMENT inc
BEGIN_SECTION ID ASSIGN-INCREMENT

What does it do?
This expression for mem_area assigns a memory area to the field symbol that has exactly the same length as the memory area of dobj and is incremented inc times this length in reference to the memory area of dobj. inc expects a numeric data object. A data object or a field symbol must be specified directly for dobj . Offset or length specifications or the dereferencing of a data reference are not possible. The field symbol cannot be declared via an inline declaration FIELD-SYMBOL( <(><)>).



Latest notes:

The dynamic ASSIGN variant with INCREMENT is designed for sequential access to similar memory areas that are located at regular intervals after each other, such as consecutive structure components of the same data type. In all other cases, ASSIGN ... INCREMENT should be used carefully. Note in particular:
Usually the addition RANGE must be used to defined the area, within which it is possible to work with INCREMENT.
The assigned memory area is handled using the data type dobj if the addition CASTING is not specified in casting_spec. This means that an implicit casting of the assigned memory areas to the data type of dobj is performed.
The typing check also refers to dobj, but is performed only when the statement is executed.
Runtime errors always occur if the following general rule is violated: If deep data objects that are in the assigned memory area do not match the typing in their type and position.
NON_V5_HINTS
ABAP_HINT_END



Example ABAP Coding

After the ASSIGN statement, the field symbol points to the fourth component col4. See the example for the addition RANGE.
ABEXA 00030
ABAP_EXAMPLE_END

ABAP_EXAMPLE_ABEXA
Field Symbols, ASSIGN INCREMENT
ABAP_EXAMPLE_END
END_SECTION ID ASSIGN-INCREMENT

Return to menu