0% found this document useful (0 votes)
328 views

Dynamic Programming in ABAP - Part 1 - Introduction To Field Symbols - SAP Blogs

Dynamic Programming in ABAP – Part 1

Uploaded by

HaydarV
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
328 views

Dynamic Programming in ABAP - Part 1 - Introduction To Field Symbols - SAP Blogs

Dynamic Programming in ABAP – Part 1

Uploaded by

HaydarV
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

9/27/2018 Dynamic Programming in ABAP – Part 1 – Introduction to Field Symbols | SAP Blogs

Products
Products Industries
Industries Support
Support Training
Training Community
Community Developer
Developer Partner
Partner

About
About

 
Ask a Question Write a Blog Post Login

Dynamic Programming in ABAP – Part 1 –


Introduction to Field Symbols
September 5, 2017 | 7,637 Views |

Former Member

ABAP Development
SAP NetWeaver Application Server for ABAP | abap | dynamic | field | field symbol | introduction | programming | symbol

share share tweet share

Follow

https://blogs.sap.com/2017/09/05/dynamic-programming-in-abap-part-1-introduction-to-field-symbols/ 1/8
9/27/2018 Dynamic Programming in ABAP – Part 1 – Introduction to Field Symbols | SAP Blogs

Field symbol is a placeholder for data object, which points to the value present
at the memory address of a data object. It does not reserve any physical
memory space when we declare them. It only points to a data object at run
time. Field symbols are of two types:

Typed Field Symbol


Generic Field Symbol

Typed Field Symbol – Typed field symbol can be declared as:

DATA: var TYPE i VALUE 2.


FIELD-SYMBOLS: <fs_num> TYPE i.
ASSIGN var TO <fs_num>.
WRITE: / <fs_num>.
<fs_num> = 4.
WRITE: / var.

The output will be 2 and 4.

NOTE:

Typed field symbols can point to the data objects of specified type only.
After assigning a data object to a field symbol, if we make any changes
to the field symbol value, then the value of corresponding data object is
also updated.

Field symbol as a replacement of Work area:

Modifying internal table records – We can declare a field symbol of type


any structure, which we can use while looping through an internal table.

DATA: lt_mara TYPE STANDARD TABLE OF mara.


FIELD-SYMBOLS: <fs_mara> TYPE mara.
SELECT * FROM mara INTO TABLE lt_mara UP TO 10 ROWS.
LOOP AT lt_mara ASSIGNING <fs_mara>.
<fs_mara>-matkl = 'DEMO'.
ENDLOOP.

NOTE:

If we change any field of structure in field symbol, the corresponding


field in internal will get updated. We don’t need to write the MODIFY
statement which we would have written if we had used work area. This

https://blogs.sap.com/2017/09/05/dynamic-programming-in-abap-part-1-introduction-to-field-symbols/ 2/8
9/27/2018 Dynamic Programming in ABAP – Part 1 – Introduction to Field Symbols | SAP Blogs

is because work area stores a copy of the internal table row, whereas
field symbol directly references the internal table row.
Hence processing of internal table with field symbol is faster than the
processing of internal table with work area.

Appending to internal table – Now suppose we want to append some values


to one internal table, then we can use field symbol as below:

DATA: lt_mara TYPE STANDARD TABLE OF mara.


FIELD-SYMBOLS: <fs_mara> TYPE mara.

APPEND INITIAL LINE TO lt_mara ASSIGNING <fs_mara>.


IF <fs_mara> IS ASSIGNED.
<fs_mara>-matnr = 'MAT1'.
<fs_mara>-matkl = '001'.
UNASSIGN <fs_mara>.
ENDIF.

APPEND INITIAL LINE TO lt_mara ASSIGNING <fs_mara>.


IF <fs_mara> IS ASSIGNED.
<fs_mara>-matnr = 'MAT2'.
<fs_mara>-matkl = '001'.
UNASSIGN <fs_mara>.
ENDIF.

After executing this, the internal table will hold two rows.

NOTE:

Always perform a check on field symbol that if it is assigned before


doing any operation to avoid short dump. Also after doing the operation,
unassign the field symbol.

Reading internal table – We can read a record of internal table using field
symbol as below:

READ TABLE lt_mara ASSIGNING <fs_mara> WITH KEY matnr = 'MAT1'.

Generic Field Symbol:

Dynamic programming is actually implemented using generic field symbols.


The most commonly used generic types are TYPE ANY and TYPE ANY

https://blogs.sap.com/2017/09/05/dynamic-programming-in-abap-part-1-introduction-to-field-symbols/ 3/8
9/27/2018 Dynamic Programming in ABAP – Part 1 – Introduction to Field Symbols | SAP Blogs

TABLE.

FIELD-SYMBOLS: <fs_str> TYPE ANY.


FIELD-SYMBOLS: <fs_tab> TYPE ANY TABLE.

Here we can assign any data object to TYPE ANY field symbol whereas TYPE
ANY TABLE field symbol is used for assigning any internal table.

TYPE ANY:

Let us assign a work area of type MARA to a TYPE ANY field symbol and
then populate the work area using field symbol.

FIELD-SYMBOLS: <fs_str> TYPE ANY.


FIELD-SYMBOLS: <fs_data> TYPE ANY.
DATA: lw_mara TYPE mara.

ASSIGN lw_mara TO <fs_str>.


IF <fs_str> IS ASSIGNED.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_str> TO <fs_data>.
IF <fs_data> IS ASSIGNED.
<fs_data> = 'MAT001'.
UNASSIGN <fs_data>.
ENDIF.
UNASSIGN <fs_str>.
ENDIF.

