Posted: Tue Sep 11, 2007 8:54 pm Post subject: Field Symbol Sample Source
1. Field Symbols
Code:
DATA int TYPE i VALUE 15.
FIELD-SYMBOLS <fs_int> TYPE i.
ASSIGN int TO <fs_int>.
WRITE: / int, <fs_int>.
<fs_int> = 17.
WRITE: / int, <fs_int>.
UNASSIGN <fs_int>.
IF <fs_int> IS ASSIGNED.
WRITE: / int, <fs_int>.
ELSE.
WRITE: / 'field symbol not assigned'(fna).
ENDIF.
Quote:
15 15
17 17
field symbol not assigned
2. References and Field Symbols
Code:
DATA int TYPE i VALUE 15.
FIELD-SYMBOLS <fs_int> LIKE int.
DATA d_ref1 TYPE REF TO data.
DATA d_ref2 LIKE d_ref1.
GET REFERENCE OF int INTO d_ref1.
d_ref2 = d_ref1.
ASSIGN d_ref2->* TO <fs_int>.
WRITE: / int, <fs_int>.
Quote:
15 15
3.
Code:
DATA: BEGIN OF STR,
A VALUE 'a',
B VALUE 'b',
C VALUE 'c',
D VALUE 'd',
END OF STR,
CN(5) VALUE 'D'.
FIELD-SYMBOLS <FS> TYPE ANY.
DO 3 TIMES.
ASSIGN COMPONENT SY-INDEX OF
STRUCTURE STR TO <FS>.
IF SY-SUBRC <> 0. EXIT. ENDIF.
WRITE <FS>.
ENDDO.
ASSIGN COMPONENT CN OF STRUCTURE STR TO <FS>.
WRITE <FS>.
Quote:
Output: a b c d
4.
Code:
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) VALUE 'COL3'.
FIELD-SYMBOLS: <F1>, <F2>, <F3>.
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>.
The output is:
Quote:
11 22 33
33
5. Example of Dynamic Type Casting -> Bad !
Code:
PARAMETERS: pa_dbtab TYPE dd02l-tabname.
DATA dummy TYPE i. "address of line mod 4 has to be zero !!!
DATA: line(65535).
FIELD-SYMBOLS: <fs_wa> TYPE ANY, <fs_comp> TYPE ANY.
SELECT * FROM (pa_dbtab) INTO line.
ASSIGN line TO <fs_wa> CASTING TYPE (pa_dbtab).
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_wa> TO <fs_comp>.
IF sy-subrc NE 0.
SKIP.
EXIT.
ENDIF.
WRITE <fs_comp>.
ENDDO.
ENDSELECT.
** Suppose pa_dbtab's value is SFLIGHT
Quote:
100 UA 0941 15.03.2002 3.313,30 USD A310-300 280 1 0,00
100 UA 0941 16.03.2002 3.313,30 USD A310-300 280 43 134.586,23
100 UA 3504 09.03.2002 3.313,30 USD RJ115 128 43 134.586,23
100 UA 3504 10.03.2002 3.313,30 USD RJ115 128 43 134.586,23
100 UA 3504 11.03.2002 3.313,30 USD RJ115 128 43 134.586,23
100 UA 3504 12.03.2002 3.313,30 USD RJ115 128 43 134.586,23
100 UA 3504 13.03.2002 3.313,30 USD RJ115 128 43 134.586,23
6. Declaring Data Object Dynamically -> Good !
Code:
PARAMETERS: pa_dbtab(30) DEFAULT 'SFLIGHT'.
DATA: d_ref TYPE REF TO data.
FIELD-SYMBOLS: <fs_wa> TYPE ANY, <fs_comp> TYPE ANY.
CREATE DATA d_ref TYPE (pa_dbtab).
ASSIGN d_ref->* TO <fs_wa>.
SELECT * FROM (pa_dbtab) INTO <fs_wa>.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_wa> TO <fs_comp>.
IF sy-subrc NE 0.
SKIP.
EXIT.
ENDIF.
WRITE <fs_comp>.
ENDDO.
ENDSELECT.
Quote:
100 UA 0941 15.03.2002 3.313,30 USD A310-300 280 1 0,00
100 UA 0941 16.03.2002 3.313,30 USD A310-300 280 43 134.586,23
100 UA 3504 09.03.2002 3.313,30 USD RJ115 128 43 134.586,23
100 UA 3504 10.03.2002 3.313,30 USD RJ115 128 43 134.586,23
100 UA 3504 11.03.2002 3.313,30 USD RJ115 128 43 134.586,23
100 UA 3504 12.03.2002 3.313,30 USD RJ115 128 43 134.586,23
100 UA 3504 13.03.2002 3.313,30 USD RJ115 128 43 134.586,23
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.