【ABAP】How To ASSIGN COMPONENT 〜 OFASSIGN COMPONENT

Introduction

I write an article for 「How to ASSIGN COMPONENT 〜 OFASSIGN COMPONENT」.Because,There were few articles about it.

Useful for thoes who use ABAP.

Meaning of 「ASSIGN COMPONENT 〜 OFASSIGN COMPONENT」

ASSIGN COMPONENT <com> OF STRUCTURE <st> TO <fs>.

You can assign the field synbol <fs> to the specified compornent  name  <com> in structure name <st>.

Example:

*structure name:profile
DATA:BEGI OF profile,
    name TYPE C VALUE 'TARO', “name:taro”
    age  TYPE I VALUE 10,     "10 age”
END OF profile.

*specified compornent
DATA co TYPE C VALUE "age".

*Define field symbol 
FIELD-SYNBOLS:<fs> TYPE ANY.
ASSIGN COMPONENT co OF STRUCTURE profile TO <fs>.
WRITE <fs>.
Output Result:10

For source code like the one above.

「ASSIGN COMPONENT <co> OF STRUCTURE <profile> TO <fs>.」is doing the following

①Specify “age” with <co> from the structure <profile> that has components “name” and “age”.

②Assign the field symbol <fs> to the specified “age”

③When <fs> is output, the value “10” of age is output.

Application

If you want to assign everything in the structure to a field symbol.

*Structure name:profile
DATA:BEGI OF profile,
    name TYPE C VALUE 'TARO', “name:Taro
    age  TYPE I VALUE 10,     "10 age”
END OF profile.

*Define field symbol
FIELD-SYNBOLS:<fs> TYPE ANY.
DO 2 TIMES
    ASSIGN COMPONENT sy-index OF STRUCTURE profile TO <fs>.
    WRITE <fs>.
ENDDO.
Output Result:10

Processes as many components as there are in the structure, The components are assigned to <fs> one by one and output.

Follow me!

コメントする

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です