Entering content frame

Basic Forms of the ASSIGN Statement Locate the document in its SAP Library structure

The statement has the following basic forms:

Static ASSIGN

If you already know the name of the field that you want to assign to the field symbol when you write a program, use the static ASSIGN statement:

ASSIGN dobj TO <fs>.

When you assign the data object, the system checks whether the technical attributes of the data object dobj  correspond to the type specifications for the field symbol <fs>. The field symbol adopts any generic attributes of dobj that are not contained in its own type specification. After the assignment, it points to this field in the memory.

Example

REPORT demo_field_symbols_stat_ASSIGN .

FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i.

DATA: text(20)  TYPE c VALUE 'Hello, how are you?',
      num       TYPE i VALUE 5,
      BEGIN OF line1,
        col1 TYPE f VALUE '1.1e+10',
        col2 TYPE i VALUE '1234',
      END OF line1,
      line2 LIKE line1.

ASSIGN text TO <f1>.
ASSIGN num TO  <f2>.
DESCRIBE FIELD <f1> LENGTH <f2>.
WRITE: / <f1>, 'has length', num.

ASSIGN line1 TO <f1>.
ASSIGN line2-col2 TO <f2>.
MOVE <f1> TO line2.
ASSIGN 'LINE2-COL2 =' TO <f1>.
WRITE: / <f1>, <f2>.

The list output is:

Hello, how are you?  has length         20

LINE-COL2 =      1,234

The example declares two field symbols <f1> and <f2>. <f2> may only have fields of type I since it has been typed accordingly. <f1> and <f2> both point to different fields during the program.

Static ASSIGN with Offset Specification

In a static ASSIGN statement, you can use positive offset and length specifications to assign a subfield to a field symbol.

ASSIGN dobj[+off][(len)] TO <fs>.

When you assign parts of fields to a field symbol, the following special conditions apply:

        The system does not check whether the selected part lies inside the field dobj. Both offset off  and length len can be larger than the length of dobj. You can address memory beyond the boundary of dobj, but not beyond the data areas for field symbols.

        If you do not specify the length len, the system automatically uses the length of the field dobj. If off is greater than zero, <fs> always points to an area beyond the limits of dobj.

        If off is smaller than the length of dobj, you can enter an asterisk (*) for len to prevent <fs> from referring to an address beyond the limits of dobj.

Example

REPORT demo_field_symbols_stat_as_off .

FIELD-SYMBOLS <fs> TYPE ANY.

DATA: BEGIN OF line,
        string1(10) VALUE '0123456789',
        string2(10) VALUE 'abcdefghij',
      END OF line.

WRITE / line-string1+5.

ASSIGN line-string1+5 TO <fs>.
WRITE / <fs>.

ASSIGN line-string1+5(*) TO <fs>.
WRITE / <fs>.

The list output is:

56789

56789abcde

56789

In this example, you can see the difference between an offset specification in a WRITE statement and an offset specification in an ASSIGNstatement. With WRITE , the output is truncated at the end of line-string1. Specifying an offset greater than 9 would lead to an error during the syntax check. In the first ASSIGN statement, the memory area of length 10 beginning with offset 5 in line-string1 is assigned to the field symbol <fs>. The output then makes sense because the memory area behind line-string1 belongs to line-string2. In the second ASSIGN statement, the length specification * prevents the system from addressing the memory area after line-string1.

Example

REPORT demo_field_symbols_stat_as_of2 .

FIELD-SYMBOLS <fs> TYPE ANY.

DATA: BEGIN OF line,
        a TYPE c VALUE '1', b TYPE c VALUE '2',
        c TYPE c VALUE '3', d TYPE c VALUE '4',
        e TYPE c VALUE '5', f TYPE c VALUE '6',
        g TYPE c VALUE '7', h TYPE c VALUE '8',
      END OF line,
      off TYPE i,
      len TYPE i VALUE 2.

DO 2 TIMES.
  off = sy-index * 3.
  ASSIGN line-a+off(len) TO <fs>.

  <fs> = 'XX'.
ENDDO.

DO 8 TIMES.
  off = sy-index - 1.
  ASSIGN line-a+off(1) TO <fs>.

  WRITE <fs>.
ENDDO.

The list output is:

1 2 3 X X 6 X X

The example show how field symbols can make it easier to access and manipulate regular structures. Note, however, that this flexible method of manipulating memory contents beyond field limits also has its dangers and may lead to runtime errors.

Dynamic ASSIGN

If you do not know the name of the field that you want to assign to the field symbol when you write a program, you can use a dynamic ASSIGNstatement:

ASSIGN (dobj) TO <fs>.

This statement assigns the data object whose name is contained in the dobj field to the field symbol <fs>. You cannot use subfields in a dynamic ASSIGN.

At runtime, the system searches for the corresponding data object in the following order:

...

       1.      If the ASSIGN statement is in a procedure, the system searches first in its local data.

       2.      If it cannot find the object in the local data (or if the ASSIGN statement is not in a procedure), it then looks in the local data of the program.

       3.      If the field is not found in the global data of the program, the system looks in the table work areas declared with the TABLES statement in the main program of the current program group. A program group consists of a main program and all of the programs that are loaded into the same internal session as a result of other program calls.

