SAP ABAP SYNTAX FOR ASSIGN


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 run time, i.e. every change to the contents of f is reflected in and vice versa. If the field symbol is not typed 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 .
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.

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.

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.

RELATED POST

1 comment :

  1. Is there any limitation while using external assign in a badi? i currently have the problem that an external assign is not working within a badi. But the programm name is in the calling stack.

    ReplyDelete