NOTE:

After assigning lw_mara to <fs_str>, we cannot directly use the ‘-‘


operator on field symbol to access the fields of MARA structure i.e.
<fs_str>-matnr would produce syntax error. This is because the field
symbol type is declared only at runtime not at compile time.
So to access the matnr field with field symbol, first we need to assign
that field component to a different field symbol and then use the new
field symbol to update the matnr field as show in above code snippet.
After execution of above code snippet, the value of lw_mara-matnr
would be MAT001.

TYPE ANY TABLE:

https://blogs.sap.com/2017/09/05/dynamic-programming-in-abap-part-1-introduction-to-field-symbols/ 4/8
9/27/2018 Dynamic Programming in ABAP – Part 1 – Introduction to Field Symbols | SAP Blogs

We can assign any internal table to this field symbol. Let us analyze the below
code snippet to understand how we could use such field symbol.

FIELD-SYMBOLS: <fs_tab> TYPE ANY TABLE.


FIELD-SYMBOLS: <fs_str> TYPE any.
FIELD-SYMBOLS: <fs_data> TYPE any.
DATA: lt_mara TYPE STANDARD TABLE OF mara.
DATA: lw_mara TYPE mara.

ASSIGN lt_mara TO <fs_tab>.


SELECT * FROM mara INTO TABLE lt_mara UP TO 10 ROWS.

LOOP AT <fs_tab> ASSIGNING <fs_str>.


IF <fs_str> IS ASSIGNED.
ASSIGN COMPONENT 'MATKL' OF STRUCTURE <fs_str> TO <fs_data>.
IF <fs_data> IS ASSIGNED.
IF <fs_data> EQ '01'.
*********** Do some processing *********
ENDIF.
UNASSIGN <fs_data>.
ENDIF.
ENDIF.
ENDLOOP.

Reading internal table using generic eld symbol:

FIELD-SYMBOLS: <fs_tab> TYPE ANY TABLE.


FIELD-SYMBOLS: <fs_str> TYPE any.
DATA: lt_mara TYPE STANDARD TABLE OF mara.

ASSIGN lt_mara TO <fs_tab>.


SELECT * FROM mara INTO TABLE lt_mara UP TO 10 ROWS.

READ TABLE <fs_tab> ASSIGNING <fs_str> WITH KEY ('MATNR') = 'MAT001'.

NOTE:

Since <fs_tab> is a generic field symbol, its type will be known only at
runtime, hence we cannot directly write the fields of MARA structure
after WITH KEY, instead we have to write the field name within
parenthesis as shown above.
In ABAP, this parenthesis indicates the compiler that the value of the
operand will be decided at runtime, hence we don’t get any compilation
error.

https://blogs.sap.com/2017/09/05/dynamic-programming-in-abap-part-1-introduction-to-field-symbols/ 5/8
9/27/2018 Dynamic Programming in ABAP – Part 1 – Introduction to Field Symbols | SAP Blogs

In my next blog i have explained about data references and its significance in
dynamic programming. Below is the link for same.

https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-
introduction-to-data-reference/

Alert Moderator

6 Comments
You must be Logged on to comment or reply to a post.

Matthew Billingham

September 5, 2017 at 7:43 am

I’m not a fan of prefixes related to typing generally, but having FS before each field
symbol seems particularly useless. The names are embedded in angle brackets – we
know it’s an FS without the need for any prefix. Concentrate on meaningful names rather
than prefixes that add zero value.

Jacques Nomssi

September 5, 2017 at 8:09 pm

Hello Rahul,
https://blogs.sap.com/2017/09/05/dynamic-programming-in-abap-part-1-introduction-to-field-symbols/ 6/8
9/27/2018 Dynamic Programming in ABAP – Part 1 – Introduction to Field Symbols | SAP Blogs

1. there is a static and a dynamic version of the ASSIGN statement. If you recognize a
dynamic case, then check of SY-SUBRC after ASSIGN ( 0 if ok, 4 if error) is enough to
make sure the field symbol valid, so a IS ASSIGNED or subsequent UNASSIGN is not
mandatory.
2. On newer releases, typed field symbols can be declared implicitely, e.g.

ASSIGN lw_mara TO FIELD-SYMBOL(<lw_mara>).

I think the VALUE operator will make the use of ASSIGNING as in the idiom
APPEND INITIAL LINE TO .. ASSIGNING less popular.

regards,

JNN

Former Member Post author

September 6, 2017 at 2:18 am

Hi Jacques,

Thanks for putting new points.

Former Member

September 7, 2017 at 6:29 pm

Also Rahul, you can refer to this quick reference to understand


the latest advancements in ABAP –
https://blogs.sap.com/2015/10/25/abap-740-quick-reference/

The full documentation is available


at https://help.sap.com/doc/abapdocu_740_index_htm/7.40/en
-US/index.htm

With these advancements programming is fun.

Former Member Post author

September 8, 2017 at 7:57 am

https://blogs.sap.com/2017/09/05/dynamic-programming-in-abap-part-1-introduction-to-field-symbols/ 7/8
9/27/2018 Dynamic Programming in ABAP – Part 1 – Introduction to Field Symbols | SAP Blogs

Thanks Tamit

Gopi Srinivasan

February 2, 2018 at 1:16 pm

Good to begin with field symbols..! Rahul. Cheers.

Share & Follow

Privacy Terms of Use

Legal Disclosure Copyright

Trademark Sitemap

Newsletter

https://blogs.sap.com/2017/09/05/dynamic-programming-in-abap-part-1-introduction-to-field-symbols/ 8/8

You might also like