ABAP Field Symbols

In ABAP reports, very often you will be having a situation where you need to store single record of a table or take input from user and append that record to table, both at run-time. In this situation you will need a structure which has all the fields of that particular table with the same namespace. Every time the content of the table changes, you will have to update your structure. So, instead of using structure you can use something called workarea and field symbols that are the replica of single row of that table with same fields.

Introduction

In programming language C/C++, there is a concept of pointers. Let us understand pointers first, before understanding Field Symbols.

Pointers in C/C++:

A pointer is also a variable that hold an address which is the location of another variable in memory. Since a pointer is also a variable, therefore its value is also stored in the memory in another location. In given image, we have assigned the address of a variable “A” to a pointer variable “P”. The link between the variables “P” and the variable “A” can be visualized as shown in the figure.

ABAP Field Symbols

ABAP Field Symbols – Pointer Illustration

The address of A is 07xff0a767cc4. We can see that the value of variable “P” is the address of the variable “A”. Thus, we may access the value of “A” by using the value of “P” [how to access is explained later]. Therefore, we say that the variable “P” points to the variable “A”, hence “P” got the name ‘pointer’.

Referencing and Dereferencing Pointer:

Referencing Pointer: As the name suggest, Referencing is used for reference. A referencing pointer is the one that uses & operator (ampersand character) to set a pointer variable i.e. Address assignment of a variable to a pointer.

int a;
int* p1;
a = 11;
p1 = &a; //p1 references c1

Dereferencing Pointer: As the name suggests, Dereferencing is used to get values out of a reference. A dereferencing pointer is the one that uses * operator (asterisk character) to get the value stored at a pointer.

int b;
b = *p1; //value of b will be then 11. Since we have assigned address of a to p1 in above example

Why to use a pointer:

A pointer is used to reduce the length and complexity of a program and hence increase the execution speed.

What is a Field Symbol?

Just like a pointer discussed above, SAP also introduced something called Field-Symbols. It refers either a table, a field or even anything. When I say anything, I mean that the structure of field symbol will be determined dynamically. In simple words, a field symbol is just a pointer that is used to point a specific line of internal table.

Definition

A field symbol in ABAP is a dereferencing pointer that is mainly used to point specific lines of Internal table. It can be used to point either a table, a field or a dynamic structure. It is used as an alternative to work area to reduce the memory consumption and increase the performance of a program.

Syntax

FIELD-SYMBOLS <fs> { typing | obsolete_typing }.

The ABAP keyword FIELD-SYMBOLS is used to declare a field symbol <fs>. Fs is the part of naming convention and the mandatory angle brackets distinguish them from the other data objects.

Syntax for declaring a field symbol using different data types:

FIELD-SYMBOLS: <fs_example1> TYPE field name.
FIELD-SYMBOLS: < fs_example2> TYPE table.
FIELD-SYMBOLS: < fs_example3> TYPE REF TO DATA. "here DATA is a reference type

The keywords which are used to assign a value to the field symbol are ASSIGNING and ASSIGN.

ASSIGN <itab> [ KEY primary_key (‘…’) = ‘…’] TO <fs_example>.

It is done because, a pointer must have an address to refer, here we assign an address to the field symbol.

It is very important to check if a field symbol is assigned or not else, we will get a run-time error.

IF <FS_EXAMPLE> IS ASSIGNED.
**Your code
ENDIF.

Dynamic Field Symbol or Field-Symbols Inline-Declaration

With the introduction of inline declaration of field symbols, we don’t have to check whether it is assigned or not. Also, we don’t have to declare a field symbol directly.

Syntax:

…. FIELD-SYMBOL(<fs>) ….

Example

LOOP AT <itab> ASSIGNING FIELD-SYMBOL(<fs_line>). 
*** Your Code 
ENDLOOP.

 

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translate »