Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member


Hi friends,

The following is the blog that will explains the relationship between data refereneces and field symbols with hands on examples.

Readers will definitely get different view and indepth understanding on feild symbols and data references.

                                                           
Field Symbols


We all know the standard definition given by SAP about Field symbols as, “Field symbols are placeholders or symbolic names for other fields.
They do not physically reserve space for a field, but points to its contents. A field symbol can 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”

Let us put the above Definition in more detailed way with
easiest examples as follows

Variable means!

   It will have its own address


Work area means!

  It will have its own address

Internal table means!

  It will have its own address


Field symbol means!

It will not have its own address

But it will point the address of the data object or it
will point to the content of the data object to which it has been assigned
.


So performance wise field symbols are powerful when we doing modify and append because the data update is not happening from the external memory,
instead of that it is updating from the same memory. This is how the field symbols are faster when compared to work area.

Note: Field symbols cannot be used without assigning a data object to them. That means either a variable or Structure or internal table
has to be assigned to field symbol before using it
.  If we remember this thumb rule it will be very easy to work with field symbols

1. Field symbol for a data variable

When we execute this program, the output of no1 is

2. Field symbol for a structure:-

When we execute the above program, it shows the MATNR, MTART as follows

3. Field symbol for an internal table

Suppose I have an internal table as follows

IT_MARA

Matnr      Matkl

1000        001    ----------(R1 Address of the record1 in internal table memory)

2000        002    ---------(R2 Address of the record2 in internal table memory)

Suppose we do have a field symbol declared as follows.

Field-symbols: <FS_MARA> TYPE MARA.

When we loop the above internal table by assigning to field
symbol as follows, What happens?

Loop at IT_MARA Assigning <FS_MARA>.

WRITE:  / <FS_MARA>-MATNR, <FS_MARA>-MTART.


ENDLOOP.


The memory location of each record is assigned to field symbol, that means when we work with the field symbol like changing any fields
or populating with some values nothing but we are working with the memory location of that particular record in the internal table. It is working like
“Pass by reference” in our subroutines. When we changed something in the field symbol, that means we are directly changing the internal table record.


Whereas when we have work area explicitly, the internal table record is different, and the work area we are using is different, so the
change we did in the work area has to be get modified explicitly in the internal table with the help of keyword ‘MODIFY’ unlike how field symbols
modify directly.


Example to use field symbol with internal table:-

When we execute the above program, the output is

Possible Syntaxes of field symbols:-


Read table <itab> assigning <fs>…

Loop at <itab> assigning <fs>…

Assign <data object> to <fs>

Assign component <component name/component index> to <fs>

Append initial line to <itab> assigning <fs>

                                                                DATA REFERENCES

Before understanding Data References, I have a question to you!


Q) What is an object / Class reference? What happens when we create a reference variable to a class?

Ans:- An object is an instance of the class. When we
create instance to the class, memory will be assigned to the components of the
class along with certain values.


Q)  What is data reference then?

Ans: - The instance of the data is called ‘Data
reference’. In simple words, memory or data will be created to the data objects
to which we want to create in the runtime.

When we go for data references?

Whenever, we don’t know clearly what data object we want to create specifically then we go for creation of data references. The data object
may be data variable, structure and internal table.

Types of Data References:-

  1. Known Data References
  2. Un Known Data References

Known Data References: When we know clearly what data type we
need to create in run time, then we go for Known data references.

Un Known Data References: When we don’t know what data type
that we need to create in run time, then we go for Un Known Data References.

Note:- It we know data type, then it doesn’t make sense to
create data reference. But before understanding Unknown data references, it is
must to understand Know creation of Data reference for known data types.

Syntax of Data reference:-

  1. I want to create data of integer, how?

Both the above syntaxes are same. We can go
for any syntax. I will prefer the first syntax in all the coming examples.

Before going to the examples of Known data
references and Unknown data references, let us discuss one small example which
is common to both Known and Unknown data References as follows.

DATA lr_data TYPE REF TO data.
CREATE DATA lr_data TYPE i.

What is ‘DATA’ in the above statement. ‘Data’ also a
predefined data type in ABAP like i, char10 etc but it is of Not Fully data
type which is a generic data type.

Let’s understand the above two program lines in debugging
mode.

Keep the break point in the program and understand what is
what happening with lr_Data variable.

Double click on lr_Data.

If we see the pointed area, the LR_DATA initially not
referring anything.

Now let us take F5, F5 and see the same LR_DATA.

Now if we see the LR_DATA, it is initialized to the initial
value of integer i.

That means through the statement CREATE DATA lr_Data TYPE I,
we are able to initialize the value of LR_DATA to ‘0’ from ‘INITIAL’.


