Anda di halaman 1dari 3

SAP ABAP: Best Practices Field Symbols

Prepared By: Dhiraj Dangore (580125)

This document presents SAP Best Practices (Tips and Tracks) for SAP ABAP development.
Using SAP ABAP Field Symbols techniques, it helps to reduce the time and improve the
performance of the program.

Field Symbols:
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 each
field symbol before you can address the latter in programs.
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.

TCS Confidential

Created By: Dhiraj Dangore (580125).

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.

Declaring Field Symbols:


To declare a field symbol, use the statement
FIELD-SYMBOLS <FS> [<type>|STRUCTURE <s> DEFAULT <wa>].
For field symbols, the angle brackets are part of the syntax. They identify field symbols in
the program code.
If you do not specify any additions, the field symbol <FS> can have data objects of any
type assigned to it. When you assign a data object, the field symbol inherits its technical
attributes. The data type of the assigned data object becomes the actual data type of the
field symbol.
Note: it is possible to assign reference variables and structured data objects to untyped field
symbols. However, the static field symbol is only a pointer to the field in memory, and
does not have the complex type attributes of a reference or structured field until runtime.
You can only use the field symbol to address the whole field (for example, in a MOVE
statement). Specific statements such as CREATE OBJECT <FS> or LOOP AT <FS> are
not possible.

TCS Confidential

Created By: Dhiraj Dangore (580125).

Sample Code:

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.
Example using the correct syntax (TYPE and CASTING):
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> LIKE line1.
ASSIGN wa TO <f1> CASTING.
FIELD-SYMBOLS: <f2> LIKE line2.
ASSIGN wa TO <f2> CASTING.
WRITE: / <f1>-col1, <F1>-col2, <F1>-col3,
/ <f2>-col1, <F2>-col2.
In both cases, the list appears as follows:
012 34 56789
01 2345/67/89
This example declares two field symbols to which different structures are attached. The
string WA is then assigned to each of them. The output shows that the field symbols assign
the strings component by component according to the type of the components.
TCS Confidential

Created By: Dhiraj Dangore (580125).

Anda mungkin juga menyukai