Ads

What are Field Symbols in SAP ABAP

What are Field Symbols in SAP ABAP


Field Symbols 
Field symbols and data references are used to access data objects dynamically in ABAP programs.
without knowing the name attributes till run time ,using field symbols ,we can access and pass data objects.

When we address a field symbol, the system works with the contents of the data object assigned to it, and not with the contents of the field symbol itself.

Field symbols are placeholders or symbolic names for other fields. They do not physically reserve space for a field, but point to its contents.

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.


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 during the field assignment whether the assigned field matches the type of field symbol.

You can assign one field symbol to another, which allows you to address subfields.
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 properties than those of the field assigned to it (casting).

While run time 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.


Field symbols Declaration in ABAP Program:

FIELD-SYMBOLS <fs> [typing].

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.

Typing Field Symbols

The typing addition allows you to specify the type of a field symbol. When you assign a data object to a field symbol, the system checks whether the type of the data object you are trying to assign is compatible with that of the field symbol. If the types are not compatible or convertible, the system reacts with a syntax or run time error. If you wish to perform casting of data objects using field symbols , you must do so explicitly using the ASSIGN statement. The system then treats the assigned data object as if it had the same type as the field symbol.

Generic Type Specification

TYPES: BEGIN OF line,      
col1 TYPE c,      
 col2 TYPE c,    
END OF line.

DATA: wa TYPE line,    
itab TYPE HASHED TABLE OF line WITH UNIQUE KEY col1,  
 key(4) TYPE c 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 not possible syntactically, since the field symbol 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 from a syntax point of view.

Specifying the Type Fully

If you have full types specified, the technical attributes of the field symbols are determined. The technical attributes of the assigned data objects must match those of the field symbol.


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.

The field symbol <fs> is fully typed as a structure, and you can address its components in the program.

During a program, you can assign data objects to field symbols at any time. You can also assign a series of different data objects to the same field symbol during a program.

To assign a data object to a field symbol, use the ASSIGN statement. The ASSIGN statement has several variants and parameters.

1.The Basic Form of the ASSIGN Statement

2.Assigning Components of Structures to a Field Symbol

3.Casting Data Objects

4.Data Areas for Field Symbols

You can assign single lines from internal tables to field symbols.

You can start a screen sequence from an ABAP program using UNASSIGN <fs>.


Basic Forms of the ASSIGN Statement

Static ASSIGN
If you already know the name of the field that you want to assign to the field symbol when you write a program, use the static ASSIGN statement:

ASSIGN dobj TO <fs>.

When you assign the data object, the system checks whether the technical attributes of the data object dobj  correspond to the type specifications for the field symbol <fs>. The field symbol adopts any generic attributes of dobj that are not contained in its own type specification. After the assignment, it points to this field in the memory.


FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i.

ASSIGN text TO <f1>.ASSIGN num TO  <f2>.

ASSIGN line1 TO <f1>.ASSIGN line2-col2 TO <f2>.MOVE <f1> TO line2.
ASSIGN 'LINE2-COL2 =' TO <f1>.

Static ASSIGN with Offset Specification
In a static ASSIGN statement, you can use positive offset and length specifications to assign a sub field to a field symbol.

ASSIGN dobj[+off][(len)] TO <fs>.

When you assign parts of fields to a field symbol, the following special conditions apply:

If you do not specify the length len, the system automatically uses the length of the field dobj. If off is greater than zero, <fs> always points to an area beyond the limits of dobj.
If off is smaller than the length of dobj, you can enter an asterisk (*) for len to prevent <fs> from referring to an address beyond the limits of dobj.

FIELD-SYMBOLS <fs> TYPE ANY.


ASSIGN line-string1+5 TO <fs>.

ASSIGN line-string1+5(*) TO <fs>.


FIELD-SYMBOLS <fs> TYPE ANY.

  ASSIGN line-a+off(len) TO <fs>.

 ASSIGN line-a+off(1) TO <fs>.
Dynamic ASSIGN
If you do not know the name of the field that you want to assign to the field symbol when you write a program, you can use a dynamic ASSIGNstatement:

ASSIGN (dobj) TO <fs>.

This statement assigns the data object whose name is contained in the dobj field to the field symbol <fs>. You cannot use subfields in a dynamic ASSIGN.

