Entering content frameBasic Forms of the ASSIGN Statement Locate the document in its SAP Library structure

The ASSIGN 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 <f> TO <FS>.

When you assign the data object, the system checks whether the technical attributes of the data object <f> correspond to any type specifications for the field symbol <FS>. The field symbol adopts any generic attributes of <f> that are not contained in its own type specification. After the assignment, it points to <f> in 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 output is:

Hello, how are you? has length 20

LINE-COL2 = 1,234

The example declares two field symbols <F1> and <F2>. <F2> has the type I, which means that only type I fields may be assigned to it. <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 part of a field to a field symbol.

ASSIGN <f>[+<o>][(<l>)] TO <FS>.

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

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 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 ASSIGN statement. 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 is meaningful 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 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 ASSIGN statement:

ASSIGN (<f>) TO <FS>.

This statement assigns the field whose name is contained in the field <f> to the field symbol <FS>. You cannot use offset and length 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 does not exist 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 program. You should therefore only use the dynamic ASSIGN statement when 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 ASSIGN statement:

ASSIGN TABLE FIELD (<f>) TO <FS>.

The system then only searches within the table work areas in the main program of the current program group for the data object that is to be assigned to the field symbol. 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 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. In this case, SY-SUBRC is 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 ASSIGN statement.

ASSIGN <FS1>[+<o>][(<l>)] TO <FS2>.

in a static ASSIGN and:

ASSIGN [TABLE FIELD] (<f>) TO <FS2>.

in a dynamic ASSIGN, where the field <f> contains 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 one, 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 (that is, where the Unicode indicator is set in the ABAP Editor), the output is simply:

1

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

 

 

 

Leaving content frame