If the search is successful and a field can be assigned to the field symbol, sy-subrc is set to 0. Otherwise, it is set to 4, and the field symbol remains unchanged. For security reasons, you should always check the value of sy-subrc after a dynamic ASSIGN to prevent the field symbol pointing to the wrong area.

Searching for the field in this way slows down the response time. You should therefore only use the dynamic ASSIGNstatement where absolutely necessary. If you know when you create the program that you want to assign a table work area to the field symbol, you can also use the following variant of the dynamic ASSIGNstatement:

ASSIGN TABLE FIELD (dobj) TO <fs>.

The system then only searches for the data object to be assigned within the table work areas declared using TABLES in the main program of the current program group. This addition is forbidden in ABAP Objects, since the latter does not support table work areas.

Example

Suppose we have three programs. The main program:

REPORT demo_field_symbols_dynami_as_1.
TABLES sbook.
sbook-fldate = sy-datum.

PERFORM form1 IN PROGRAM demo_form1.

The other two programs are:

REPORT demo_form1.
FORM form1.
  PERFORM form2 IN PROGRAM demo_form2.
ENDFORM.

and

REPORT demo_form2.
FORM form2.
  DATA name(20) TYPE c VALUE 'SBOOK-FLDATE'.
  FIELD-SYMBOLS <fs> TYPE ANY.
  ASSIGN (name) TO <fs>.
  IF sy-subrc EQ 0.
    WRITE / <fs>.

  ENDIF.
ENDFORM.

The output looks something like this:

02.06.1998

The program group in the internal session now consists of the programs DEMO, MYFORMS1 and MYFORMS2. The field symbol <fs> is defined in MYFORMS2. After the dynamic ASSIGN statement, it points to the component FLDATE of the table work area SBOOK declared in the main program DEMO.

Example

REPORT demo_field_symbols_dynami_as_2 .

TABLES sbook.

DATA: name1(20) TYPE c VALUE 'SBOOK-FLDATE',
      name2(20) TYPE c VALUE 'NAME1'.

FIELD-SYMBOLS <fs> TYPE ANY.

ASSIGN TABLE FIELD (name1) TO <fs>.
WRITE: / 'sy-subrc:', sy-subrc.

ASSIGN TABLE FIELD (name2) TO <fs>.
WRITE: / 'sy-subrc:', sy-subrc.

The list output is:

sy-subrc:      0

sy-subrc:      4

In the first ASSIGN statement, the system finds the component FLDATE of the table work area SBOOK, and sy-subrc is set to 0. In the second ASSIGN statement, the system does not find the field name1 because it is declared by the DATA statement and not by the TABLES statement. sy-subrc is therefore set to 4.

Assigning Field Symbols

Instead of using the names of data objects, you can also assign field symbols to field symbols in all variants of the ASSIGNstatement.

ASSIGN <fs1>[+off][(len)] TO <fs2>.

in a static ASSIGN and:

ASSIGN [TABLE FIELD] (dobj) TO <fs2>.

in a dynamic ASSIGN, where the field dobjcontains the name of a field symbol <fs1>. <fs1> and <fs2> may be identical.

Example

Note that the same code in Unicode system will produce different results than in non-Unicode systems:

REPORT demo_field_symbols_dynami_as_3 .

DATA: BEGIN OF s,
        a TYPE c VALUE '1',
        b TYPE c VALUE '2',
        c TYPE c VALUE '3',
        d TYPE c VALUE '4',
        e TYPE c VALUE '5',
        f TYPE c VALUE '6',
        g TYPE c VALUE '7',
        h TYPE c VALUE '8',
      END OF s.

DATA off TYPE i.

FIELD-SYMBOLS <fs> TYPE ANY.

ASSIGN s-a TO <fs>.

DO 4 TIMES.
  off = sy-index - 1.
  IF <fs> IS ASSIGNED.

    ASSIGN <fs>+off(1) TO <fs>.
    IF <fs> IS ASSIGNED.
      WRITE <fs>.
    ENDIF.
  ENDIF.
ENDDO.

In non-Unicode systems, the output is as follows:

1 2 4 7

The program declares a structure with eight components s-a  to s-h, and fills them with the digits 1 to 8. These character strings are stored regularly in memory. The component s-a is assigned initially to the field symbol <fs>. The effect of the statements in the DO loop is:

Loop pass 1:

<fs> points to s-a, off is zero, and s-a is assigned to <fs>

Loop pass 2:

<fs> points to s-a, off is zero, and s-b is assigned to <fs>

Loop pass 3:

<fs> points to s-b, off is two, and s-d is assigned to <fs>

Loop pass 4:

<fs> points to s-d, off is three, and s-g is assigned to <fs>

In a Unicode environment (where the Unicode flag is set in the editor settings), the output is simply:

1

No data object is assigned to <fs> from the second loop pass onwards.

 

 

 

Leaving content frame