2008 Mar 13 4:26 AM
Hi
Kindly guide me on use & significance of field symbols.
When to use it? What are advantages? Syntax?
Regards
Harshada
2008 Mar 13 4:30 AM
hi,
Field symbols are placeholders or symbolic names for other fields. They do not physically reserve space for a field, but point to its contents. A field symbol cam point to any data object. The data object to which a field symbol points is assigned to it after it has been declared in the program.
Whenever you address a field symbol in a program, you are addressing the field that is assigned to the field symbol. After successful assignment, there is no difference in ABAP whether you reference the field symbol or the field itself. You must assign a field to a field symbol before you can address it in a program.
Field symbols are similar to dereferenced pointers in C (that is, pointers to which the content operator * is applied). However, the only real equivalent of pointers in ABAP, that is, variables that contain a memory address (reference) and that can be used without the contents operator, are reference variables in ABAP Objects.
All operations programmed with field symbols are applied to the field assigned to it. For example, a MOVE statement between two field symbols moves the contents of the field assigned to the first field symbol to the field assigned to the second field symbol. The field symbols themselves point to the same fields after the MOVE statement as they did before.
You can create field symbols either without or with type specifications. If you do not specify a type, the field symbol inherits all of the technical attributes of the field assigned to it. If you do specify a type, the system checks the compatibility of the field symbol and the field you are assigning to it during the ASSIGN statement.
Field symbols provide greater flexibility when you address data objects:
If you want to process sections of fields, you can specify the offset and length of the field dynamically.
You can assign one field symbol to another, which allows you to address parts of fields.
Assignments to field symbols may extend beyond field boundaries. This allows you to address regular sequences of fields in memory efficiently.
You can also force a field symbol to take different technical attributes from those of the field assigned to it.
The flexibility of field symbols provides elegant solutions to certain problems. On the other hand, it does mean that errors can easily occur. Since fields are not assigned to field symbols until runtime, the effectiveness of syntax and security checks is very limited for operations involving field symbols. This can lead to runtime errors or incorrect data assignments.
While runtime errors indicate an obvious problem, incorrect data assignments are dangerous because they can be very difficult to detect. For this reason, you should only use field symbols if you cannot achieve the same result using other ABAP statements.
For example, you may want to process part of a string where the offset and length depend on the contents of the field. You could use field symbols in this case. However, since the MOVE statement also supports variable offset and length specifications, you should use it instead. The MOVE statement (with your own auxiliary variables if required) is much safer than using field symbols, since it cannot address memory beyond the boundary of a field. However, field symbols may improve performance in some cases.
ex :
TYPES: BEGIN OF LINE,
COL1,
COL2,
END OF LINE.
DATA: WA TYPE LINE,
ITAB TYPE HASHED TABLE OF LINE WITH UNIQUE KEY COL1,
KEY(4) VALUE 'COL1'.
FIELD-SYMBOLS <FS> TYPE ANY TABLE.
ASSIGN ITAB TO <FS>.
READ TABLE <FS> WITH TABLE KEY (KEY) = 'X' INTO WA.
The internal table ITAB is assigned to the generic field symbol <FS>, after which it is possible to address the table key of the field symbol dynamically. However, the static address
READ TABLE <FS> WITH TABLE KEY COL1 = 'X' INTO WA.
is syntactically not possible, since the formal parameter P does not adopt the key of table ITAB until runtime. In the program, the type specification ANY TABLE only indicates that <FS> is a table. If the type had been ANY (or no type had been specified at all), even the specific internal table statement READ TABLE <FS> would not have been possible.
ex : 2
DATA: BEGIN OF LINE,
COL1,
COL2 VALUE 'X',
END OF LINE.
FIELD-SYMBOLS <FS> LIKE LINE.
ASSIGN LINE TO <FS>.
MOVE <FS>-COL2 TO <FS>-COL1.
The field symbol <FS> is fully typed as a structure, and you can address its components in the program.
ex : 3
DATA: WA(10) VALUE '0123456789'.
DATA: BEGIN OF LINE1,
COL1(3),
COL2(2),
COL3(5),
END OF LINE1.
DATA: BEGIN OF LINE2,
COL1(2),
COL2 LIKE SY-DATUM,
END OF LINE2.
FIELD-SYMBOLS: <F1> STRUCTURE LINE1 DEFAULT WA,
<F2> STRUCTURE LINE2 DEFAULT WA.
WRITE: / <F1>-COL1, <F1>-COL2, <F1>-COL3,
/ <F2>-COL1, <F2>-COL2.
The output is:
012 34 56789
01 2345/67/89
Reply back..
With Rgds,
S.Barani
2008 Mar 13 4:28 AM
HI,
FIELD-SYMBOLS
Declares field symbols.
Syntax
FIELD-SYMBOLS <FS> [<type>|STRUCTURE <s> DEFAULT <wa>].
Field symbols are placeholders or symbolic names for other fields. Pointed brackets are part of the syntax for field symbol names. The <type> addition allows you to specify the type of a field symbol. The STRUCTURE addition forces a structured view of the data objects that you assign to the field symbol.
Regards,
Priya.
2008 Mar 13 4:30 AM
hi,
Field symbols are placeholders or symbolic names for other fields. They do not physically reserve space for a field, but point to its contents. A field symbol cam point to any data object. The data object to which a field symbol points is assigned to it after it has been declared in the program.
Whenever you address a field symbol in a program, you are addressing the field that is assigned to the field symbol. After successful assignment, there is no difference in ABAP whether you reference the field symbol or the field itself. You must assign a field to a field symbol before you can address it in a program.
Field symbols are similar to dereferenced pointers in C (that is, pointers to which the content operator * is applied). However, the only real equivalent of pointers in ABAP, that is, variables that contain a memory address (reference) and that can be used without the contents operator, are reference variables in ABAP Objects.
All operations programmed with field symbols are applied to the field assigned to it. For example, a MOVE statement between two field symbols moves the contents of the field assigned to the first field symbol to the field assigned to the second field symbol. The field symbols themselves point to the same fields after the MOVE statement as they did before.
You can create field symbols either without or with type specifications. If you do not specify a type, the field symbol inherits all of the technical attributes of the field assigned to it. If you do specify a type, the system checks the compatibility of the field symbol and the field you are assigning to it during the ASSIGN statement.
Field symbols provide greater flexibility when you address data objects:
If you want to process sections of fields, you can specify the offset and length of the field dynamically.
You can assign one field symbol to another, which allows you to address parts of fields.
Assignments to field symbols may extend beyond field boundaries. This allows you to address regular sequences of fields in memory efficiently.
You can also force a field symbol to take different technical attributes from those of the field assigned to it.
The flexibility of field symbols provides elegant solutions to certain problems. On the other hand, it does mean that errors can easily occur. Since fields are not assigned to field symbols until runtime, the effectiveness of syntax and security checks is very limited for operations involving field symbols. This can lead to runtime errors or incorrect data assignments.
While runtime errors indicate an obvious problem, incorrect data assignments are dangerous because they can be very difficult to detect. For this reason, you should only use field symbols if you cannot achieve the same result using other ABAP statements.
For example, you may want to process part of a string where the offset and length depend on the contents of the field. You could use field symbols in this case. However, since the MOVE statement also supports variable offset and length specifications, you should use it instead. The MOVE statement (with your own auxiliary variables if required) is much safer than using field symbols, since it cannot address memory beyond the boundary of a field. However, field symbols may improve performance in some cases.
ex :
TYPES: BEGIN OF LINE,
COL1,
COL2,
END OF LINE.
DATA: WA TYPE LINE,
ITAB TYPE HASHED TABLE OF LINE WITH UNIQUE KEY COL1,
KEY(4) VALUE 'COL1'.
FIELD-SYMBOLS <FS> TYPE ANY TABLE.
ASSIGN ITAB TO <FS>.
READ TABLE <FS> WITH TABLE KEY (KEY) = 'X' INTO WA.
The internal table ITAB is assigned to the generic field symbol <FS>, after which it is possible to address the table key of the field symbol dynamically. However, the static address
READ TABLE <FS> WITH TABLE KEY COL1 = 'X' INTO WA.
is syntactically not possible, since the formal parameter P does not adopt the key of table ITAB until runtime. In the program, the type specification ANY TABLE only indicates that <FS> is a table. If the type had been ANY (or no type had been specified at all), even the specific internal table statement READ TABLE <FS> would not have been possible.
ex : 2
DATA: BEGIN OF LINE,
COL1,
COL2 VALUE 'X',
END OF LINE.
FIELD-SYMBOLS <FS> LIKE LINE.
ASSIGN LINE TO <FS>.
MOVE <FS>-COL2 TO <FS>-COL1.
The field symbol <FS> is fully typed as a structure, and you can address its components in the program.
ex : 3
DATA: WA(10) VALUE '0123456789'.
DATA: BEGIN OF LINE1,
COL1(3),
COL2(2),
COL3(5),
END OF LINE1.
DATA: BEGIN OF LINE2,
COL1(2),
COL2 LIKE SY-DATUM,
END OF LINE2.
FIELD-SYMBOLS: <F1> STRUCTURE LINE1 DEFAULT WA,
<F2> STRUCTURE LINE2 DEFAULT WA.
WRITE: / <F1>-COL1, <F1>-COL2, <F1>-COL3,
/ <F2>-COL1, <F2>-COL2.
The output is:
012 34 56789
01 2345/67/89
Reply back..
With Rgds,
S.Barani
2008 Mar 13 5:01 AM
Thanks for the answer.
Can you provide me more example in detail
2008 Mar 13 4:32 AM
Hi
Syntax
FIELD-SYMBOLS <fs> { typing | STRUCTURE struc DEFAULT dobj }.
Extras:
1. ... typing
2. ... STRUCTURE struc DEFAULT dobj
Effect
The FIELD-SYMBOLS statement declares a field symbol <fs>. The name conventions apply to the name fs. The angle brackets of the field symbols indicate the difference to data objects and are obligatory. You can declare field symbols in any procedure and in the global declaration section of an ABAP program, but not in the declaration section of a class or an interface. You can use a field symbol in any operand position in which it is visible and which match the typing defined using typing.
After its declaration, a field symbol is initial - that is, it does not reference a memory area. You have to assign a memory area to it (normally using the ASSIGN statement) before you can use it as an operand. Otherwise an exception will be triggered.
Addition 1
... typing
Effect
You can use the addition typing to type the field symbol. The syntax of typing is described under Syntax of Typing. The typing specifies which memory areas can be assigned to the field symbol (see Checking the Typing) and in which operand positions it can be used.
Note
You can omit the addition typing outside of methods. In this case, the field symbol has the complete generic type any and is implicitly assigned the predefined constant space during the declaration.
Addition 2
... STRUCTURE struc DEFAULT dobj
Effect
If you specify the addition STRUCTURE instead of typing for a field symbol, and struc is a local program structure (a data object, not a data type) or a flat structure from the ABAP Dictionary, this structure is cast for the field symbol <fs>. You have to specify a data object dobj that is initially assigned to the field symbol.
The field symbol copies the technical attributes of structure struc as if it were completely typed. When you assign a data object using the addition DEFAULT, or later using ASSIGN, its complete data type is not checked in non- Unicode programs. Instead, the system merely checks whether it has at least the length of the structure and its alignment.
In Unicode programs, we differentiate between structured and elementary data objects. For a structured data object dobj, its Unicode fragment view has to match the one of struc. In the case of an elementary data object, the object must be character-type and flat, and struc must be purely character-type. The same applies to assignments of data objects to field symbols typed using STRUCTURE when using the ASSIGN statement.
Note
Field symbols declared using the addition STRUCTURE are a mixture of typed field symbols and a utility for casting structured data types. You should use the additions TYPE or LIKE for the FIELD-SYMBOLS statement to type field symbols, while the addition CASTING of the ASSIGN statement is used for casting.
Example
The first example shows the obsolete usage of the addition STRUCTURE.
DATA wa1(512) TYPE c.
FIELD-SYMBOLS STRUCTURE scarr DEFAULT wa1.
-carrid = '...'.
The second example shows the replacement of STRUCTURE with the additions TYPE and CASTING.
DATA wa2(512) TYPE c.
FIELD-SYMBOLS TYPE scarr.
ASSIGN wa2 TO CASTING.
-carrid = '...'.
field symbols are just like pointers concept which are used in C language. We use them when we want to refer to
the fields considered,it doesnt allocate any memory for it.
2008 Mar 13 4:35 AM
hi,
You can declare field symbols in any procedure and in the global declaration section of an ABAP program, but not in the declaration section of a class or an interface. You can use a field symbol in any operand position in which it is visible and which match the typing defined using typing.
field-SYMBOLS <f> type any.
After its declaration, a field symbol is initial - that is, it does not reference a memory area. You have to assign a memory area to it using the ASSIGN statement before you can use it as an operand. Otherwise an exception will be triggered.
Reward if it helps.
Thanks,
VV
2008 Mar 13 5:23 AM
DEMO_FIELD_SYMBOLS_DYNAMI_AS_1
DEMO_FIELD_SYMBOLS_DYNAMI_AS_2
DEMO_FIELD_SYMBOLS_DYNAMI_AS_3
Check this link for more.
http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb387a358411d1829f0000e829fbfe/content.htm
Reward if helpful.
Also use abapdocu transanction which will help you to know abt field symbols in detail.
Thanks,
Imran.