*&———————————————————————*
*& Report  ZASSIGN
*& 作成者:張玉剛
*&———————————————————————*
*& 作成日  :2009/08/03
*& 処理概要:ASSIGNの使い方
*&———————————————————————*

REPORT  ZASSIGN.

*&———————————————————————*
*& ASSIGN f TO <fs>
*&———————————————————————*
“ASSIGN f TO <fs>.

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

“結果:JOHN

DATA: NAME(12) VALUE ‘JACKJOHNCARL’,
      X(10)    VALUE ‘XXXXXXXXXX’.
FIELD-SYMBOLS <F>.
ASSIGN NAME+4(*) TO <F>.
“ASSIGN NAME+4(0) TO <F>. “0だめ
WRITE <F>.

“結果:JOHNCARL

*&———————————————————————*
*& … TYPE typ
*&———————————————————————*
“… TYPE typ
DATA LETTER TYPE C.
FIELD-SYMBOLS <F>.
ASSIGN LETTER TO <F>.

ASSIGN LETTER TO <F> TYPE ‘X’.

*&———————————————————————*
*& … DECIMALS dec
*&———————————————————————*
“… DECIMALS dec
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>.

“結果:
     “1,234,567.00
     “1,234.56700

*&———————————————————————*
*& … LOCAL COPY OF …
*&———————————————————————*

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.

“結果:Carl John Carl

*&———————————————————————*
*& ASSIGN (f) TO <fs>.
*&———————————————————————*
“ASSIGN (f) TO <fs>.
“ASSIGN (f)+off(len) TO <fs> ” is not allowed

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

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

“結果:5

*&———————————————————————*
*& ASSIGN TABLE FIELD (f) TO <fs>.
*&———————————————————————*
“ASSIGN TABLE FIELD (f) TO <fs>.

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

“結果:XYZ_PROG

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.

“結果:LOCAL

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.

“結果:F_local  G_global  XYZ_PROG

*&———————————————————————*
*& ASSIGN COMPONENT idx OF STRUCTURE rec TO <fs>.
*& ASSIGN COMPONENT name OF STRUCTURE rec TO <fs>.
*&———————————————————————*
“ASSIGN COMPONENT idx OF STRUCTURE rec TO <fs>.
“ASSIGN COMPONENT name OF STRUCTURE rec TO <fs>.

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

“結果:a b c d