Skip to content

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

ASSIGN - writable_exp

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... NEW class( ... )->attr |
CAST type( ... )->dobj 
  | table_exp  ...

Alternatives

1. ... NEW class( ... )->attr | CAST type( ... )->dobj

2. ... table_exp

Effect

The operand position after ASSIGN is a result position in which writable expressions can be specified.


Note

Writable expressions can be specified for the memory are but not any 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.

Alternative 1

... NEW class( ... )->attr | CAST type( ... )->dobj

Effect

This alternative to specifying the memory area mem_area of the statement ASSIGN assigns the result of a constructor expression

to the field symbol. The same rules apply as when statically specifying the memory area, but no offsets/lengths can be specified.


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.
  • In this variant, the constructor expression in question sets the return code sy-subrc, not the statement ASSIGN.

  • If the object is created successfully, the instance operator NEW sets the return code sy-subrc to 0.

  • The casting operator CAST does not set the return code sy-subrc.

Example

Constructor expression with NEW in the specified memory area of the statement ASSIGN. The assignment of the attribute attr to a field symbol persists the object.

CLASS class DEFINITION. 
  PUBLIC SECTION. 
    DATA attr TYPE string VALUE 'foo'. 
ENDCLASS. 

START-OF-SELECTION. 
  ASSIGN NEW class( )->attr TO FIELD-SYMBOL(<fs>). 
  cl_demo_output=>display( <fs> ).

Example

Constructor expression with CAST in the specified memory area of ASSIGN statements.

TYPES: BEGIN OF t_struc, 
        col1 TYPE i, 
        col2 TYPE i, 
       END OF t_struc. 

DATA dref TYPE REF TO data. 
DATA struc TYPE t_struc. 

dref = NEW t_struc( ). 

ASSIGN CAST t_struc( dref )->col1 TO FIELD-SYMBOL(<col1>). 
ASSIGN CAST t_struc( dref )->col2 TO FIELD-SYMBOL(<col2>). 

Alternative 2

... table_exp

Effect

This alternative way of 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 row that was 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 row is found, sy-subrc is set to 0.
  • If the row is not found, 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.

Unlike when table expressions are used in other ways, the system field sy-tabix is set here in the same way as in a corresponding READ TABLE statement.

If the assignment is not successful, the field symbol keeps its previous state. In this variant, it is therefore not enough just to evaluate the predicate expression <fs> IS ASSIGNED; sy-subrc needs to be checked as well.

In the case of 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.


Notes

  • More specifically, the value of sy-subrc and sy-tabix is set as in the statement READ TABLE and
Unlike READ TABLE, chainings can be used here to assign components of read rows or rows from nested internal tables.

  • The constructor operators VALUE and REF used to control the result of the table expression cannot be used here.
  • If the specified row is not found, an exception is not raised (unlike in other uses of table expressions).

Example

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.

DATA: carrid TYPE sflight-carrid, 
      connid TYPE sflight-connid, 
      fldate TYPE sflight-fldate. 

... 

DATA sflight_tab TYPE SORTED TABLE OF sflight 
                 WITH UNIQUE KEY carrid connid fldate. 

SELECT * 
       FROM sflight 
       WHERE carrid = @carrid AND 
             connid = @connid 
       INTO TABLE @sflight_tab. 

IF sy-subrc = 0. 
  ASSIGN sflight_tab[ KEY primary_key COMPONENTS 
                         carrid = carrid 
                          connid = connid 
                          fldate = fldate ]-price 
         TO FIELD-SYMBOL(<price>). 
  IF sy-subrc = 0. 
    <price> *= '0.9'. 
  ENDIF. 
ENDIF.