ASSIGN (ABAP Keyword)

ASSIGN (ABAP Keyword) introduction & details

ASSIGN

Variants

1. ASSIGN f TO
.
2. ASSIGN (f) TO .
3. ASSIGN TABLE FIELD (f) TO .
4.
ASSIGN LOCAL COPY OF MAIN TABLE FIELD (f) TO .
5. ASSIGN COMPONENT idx OF
STRUCTURE rec TO .
6. ASSIGN COMPONENT name OF STRUCTURE rec TO
.

Variant 1
ASSIGN f TO .

Additions

1.
… TYPE typ
2. … DECIMALS dec
3. … LOCAL COPY OF

Effect
Assigns the field f to the field symbol . The field
symbol “points to” the contents of the field f at runtime, i.e. every change
to the contents of f is reflected in and vice versa. If the field symbol
is not typed (see FIELD-SYMBOLS ), the field symbol adopts the type and
atrributes of the field f at runtime, particularly the conversion exit.
Otherwise, when the assignment is made, the system checks whether the type of
the field f matches the type of the field symbol .

Note
With the
ASSIGN statement, the offset and length specifications in field f (i.e. f+off ,
f+len or f+off(len) ) have a special meaning:

They may be variable and
thus not evaluated until runtime.
The system does not check whether the
selected area still lies within the field f .
If an offset is specified, but
no length, for the field f , the field symbol adopts the length of the field
f . Caution: also points to an area behind the field f . If you do not want
this, the offset and length specifications can be in the form ASSIGN f+off(*) TO
. . This means that the field symbol is set so that the field limits of
f are not exceeded.
In the ASSIGN statement, you can also use offset and
length specifications to access field symbols, FORM and function
parameters.
Warning: If the effect of the ASSIGN statement is to assign parts
of other fields beyond the limits of the field f , the changing of the contents
via the field symbol may mean that the data written to these fields does not
match the data type of these fields and thus later results in a runtime
error.

Note
Since the ASSIGN statement does not set any return
code value in the system field SY-SUBRC , subsequent program code should not
read this field.

Example

DATA NAME(4) VALUE
‘JOHN’.
FIELD-SYMBOLS .
ASSIGN NAME TO .
WRITE
.

Output: JOHN

Example

DATA: NAME(12) VALUE
‘JACKJOHNCARL’,
X(10) VALUE ‘XXXXXXXXXX’.
FIELD-SYMBOLS .
ASSIGN
NAME+4 TO .
WRITE .
ASSIGN NAME+4(*) TO .
WRITE
.

Output: JOHNCARLXXXX JOHNCARL

Example

DATA:
NAME(12) VALUE ‘JACKJOHNCARL’,
X(10) VALUE ‘XXXXXXXXXX’.
FIELD-SYMBOLS
.
ASSIGN NAME+4 TO .
WRITE .
ASSIGN NAME+4(*) TO .
WRITE
.

Output: JOHNCARLXXXX JOHNCARL

Addition 1
… TYPE
typ

