ASSIGN


Variants



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

Variant 1

ASSIGN f TO <fs>.

Additions




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

Effect

Assigns the field f to the field symbol <fs>. The field symbol <fs> "points to" the contents of the field f at runtime, i.e. every change to the contents of f is reflected in <fs> and vice versa. If the field symbol <fs> 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 <fs>.

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:

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 <F>. ASSIGN NAME TO <F>. WRITE <F>.

Output: JOHN

Example

DATA: NAME(12) VALUE 'JACKJOHNCARL', X(10) VALUE 'XXXXXXXXXX'. FIELD-SYMBOLS <F>. ASSIGN NAME+4 TO <F>. WRITE <F>. ASSIGN NAME+4(*) TO <F>. WRITE <F>.

Output: JOHNCARLXXXX JOHNCARL

Example

DATA: NAME(12) VALUE 'JACKJOHNCARL', X(10) VALUE 'XXXXXXXXXX'. FIELD-SYMBOLS <F>. ASSIGN NAME+4 TO <F>. WRITE <F>. ASSIGN NAME+4(*) TO <F>. WRITE <F>.

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 <fs> . 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 <F>. ASSIGN LETTER TO <F>.

The field symbol has the type C and the output length 1.
ASSIGN LETTER TO <F> 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 <SALES_DEC5>. ASSIGN SALES_DEC2 TO <SALES_DEC5> DECIMALS 5. WRITE: / SALES_DEC2, / <SALES_DEC5>.

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 <fs> must also be defined locally in the subroutine.

Example

DATA X(4) VALUE 'Carl'. PERFORM U. FORM U. FIELD-SYMBOLS <F>. ASSIGN LOCAL COPY OF X TO <F>. WRITE <F>. MOVE 'John' TO <F>. WRITE <F>. WRITE X. ENDFORM.

Output: Carl John Carl

Variant 2

ASSIGN (f) TO <fs>.

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 <fs> " is not allowed.

Notes


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 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 <F>. ASSIGN (NAME) TO <F>. WRITE <F>.

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 <fs>.

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 <F>. MOVE 'XYZ_PROG' TO TRDIR-NAME. ASSIGN TABLE FIELD (NAME) TO <F>. WRITE <F>.

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 <F>. ASSIGN (NAME) TO <F>. WRITE <F>. 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 <F>. ASSIGN (NAME) TO <F>. WRITE <F>. MOVE 'G' TO NAME. ASSIGN (NAME) TO <F>. WRITE <F>. MOVE 'TRDIR-NAME' TO NAME. ASSIGN (NAME) TO <F>. WRITE <F>. ENDFORM.

Output: F_local G_global XYZ_PROG

Example

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

PROGRAM P1SUB. TABLES TFDIR. ... FORM U. FIELD-SYMBOLS <F>. DATA NAME(30) VALUE 'TRDIR-NAME'. ASSIGN TABLE FIELD (NAME) TO <F>. WRITE <F>. MOVE 'FCT_PROG' TO TFDIR-PNAME. ENDFORM.

FUNCTION-POOL FUN1. FUNCTION EXAMPLE. DATA NAME(30) VALUE 'TRDIR-NAME'. FIELD-SYMBOLS <F>. ASSIGN (NAME) TO <F>. IF SY-SUBRC = 0. WRITE <F>. 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 <F>. DATA NAME(30) VALUE 'X-NAME'. ASSIGN (NAME) TO <F>. WRITE <F>. ENDFORM.

Output: XYZ_PROG

Variant 4

ASSIGN LOCAL COPY OF MAIN TABLE FIELD (f) TO <f>.

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 <F>. MOVE 'XYZ_PROG' TO TRDIR-NAME. CALL FUNCTION 'EXAMPLE'.

FUNCTION-POOL FUN1. FUNCTION EXAMPLE. DATA NAME(30) VALUE 'TRDIR-NAME'. FIELD-SYMBOLS <F>. ASSIGN LOCAL COPY OF MAIN TABLE FIELD (NAME) TO <F>. IF SY-SUBRC = 0. WRITE <F>. 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 <fs>.

Variant 6

ASSIGN COMPONENT name OF STRUCTURE rec TO <fs>.

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 <fs>.

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 <FS>. DO 3 TIMES. ASSIGN COMPONENT SY-INDEX OF STRUCTURE REC TO <FS>. IF SY-SUBRC <> 0. EXIT. ENDIF. WRITE <FS>. ENDDO. ASSIGN COMPONENT CN OF STRUCTURE REC TO <FS>. WRITE <FS>.

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.

Index
SAP AG 1996