Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Assignments →  Assigning References →  Setting Field Symbols →  ASSIGN 

ASSIGN - range_spec

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... { } 
  | {RANGE range}.

Alternatives

1. ... { }

2. ... RANGE range

Effect

When specified, range_spec defines the area limits within which a memory area can be assigned to the field symbol. Either nothing can be specified here or the addition RANGE.

At the same time, the statement ASSIGN assigns these area limits to the field symbol <fs>. If the field symbol <fs> is itself used in a subsequent ASSIGN statement to specify a memory area mem_area, the assigned memory areas are used for determining the area limits of the field symbol that is being assigned.


Note

The area limits assigned to a field symbol using range_spec only apply to the following ASSIGN statements. In other statements, with the exception of ADD UNTIL, the general rules apply.

Alternative 1

... { }

Effect

If no value is specified for range_spec, the area limits are defined as follows:

  • If an elementary data object was specified for dobj in mem_area, the memory area of this data object determines the area limits.
  • If a field symbol was specified for dobj in mem_area, and this field symbol has an elementary data object assigned to it, the field symbol <fs> of the current statement takes on the area limits assigned to this field symbol.
  • If a structure or a field symbol was specified for dobj in mem_area, and one of these points to a structure, the system checks whether the structure has a character-like initial part (up to the first alignment gap). This then determines the area limits.

If these area limits are exceeded, no memory area is assigned for the static variant of mem_area after the ASSIGN statement. Also, the predicate expression <fs> IS ASSIGNED is incorrect, while sy-subrc is set to 4 in the dynamic variant.


Example

In the first ASSIGN statement, the area limits of the data object text are assigned to <fs1>. In the second ASSIGN statement, <fs2> takes on these limits. From the sixth pass, an attempt is made to assign a larger memory area to <fs2>, which makes the logical expression after IF false.

DATA text TYPE c LENGTH 8 VALUE '12345678'. 

FIELD-SYMBOLS: <fs1> TYPE ANY, 
               <fs2> TYPE ANY. 

ASSIGN text+3(3) TO <fs1>. 

DO 8 TIMES. 
  ASSIGN <fs1>(sy-index) TO <fs2>. 
  IF <fs2> IS ASSIGNED. 
    cl_demo_output=>write_text( |{ <fs2> }| ). 
  ENDIF. 
ENDDO. 
cl_demo_output=>display( ). 

Alternative 2

... RANGE range

Effect

If the addition RANGE is specified in range_spec, the area limits are defined by the data area of a range data object. range expects a data object of any data type. This object must cover the area limits when the RANGE addition is not specified (see above). If it is established at runtime that range does not cover these area limits, a non-handleable exception is raised.

When the RANGE addition is used, only subareas of the range data object can be assigned to the field symbol. If these area limits are exceeded, no memory area is assigned for the static variant of mem_area after the ASSIGN statement. Also, the predicate expression <fs> IS ASSIGNED is incorrect, while sy-subrc is set to 4 in the dynamic variant.

The addition RANGE cannot be used with the addition CASTING TYPE HANDLE or for assigning table expressions.


Notes

  • A RANGE addition does not remove the type-specific rules for substring accesses. To enable substring access beyond the field limits of a specified data object dobj, substring access must be possible in principle. In particular, substring access is never possible beyond the limits of a structure, since the character-like initial part is not accessed in this case.
  • If a structure is specified for range that contains dynamic data objects, they only contribute the internal reference to the data area of the structure. The actual data area of the dynamic data objects is paged out and is ignored by RANGE. This also applies to substructures that are declared as boxed components.

Example

The struc structure is constructed from ten components col1_1, col2_1, ..., col1_5, col2_5. The ASSIGN statement assigns the memory area of two neighboring components to the structure-typed field symbol <sub>, one after the other. Here the memory area is determined by the common name of the first two components comp1 in the structure struc and the specification of INCREMENT. Without the RANGE addition, the WHILE loop would only execute once since it would only be possible to access the memory area of struc-comp1. The RANGE addition causes the loop to be passed five times. The components of the field symbol can be accessed after the assignment.

TYPES: BEGIN OF sub_struc, 
          col1 TYPE c LENGTH 10, 
          col2 TYPE c LENGTH 10, 
       END OF sub_struc. 

DATA BEGIN OF struc. 
INCLUDE TYPE: sub_struc AS comp1 RENAMING WITH SUFFIX _1, 
             sub_struc AS comp2 RENAMING WITH SUFFIX _2, 
              sub_struc AS comp3 RENAMING WITH SUFFIX _3, 
              sub_struc AS comp4 RENAMING WITH SUFFIX _4, 
              sub_struc AS comp5 RENAMING WITH SUFFIX _5. 
DATA END OF struc. 

FIELD-SYMBOLS <sub> TYPE sub_struc. 

struc = VALUE #( col1_1 = 'col1_1'  col2_1 = 'col2_1' 
                 col1_2 = 'col1_2'  col2_2 = 'col2_2' 
                 col1_3 = 'col1_3'  col2_3 = 'col2_3' 
                 col1_4 = 'col1_4'  col2_4 = 'col2_4' 
                 col1_5 = 'col1_5'  col2_5 = 'col2_5' ). 

DATA inc TYPE i. 

WHILE sy-subrc = 0. 
  inc = sy-index  - 1. 
  ASSIGN struc-comp1 INCREMENT inc TO <sub> CASTING 
                                           RANGE struc. 
  IF sy-subrc = 0. 
    cl_demo_output=>write_data( <sub> ). 
  ENDIF. 
ENDWHILE. 
cl_demo_output=>display( ).