Effect
With untyped field symbols, allows you to change the
current type of the field symbol to the type typ. The output length of the field
symbol is corrected according to its type.
With typed field symbols, this
addition should only be used if the type of the field f does not match the type
of the field symbol . The specified type type must be compatible with the
type of the field symbol. Since no conversion can be performed (as with MOVE ,
the system must be able to interpret f as a field with this type type .
The
type specification is in the form of a literal or a field. At present, only
system types ( C, D, T, P, X, N, F, I or W ) are allowed; you can also specify
type ‘s’ for 2-byte integer fields with a sign and type ‘b’ for 1-byte integer
fields without a sign (see also DESCRIBE FIELD ).

Note
This statement
results in a runtime error if the specified type is unknown or does not match
the field to be assigned (due to a missing alignment or an inappropriate
length).

Example

DATA LETTER TYPE C.
FIELD-SYMBOLS
.
ASSIGN LETTER TO .

The field symbol has the type C and the
output length 1.

ASSIGN LETTER TO TYPE ‘X’.

The field
symbol has the type X and the output length 2.

Addition 2
… DECIMALS
dec

Effect
This addition only makes sense when used with type P. The
field symbol contains dec decimal places.

Example
Output sales in
thousands:

DATA SALES_DEC2(10) TYPE P DECIMALS 2 VALUE
1234567.
FIELD-SYMBOLS .

ASSIGN SALES_DEC2 TO
DECIMALS 5.
WRITE: / SALES_DEC2,
/
.

Output:
1,234,567.00
1,234.56700

Note
This
statement results in a runtime error if the field symbol has a type other than P
at runtime or the specified number of decimal places is not in the range 0 to
14.

Addition 3
… LOCAL COPY OF …

Effect
With LOCAL COPY
OF , the ASSIGN statement can only be used in subroutines. This creates a copy
of f which points to the field symbol.

Note
The field symbol must
also be defined locally in the subroutine.

Example

DATA X(4) VALUE
‘Carl’.
PERFORM U.
FORM U.
FIELD-SYMBOLS .
ASSIGN LOCAL COPY OF X
TO .
WRITE .
MOVE ‘John’ TO .
WRITE .
WRITE
X.
ENDFORM.

Output: Carl John Carl

Variant 2
ASSIGN (f)
TO .

Additions

1. … TYPE typ
2. … DECIMALS
dec
3. … LOCAL COPY OF …

Effect
Assigns the field whose name is
stored in the field f to the field symbol.
The statement ” ASSIGN
(f)+off(len) TO ” is not allowed.

Notes
The search for the field
to be assigned is performed as follows:

If the statement is in a
subroutine or function module, the system first searches in this modularization
unit. If the statement lies outside any such modularization units or if the
field is not found there, the system searches for the field in the global data
of the program. If the field is not found there, the system searches in the
table work areas of the main program of the current program group declared with
TABLES

The name of the field to be assigned can also be the name of a
field symbol or formal parameter (or even a component of one of these, if the
field symbol or the parameter has a structure).
If the name of the field to
be assigned is of the form “(program name)field name”, the system searches in
the global fields of the program with the name “Program name” for the field with
the name “Field name”. However,it is only found if the program has already been
loaded.
Warning: This option is for internal use by specialists only.
Incompatible changes or developments may occur at any time without warning or
prior notice.

The return code value is set as
follows:

SY-SUBRC = 0 The assignment was successful.
SY_SUBRC = 4 The
field could not be assigned to the field symbol.

Example

DATA:
NAME(4) VALUE ‘XYZ’, XYZ VALUE ‘5’.
FIELD-SYMBOLS .
ASSIGN (NAME) TO
.
WRITE .

Output: 5

Addition 1
… TYPE
typ
Addition 2
… DECIMALS dec
Addition 3
… LOCAL COPY OF

Effect
See similar additions of variant 1.

Variant
3
ASSIGN TABLE FIELD (f) TO .

Effect
Identical to variant 2,
except that the system searches for the field f only in the data in the current
program group declared with TABLES .

The return code value is set as
follows:

SY-SUBRC = 0 The assignment was successful.
SY_SUBRC = 4
The field could not be assigned to the field
symbol.

Example

TABLES TRDIR.
DATA NAME(10) VALUE
‘TRDIR-NAME’.
FIELD-SYMBOLS .
MOVE ‘XYZ_PROG’ TO TRDIR-NAME.
ASSIGN
TABLE FIELD (NAME) TO .
WRITE .

Output:
XYZ_PROG

Example

TABLES T100.
T100-TEXT = ‘Global’.
PERFORM
EXAMPLE.
FORM EXAMPLE.
DATA: BEGIN OF T100, TEXT(20) VALUE ‘LOCAL’, END OF
T100,
NAME(30) VALUE ‘T100-TEXT’.
FIELD-SYMBOLS .
ASSIGN (NAME) TO
.
WRITE .
ENDFORM.

Output: Local – although the global
table field T100-TEXT has “global” contents. (This kind of name assignment of
work fields is, of course, not recommended.)

Example

TABLES
TRDIR.
DATA: F(8) VALUE ‘F_global’,
G(8) VALUE ‘G_global’.
MOVE
‘XYZ_PROG’ TO TRDIR-NAME.
PERFORM U.
FORM U.
DATA: F(8) VALUE
‘F_local’,
NAME(30) VALUE ‘F’.
FIELD-SYMBOLS .
ASSIGN (NAME) TO
.
WRITE .
MOVE ‘G’ TO NAME.
ASSIGN (NAME) TO .
WRITE
.
MOVE ‘TRDIR-NAME’ TO NAME.
ASSIGN (NAME) TO .
WRITE
.
ENDFORM.

Output: F_local G_global
XYZ_PROG

Example

PROGRAM P1MAIN.
TABLES TRDIR.
DATA NAME(30)
VALUE ‘TFDIR-PNAME’.
FIELD-SYMBOLS .
MOVE ‘XYZ_PROG’ TO
TRDIR-NAME.
PERFORM U(P1SUB).
ASSIGN (NAME) TO .
WRITE .
CALL
FUNCTION ‘EXAMPLE’.

PROGRAM P1SUB.
TABLES
TFDIR.

FORM U.
FIELD-SYMBOLS .
DATA NAME(30) VALUE
‘TRDIR-NAME’.
ASSIGN TABLE FIELD (NAME) TO .
WRITE .
MOVE
‘FCT_PROG’ TO TFDIR-PNAME.
ENDFORM.

FUNCTION-POOL
FUN1.
FUNCTION EXAMPLE.
DATA NAME(30) VALUE ‘TRDIR-NAME’.
FIELD-SYMBOLS
.
ASSIGN (NAME) TO .
IF SY-SUBRC = 0.
WRITE
.
ELSE.
WRITE / ‘TRDIR-NAME cannot be
accessed’.
ENDIF.
ENDFUNCTION.

Output: XYZ_PROG
FCT_PROG
TRDIR-NAME cannot be accessed

Example

TABLES
TRDIR.
MOVE ‘XYZ_PROG’ to TRDIR-NAME.
PERFORM U USING TRDIR.
FORM U
USING X STRUCTURE TRDIR.
FIELD-SYMBOLS .
DATA NAME(30) VALUE
‘X-NAME’.
ASSIGN (NAME) TO .
WRITE .
ENDFORM.

Output:
XYZ_PROG

Variant 4
ASSIGN LOCAL COPY OF MAIN TABLE FIELD (f) TO
.

Additions

1. … TYPE typ
2. … DECIMALS
dec

Effect
Identical to variant 3, except that the system searches
for the field whose name is in f steht only in the data in the program group of
the main program declared with TABLES . However, the field symbol then points
not directly to the found field, but to a copy of this field on theq value
stack.
This variant therefore ensures that any access to Dictionary fields of
an external program group is read only and no changes are
made.

Example

PROGRAM P1MAIN.
TABLES TRDIR.
DATA
NAME(30) VALUE ‘TFDIR-PNAME’.
FIELD-SYMBOLS .
MOVE ‘XYZ_PROG’ TO
TRDIR-NAME.
CALL FUNCTION ‘EXAMPLE’.

FUNCTION-POOL
FUN1.
FUNCTION EXAMPLE.
DATA NAME(30) VALUE ‘TRDIR-NAME’.
FIELD-SYMBOLS
.
ASSIGN LOCAL COPY OF MAIN
TABLE FIELD (NAME) TO .
IF SY-SUBRC =
0.
WRITE .
ELSE.
WRITE / ‘TRDIR-NAME cannot be
accessed’.
ENDIF.
ENDFUNCTION.

Output: XYZ_PROG

Addition
1
… TYPE typ
Addition 2
… DECIMALS dec

Effect
See similar
additions to variant 1.

Variant 5
ASSIGN COMPONENT idx OF STRUCTURE
rec TO .
Variant 6
ASSIGN COMPONENT name OF STRUCTURE rec TO
.

Additions

1. … TYPE typ
2. … DECIMALS
dec

Effect
If the field name or idx has the type C or if it is a field
string with no internal table, it is treated as a component name. Otherwise, it
is considered as a component number. The corresponding component of the field
string rec is assigned to the field symbol .

The return code value is
set as follows:

SY-SUBRC = 0 The assignment was
successful.
SY_SUBRC = 4 The field could not be assigned to the field
symbol.

Note
If idx has the value 0, the entire field string is
assigned to the field symbol.

Example

PROGRAM P1MAIN.
DATA:
BEGIN OF REC,
A VALUE ‘a’,
B VALUE ‘b’,
C VALUE ‘c’,
D VALUE
‘d’,
END OF REC,
CN(5) VALUE ‘D’.
FIELD-SYMBOLS .
DO 3
TIMES.
ASSIGN COMPONENT SY-INDEX OF
STRUCTURE REC TO .
IF SY-SUBRC
<> 0. EXIT. ENDIF.
WRITE .
ENDDO.
ASSIGN COMPONENT CN OF
STRUCTURE REC TO .
WRITE .

Output: a b c d

Addition
1
… TYPE typ
Addition 2
… DECIMALS dec

Effect
See similar
additions to variant 1.

Note
Runtime errors

Depending on the
operands, the ASSIGN statement can cause runtime errors
.

Note
Performance

For performance reasons, you are recommended
to use typed field symbols. The runtime for a typed ASSIGN statement amounts to
approx. 9 msn (standardized microseconds) against approx. 13 msn for an untyped
ASSIGN statement.