Here LR_DATA is called as “Reference Variable”, not the
normal variable.

Now double click on ‘LR_DATA’ to understand more about
“Reference variable”.


Here LR_DATA, is a Reference variable

  • REF TO \TYPE=I, It is of type REF TO ‘I’
  • {A:1*\TYPE=I},  is the Address of the
    data we created


Point 1:- We cannot see the value/content of the Reference variable directly with a
double click like normal variable

To see the content/value of the Reference variable, one way is to double click on the Hook symbol.

It will show the value of the reference variable LR_DATA as
follows

Another way is, in debugging mode itself after double
clicking on LR_DATA, put ->* next to the LR_DATA as follows and hit enter,
it will show the value in that variable


By putting ->* next to a reference variable we can see the value of that
variable but we cannot assign any value into that variable


Point 2:- We need to know the meaning of two symbols ->, * .

-> Means Pointing the address

  1. * means Value at that address
  2. ->* Means Pointing the value
    at that address 

So as per our discussion, now let us try to assign value into the reference variable in the program as follows and see what happens and
why?

What is De REFERENCE means?

Referencing means -> Addressing certain data object by
assigning some memory to it.

De referencing means accessing the address (or) assigning
the value into the content of the reference.

-> This is referencing

->*   This is de referencing (or accessing the value at that address)

Here in our example de referencing of variable (or any object) is not possible through by simply adding the symbol ->* next to that
reference variable.

Then, how it is possible to assign the value into the reference variable?


Through Field-symbols only we can access the reference variables.


As field symbols are only things given by SAP, that can point the content of the address of the data
object. (Means field symbols will point to the contend of the data object, explained in the starting of this blog)


Golden Rule to Remember:-


So it is always a thumb rule to remember, to deal with accessing the address or assigning certain value into particular address, it is require to assign that
address to field symbol of that type of data object. If you don’t know the data object of what data reference we are accessing then that field symbols has to
be declared with generic data types which will be discussed later in the same blog.

DATA REFERENCES:-

Known Type

Un Known Type


Let us discuss the examples of Known Type Data references with Hands on examples as follows


Known Data References ( data variable creation )

Now execute the program, we will able to see that we created
an integer type variable and stored a value 10 into that through Known data
reference and a field symbol

2. Known data references(structure creation)

Now execute the program and see the values of the structure
we created through the data references

3. Known data reference(internal table creation)

Now execute the program and see, we could able to create an
internal table through data references and shown as follows. We can write
select query also as well.

Let us discuss the examples for Unknown data references.


Note:- When we working with un known data references, we definitely do not know what data object we are going to create like, we are not
sure whether we are creating an integer or character, we are not sure whether we are creating the data of MARA or VBAK, we are not sure whether we are
creating an internal table of MARA or VBAK. But it is important to know, whether we are going to create a ‘Data variable’ or ‘Structure’ or ‘Internal table’.

Here we need to know few Generic data types given by SAP. The generic data types are also the data types like ‘DDIC’ and ‘ABAP Program
data types’, but which are used when we don’t know the type of the data till run time.


Examples of generic data types:-


Type any (Used for both unknown variable creation, unknown structure creation)

Type any table(Used for creation of unknown table creation)

Type ref to data

Type object

Etc Etc

Note:- Generic data types can only be used with field symbols.

1. Un known data reference(creation of data
variable)

2. Un known data references(creation of structure)


Now let us execute, and give your desired structure
name, our data reference creates a record from that structure and shows as
follows


   Enter VBAP and execute, it gives as follows

3. Unknown data references(creation of table):-

*Parameter for user input

PARAMETERS: p_var type string.



data: lr_Data type ref to data.

create data lr_Data type table of (p_var).



"As we dont know what type of structure we have to create, let us declare field symbol dynamically as follows

FIELD-SYMBOLS: <ft_table> type ANY TABLE. "key word any table is used for declaration of unknown field symbol



ASSIGN lr_Data->* to <ft_table>.

if sy-subrc is initial.

  select * from (p_var) into table <ft_table> UP TO 5 ROWS.

    if sy-subrc = 0.

      data: r_Data type REF TO cl_salv_table.

*      TRY.

      CALL METHOD cl_salv_table=>factory

*        EXPORTING

*          list_display   = IF_SALV_C_BOOL_SAP=>FALSE

*          r_container    =

*          container_name =

        IMPORTING

          r_salv_table   = r_Data

        CHANGING

          t_table        = <ft_table>

          .

*       CATCH cx_salv_msg .

*      ENDTRY.



    endif.



    r_Data->display( ).

endif.

Now exectute the program and give Any table name that you wish, it will show the records from that table in ALV Report.

This is the brief explanation of Data references with field symbols

6 Comments