At run time, the system searches for the corresponding data object in the following order:

If the ASSIGN statement is in a procedure , the system searches first in its local data.
If it cannot find the object in the local data (or if the ASSIGN statement is not in a procedure), it then looks in the local data of the program.

ASSIGN TABLE FIELD (dobj) TO <fs>.

FIELD-SYMBOLS <fs> TYPE ANY.
ASSIGN (name) TO <fs>.  IF sy-subrc EQ 0.    WRITE / <fs>.  ENDIF.

FIELD-SYMBOLS <fs> TYPE ANY.

ASSIGN TABLE FIELD (name1) TO <fs>

ASSIGN TABLE FIELD (name2) TO <fs>.

ASSIGN <fs1>[+off][(len)] TO <fs2>.

in a static ASSIGNand:

ASSIGN [TABLE FIELD] (dobj) TO <fs2>.

in a dynamic ASSIGN, where the field dobj contains the name of a field symbol <fs1>. <fs1> and <fs2> may be identical.

DATA off TYPE i.
FIELD-SYMBOLS <fs> TYPE ANY.

ASSIGN s-a TO <fs>.

Assigning Components of Structures to a Field Symbol


ASSIGN COMPONENT comp OF STRUCTURE struc TO <fs>.

to assign one of its components comp to the field symbol <fs>. You can specify the component comp either as a literal or a variable. If comp is of type c or a structure which has no internal tables as components, it specifies the name of the component. If comp has any other elementary data type, it is converted to type i and specifies the number of the component. If the assignment is successful, sy-subrc is set to 0. Otherwise, it returns 4.

FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE ANY, <f3> TYPE ANY.

ASSIGN line TO <f1>.ASSIGN comp TO <f2>.


ASSIGN COMPONENT <f2> OF STRUCTURE <f1> TO <f3>.WRITE / <f3>.

Casting Data Objects

When you assign a data object to a field symbol, you can cast to any data type. This means that any area of memory can be viewed as having assumed a given type. You can then address parts of fields symbolically without having to use offset/length addressing.

A cast is performed using the CASTINGaddition of the ASSIGN statement. The CASTINGaddition allows you to assign a data object to a field symbol where the type of the data object is incompatible with that of the field symbol. There are two types of casting: casting with an implicit type declaration and casting with an explicit type declaration.


Casting with an Implicit Type Declaration

Provided the field symbol is either fully typed or has one of the generic built-in ABAP types - c, n, p, or x - you can use the following ASSIGNstatement:

ASSIGN ...TO <fs> CASTING.

FIELD-SYMBOLS <fs> TYPE t_date.

ASSIGN sy-datum TO <fs> CASTING.

Casting with an Explicit Type Declaration
If the field symbol is neither fully typed nor generically typed, use the following form of the ASSIGNstatement:

ASSIGN ...TO <fs> CASTING {TYPE type}|{LIKE dobj} [DECIMALS dec].

When the system accesses the field symbol, the content of the assigned data object is interpreted as if it had the type declared in the statement. After the TYPE addition, you can declare the name of a data object enclosed in parentheses. The content of this data object indicates the data type at run time.

FIELD-SYMBOLS: <fs> TYPE ANY,               <f>  TYPE n.

ASSIGN sy-datum TO <fs> CASTING TYPE t_date.


Data Areas for Field Symbols


You can only assign data objects from the data areas of the ABAP program to a field symbol. When you assign a data object to a field symbol, the system checks at runtime to ensure that no data is lost due to the field symbol addressing memory outside the data area.

The data areas of an ABAP program :

The table memory area for internal tables. The size of this storage area depends on the number of table lines. This is not a fixed value, it is  determined dynamically at runtime.
The DATA storage area for other data objects. The size of this storage area is fixed during the data declaration.
Field symbols cannot point to addresses outside these areas. If you assign data objects to a field symbol and point to addresses outside these areas, a runtime error occurs.

Select Statements Types in ABAP Programming

Comments

Popular posts from this blog

BADI Interview Questions in SAP ABAP

Sample SAP ABAP Programming Examples for Practice

Module Pool Programming Interview Questions and Answers in SAP ABAP

Step by Step tutorial on BDC Session Method Program in SAP ABAP

SAP ABAP Interview Questions and Answers for 10 Years Experienced