ABAP--Field Symbol 的Example(来自SAP的样例)
Full type specification
REPORT demo_field_symbols_type .
DATA: BEGIN OF line,
col1(1) TYPE c,
col2(1) TYPE c VALUE 'X',
END OF line.
FIELD-SYMBOLS <fs> LIKE line.
ASSIGN line TO <fs>.
MOVE <fs>-col2 TO <fs>-col1.
WRITE: <fs>-col1, <fs>-col2.
Forcing structures
REPORT demo_field_symbols_structure .
DATA: wa(10) TYPE c VALUE '0123456789'.
DATA: BEGIN OF line1,
col1(3) TYPE c,
col2(2) TYPE c,
col3(5) TYPE c,
END OF line1.
DATA: BEGIN OF line2,
col1(2) TYPE c,
col2 TYPE sy-datum,
END OF line2.
* obsolete -------------------------------------------------------------
FIELD-SYMBOLS: <f1> STRUCTURE line1 DEFAULT wa,
<f2> STRUCTURE line2 DEFAULT wa.
* correct --------------------------------------------------------------
FIELD-SYMBOLS <f3> LIKE line1.
ASSIGN wa TO <f3> CASTING.
FIELD-SYMBOLS <f4> LIKE line2.
ASSIGN wa TO <f4> CASTING.
* ----------------------------------------------------------------------
WRITE: / <f1>-col1, <f1>-col2, <f1>-col3,
/ <f2>-col1, <f2>-col2.
SKIP.
WRITE: / <f3>-col1, <f3>-col2, <f3>-col3,
/ <f4>-col1, <f4>-col2.
Static assign
REPORT demo_field_symbols_stat_assign .
FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i.
DATA: text(20) TYPE c VALUE 'Hello, how are you?',
num TYPE i VALUE 5,
BEGIN OF line1,
col1 TYPE f VALUE '1.1e+10',
col2 TYPE i VALUE '1234',
END OF line1,
line2 LIKE line1.
ASSIGN text TO <f1>.
ASSIGN num TO <f2>.
DESCRIBE FIELD <f1> LENGTH <f2>.
WRITE: / <f1>, 'has length', num.
ASSIGN line1 TO <f1>.
ASSIGN line2-col2 TO <f2>.
MOVE <f1> TO line2.
ASSIGN 'LINE2-COL2 =' TO <f1>.
WRITE: / <f1>, <f2>.
Assign with offset
REPORT demo_field_symbols_stat_as_off .
FIELD-SYMBOLS <fs> TYPE ANY.
DATA: BEGIN OF line,
string1(10) VALUE '0123456789',
string2(10) VALUE 'abcdefghij',
END OF line.
WRITE / line-string1+5.
ASSIGN line-string1+5 TO <fs>.
WRITE / <fs>.
ASSIGN line-string1+5(*) TO <fs>.
WRITE / <fs>.
REPORT demo_field_symbols_stat_as_of2 .
FIELD-SYMBOLS <fs> TYPE ANY.
DATA: BEGIN OF line,
a TYPE c VALUE '1', b TYPE c VALUE '2',
c TYPE c VALUE '3', d TYPE c VALUE '4',
e TYPE c VALUE '5', f TYPE c VALUE '6',
g TYPE c VALUE '7', h TYPE c VALUE '8',
END OF line,
off TYPE i,
len TYPE i VALUE 2.
DO 2 TIMES.
off = sy-index * 3.
ASSIGN line-a+off(len) TO <fs>.
<fs> = 'XX'.
ENDDO.
DO 8 TIMES.
off = sy-index - 1.
ASSIGN line-a+off(1) TO <fs>.
WRITE <fs>.
ENDDO.
Dynamic assign
REPORT demo_field_symbols_dynami_as_2 .
TABLES sbook.
DATA: name1(20) TYPE c VALUE 'SBOOK-FLDATE',
name2(20) TYPE c VALUE 'NAME1'.
FIELD-SYMBOLS <fs> TYPE ANY.
ASSIGN TABLE FIELD (name1) TO <fs>.
WRITE: / 'SY-SUBRC:', sy-subrc.
ASSIGN TABLE FIELD (name2) TO <fs>.
WRITE: / 'SY-SUBRC:', sy-subrc.
Assigning field symbols
REPORT demo_field_symbols_dynami_as_3 .
DATA: BEGIN OF s,
a TYPE c VALUE '1', b TYPE c VALUE '2', c TYPE c VALUE '3',
d TYPE c VALUE '4', e TYPE c VALUE '5', f TYPE c VALUE '6',
g TYPE c VALUE '7', h TYPE c VALUE '8',
END OF s.
DATA off TYPE i.
FIELD-SYMBOLS <fs> TYPE ANY.
ASSIGN s-a TO <fs>.
DO 4 TIMES.
off = sy-index - 1.
ASSIGN <fs>+off(1) TO <fs>.
WRITE <fs>.
ENDDO.
Assigning a structure by component
REPORT demo_field_symbols_assign_comp .
DATA: BEGIN OF line,
col1 TYPE i VALUE '11',
col2 TYPE i VALUE '22',
col3 TYPE i VALUE '33',
END OF line.
DATA comp(5) TYPE c VALUE 'COL3'.
FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE ANY, <f3> TYPE ANY.
ASSIGN line TO <f1>.
ASSIGN comp TO <f2>.
DO 3 TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE <f1> TO <f3>.
WRITE <f3>.
ENDDO.
ASSIGN COMPONENT <f2> OF STRUCTURE <f1> TO <f3>.
WRITE / <f3>.
Casting with field symbol type
REPORT demo_field_symbols_casting.
TYPES: BEGIN OF t_date,
year(4) TYPE n,
month(2) TYPE n,
day(2) TYPE n,
END OF t_date.
FIELD-SYMBOLS <fs> TYPE t_date.
ASSIGN sy-datum TO <fs> CASTING.
WRITE / sy-datum.
SKIP.
WRITE: / <fs>-year , / <fs>-month, / <fs>-day.
Casting with explicit type
REPORT demo_field_symbols_casting_typ.
TYPES: BEGIN OF t_date,
year(4) TYPE n,
month(2) TYPE n,
day(2) TYPE n,
END OF t_date.
FIELD-SYMBOLS: <fs> TYPE ANY,
<f> TYPE n.
ASSIGN sy-datum TO <fs> CASTING TYPE t_date.
WRITE / sy-datum.
SKIP.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs> TO <f>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
WRITE / <f>.
ENDDO.
Casting with predefined data types
REPORT demo_field_symbols_assign_type .
DATA txt(8) TYPE c VALUE '19980606'.
DATA mytype(1) VALUE 'X'.
FIELD-SYMBOLS <fs> TYPE ANY.
ASSIGN txt TO <fs>.
WRITE / <fs>.
SKIP.
* obsolete -------------------------------------------------------------
ASSIGN txt TO <fs> TYPE 'D'.
WRITE / <fs>.
ASSIGN txt TO <fs> TYPE mytype.
WRITE / <fs>.
SKIP.
* correct --------------------------------------------------------------
ASSIGN txt TO <fs> CASTING TYPE d.
WRITE / <fs>.
ASSIGN txt TO <fs> CASTING TYPE (mytype).
WRITE / <fs>.
Casting decimla places
REPORT demo_field_symbols_assign_deci .
DATA: pack1 TYPE p DECIMALS 2 VALUE '400',
pack2 TYPE p DECIMALS 2,
pack3 TYPE p DECIMALS 2.
FIELD-SYMBOLS: <f1> TYPE ANY ,
<f2> TYPE ANY.
WRITE: / 'PACK1', pack1.
SKIP.
* obsolete -------------------------------------------------------------
ASSIGN pack1 TO <f1> DECIMALS 1.
WRITE: / '<F1> ', <f1>.
pack2 = <f1>.
WRITE: / 'PACK2', pack2.
ASSIGN pack2 TO <f2> DECIMALS 4.
WRITE: / '<F2> ', <f2>.
pack3 = <f1> + <f2>.
WRITE: / 'PACK3', pack3.
<f2> = '1234.56789'.
WRITE: / '<F2> ', <f2>.
WRITE: / 'PACK2', pack2.
SKIP.
* correct --------------------------------------------------------------
ASSIGN pack1 TO <f1> CASTING TYPE p DECIMALS 1.
WRITE: / '<F1> ', <f1>.
pack2 = <f1>.
WRITE: / 'PACK2', pack2.
ASSIGN pack2 TO <f2> CASTING TYPE p DECIMALS 4.
WRITE: / '<F2> ', <f2>.
pack3 = <f1> + <f2>.
WRITE: / 'PACK3', pack3.
<f2> = '1234.56789'.
WRITE: / '<F2> ', <f2>.
WRITE: / 'PACK2', pack2.
Data areas for field symbols
REPORT demo_field_symbols_assign_err .
DATA: text1(10) TYPE c, text2(10) TYPE c, text3(5) TYPE c.
FIELD-SYMBOLS <fs> TYPE ANY.
DO 100 TIMES. "Runtime-Error!
ASSIGN text1+sy-index(1) TO <fs>.
ENDDO.
Data references
REPORT demo_data_reference.
TYPES: BEGIN OF t_struct,
col1 TYPE i,
col2 TYPE i,
END OF t_struct.
DATA: dref1 TYPE REF TO data,
dref2 TYPE REF TO data.
FIELD-SYMBOLS: <fs1> TYPE t_struct,
<fs2> TYPE i.
CREATE DATA dref1 TYPE t_struct.
ASSIGN dref1->* TO <fs1>.
<fs1>-col1 = 1.
<fs1>-col2 = 2.
dref2 = dref1.
ASSIGN dref2->* TO <fs2> CASTING.
WRITE / <fs2>.
GET REFERENCE OF <fs1>-col2 INTO dref2.
ASSIGN dref2->* TO <fs2>.
WRITE / <fs2>.