SAP NetWeaver AS ABAP Release 751, ©Copyright 2017 SAP AG. All rights reserved.  

Field Symbols

Field symbols are placeholders or symbolic names for existing data objects (or parts of existing data objects), declared by the statement FIELD-SYMBOLS or by the declaration operator FIELD-SYMBOL . A memory area is assigned to a field symbol at program runtime. A field symbol does not reserve physical space in the data area of a program like with a data object . Instead, it works as a dynamic name of a memory area, where a specific data object or part of an object is located. A field symbol can be used instead of data objects at operand positions of statements. When a statement of this type is executed, a memory area must be assigned to the field symbol by the statement ASSIGN or the addition ASSIGNING when processing internal tables .

Field symbols can point to almost any data object. A field symbol is the equivalent of a data reference variable dereferenced using ->* . Unlike data reference variables, value semantics are used to access a field symbol Accessing a field symbol is like accessing the named data object (or part of the object). For data reference variables, however, reference semantics apply.

Field symbols are implemented both for generic work with data objects and for explicit casting of data objects (only possible using field symbols).

Programming Guideline

  • Field symbols to which data objects or parts of data objects are assigned in the heap are memory-preserving, like heap references .
  • From a technical perspective, the field symbols are implemented by references or pointers, which are comparable to references in data reference variables . A data reference variable is declared in the same way as every other data object und the memory area for the reference contained in the variable is located in the data area of the ABAP program. However, the pointer assigned to a field symbol is exclusively managed by the ABAP runtime environment and is located in the memory area, which cannot be accessed directly in an ABAP program.
  • Getting started with ABAP
  • ABAP GRID List Viewer (ALV)
  • ABAP Objects
  • Control Flow Statements
  • Data Declaration
  • Dynamic Programming
  • Data references
  • Field-Symbols
  • RunTime Type Services
  • Internal Tables
  • Message Classes/MESSAGE keyword
  • Naming Conventions
  • Regular Expressions
  • Template Programs
  • Unit testing

ABAP Dynamic Programming Field-Symbols

Fastest entity framework extensions.

Field-Symbols are ABAP's equivalent to pointers, except that Field-Symbols are always dereferenced (it is not possible to change the actual address in memory).

Declaration

To declare a Field-Symbol the keyword FIELD-SYMBOLS must be used. Types can be generic ( ANY [... TABLE] ) to handle a wide variety of variables.

Field-Symbols are unassigned on declaration, which means that they are pointing to nothing. Accessing an unassigned Field-Symbol will lead to an exception, and, if uncaught, to a short dump. Therefore, the state should be checked with IS ASSIGNED :

As they are only references, no real data can be stored inside. So, declared DATA is needed in every case of use.

Unassigning

Sometimes it could be useful to reset a Field-Symbol. This can be done using UNASSIGN .

Use for internal tables

Field-Symbols may be used to modify internal tables.

Attention! Field-Symbols stay assigned even after leaving the loop. If you want to reuse them safely, unassign them immediately.

pdf

Get monthly updates about new articles, cheatsheets, and tricks.

Author's profile photo Former Member

Dynamic Programming in ABAP – Part 1 – Introduction to Field Symbols

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 – Typed field symbol can be declared as:

The output will be 2 and 4.

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.

Appending to internal table – Now suppose we want to append some values to one internal table, then we can use field symbol as below:

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

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

Generic Field Symbol:

Dynamic programming is actually implemented using generic field symbols. The most commonly used generic types are TYPE ANY and 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.

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

TYPE ANY TABLE:

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.

Reading internal table using generic field symbol:

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/

Assigned Tags

Author's profile photo Matthew Billingham

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.

Author's profile photo Bhakti joshi

its for readability.we shud not invent new things just for the heck of it. readability in a program is very important for the portability and management of code.

Author's profile photo Sandra Rossi

What “should not invent new things” means, here?

i mean the idea of ditching fs_ from the name of field symbol is against universally accepted standards and its better not to divert bcz it might reduce readability and clarity in complex coding.

I've been telling developers to not use <FS_...> for 20 years. I think all the projects I was working in did not prefix them with FS. Sorry, it's not a universal rule. I don't know who invented this rule of prefixing field symbols with FS. I think those people who invented the prefix FS for field symbols were crazy 😉

"i mean the idea of ditching fs_ from the name of field symbol is against universally accepted standards"

With the greatest possible respect - this is not the case. But just to be sure I checked - twice - and it turns out - I am right. It really isn't the case..

I've been programming in ABAP since 1997. I've also defined the standards for numerous multinationals. Never was <fs_...> permitted. In code reviews, the code would be sent back and the programmer told not to be so silly. Precisely because clarity and readability are so important.

I've also managed to get the current SAP and DSAG recommendations for variable naming included at a couple of global companies.  These recommendations follow the general consensus across the  entire IT current programming community that clarity and meaningful names are vital to readability, and prefixes reduce clearly and readability and so should be avoided .

Do you also prefix forms with F_

Thanks for sharing ur experience. I’ve always seen field symbols with fs_ in more than 15 years of experience seeing multitude of global  MNC systems across geographies . If there are any std sap blogs / document / examples without this convention then it will be good to refer. Maybe within scn site itself . tat is in case it’s worth spending any more time. thanks 

Avoid prefixes (documentation “Clean ABAP”, by SAP): https://github.com/SAP/styleguides/blob/master/clean-abap/CleanABAP.md#avoid-encodings-esp-hungarian-notation-and-prefixes

Author's profile photo Patrick Van Nierop

Personally I don't even consider the 'fs_' part to be a prefix. It's completely useless in my eyes as the '<' & '>' clearly indicate that you're looking at a field-symbol.

A prefix in a field-symbol would be adding a 's_', 't_',.. etc to the name of the field-symbol. At the very least that would add some meaning :).

Also, with regards to the Clean ABAP styleguide, no prefixes works if you adhere to a functional style of programming (smaller 'functions' that do only one thing). Sadly, I still see a lot off wall-of-text code, with both global & local variables..   And anyway, I prefer to see meaningful names.. so rather  LV_COMPANY_CODE than LV_BUKRS or gods forbid BUKRS ;).

Last but not least, for a lot of us, it is the customer that decides upon the naming conventions, and it is not that easy to convince them to change these..

Or lv_bukrs for a global variable... (But I only use it locally within methods... ? )

I’ve always seen field symbols with fs_

This is exactly how wrong practices get perpetuated. Nobody challenges the status quo, nobody thinks. And then it becomes the standard - and it's still wrong.

Author's profile photo Ryan Crosby

Spot on... I challenge it all the time because that's the road to continuous improvement.  One of my least favorite sayings of all time... "That's the way we've always done it".

Author's profile photo Jacques Nomssi Nzali

Hello Rahul,

Author's profile photo Former Member

Hi Jacques,

Thanks for putting new points.

Author's profile photo Tamit Kumar Das Sharma

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.

Thanks Tamit

Author's profile photo Gopi Srinivasan

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

Author's profile photo Francesco Allocca

I ask for information,

rather than

Isn't this better?

Author's profile photo Paul Hammond

I still don't see what the advantage of using field symbols is.

For example, instead of

what's wrong with

Author's profile photo Vera Kulish

I like your example and agree to use that simple code instead of that confusing code with assigning and unassing field symbol.

Insert/edit link

Enter the destination URL

Or link to existing content

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Assigning Inline declared table to Field Symbols

There is an inline-declared table generated from a SELECT statement as below:

How can lt_result be assigned to a Field Symbol?

I tried the following way:

But I'm not able to call any components in the field symbol inside the loop like:

(syntax error: The data object "<FS_LT_RESULT>" does not have a structure and therefore does not have a component called "CARRID". )

Sandra Rossi's user avatar

3 Answers 3

Inline declaration of a field symbol for an internal table in an ASSIGN statement and inline declaration of a field symbol for the rows of the table in a LOOP.

source: https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abenfield-symbol_inline.htm

You declared that the field symbol is of unknown type ( ANY , i.e. the exact type is known only at run time), so the compiler can't be sure that the mentioned component ( CARRID ) exists, hence the syntax error.

If you want to mention a component statically, the compiler must be informed of the exact type (what components exist).

For instance, this would work:

Or use the inline declaration of the field symbol as you proposed in your own answer/solution:

NB: if your internal table had a type decided at run time only, it would be impossible to indicate statically the component name and you'd need to refer to the component dynamically:

EXAMPLE: FIELD SYMBOL : TYPE VBAK. DATA: ITAB TYPE TABLE OF VBAK .

SELECT * FROM VBAK INTO TABLE ITAB UP TO 10 ROWS.

LOOP AT ITAB ASSIGNING WRITE : / -VBELN . (WRITE WHICH OUTPUT YOU WANT TO DISPLAY) ENDLOOP.

user10789393's user avatar

Your Answer

Sign up or log in, post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged abap custom-data-type or ask your own question .

Hot Network Questions

assigning field symbol in abap

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

author's profile photo Former Member

Assign field symbols

I have an issue on

FIELD-SYMBOLS :

<itab> TYPE ANY TABLE ,

<wa> TYPE ANY,

I am facing assign error.

How to append values into internal table from WA using field symbols.??

Using Field Symbols To Process Internal Tables

After completing this lesson, you will be able to:

Using Field Symbols To Access Internal Tables

You have already learned how to iterate over an internal table using a structure as a work area. When you use this technique, the system copies data from the internal table into the work area. You then change the contents of the work area before copying them back into the internal table using the MODIFY statement.

assigning field symbol in abap

If you use this technique to process a large internal table, you risk causing performance problems because of the costs of copying the data back and forth. You can reduce these costs by replacing the work area with a field symbol.

Replacing a Work Area With a Field Symbol

A field symbol is a pointer. A pointer is a data object that knows the memory address of a different object and allows you to manipulate that object. In the case of internal tables, the pointer allows you to address a line of an internal table without first copying it into a work area. Since you work directly with the table line and not with a work area, you do not have to copy your changes back into the internal table.

assigning field symbol in abap

To define a field symbol, you use the FIELD-SYMBOLS statement and assign a name to the field symbol. The name must be included in angled brackets. You also assign a type to the field symbol. If you want to use a field symbol to process an internal table, you define the field symbol with the line type of the internal table, just as you would with a work area.

To use the field symbol in a loop over an internal table, you use the ASSIGNING addition in the LOOP statement. In each loop pass, the pointer now points to the corresponding line of the internal table and you can change its contents directly. As the field symbol has the line type of the internal table, you can treat it as you would a corresponding structure. After the name of the field symbol, you can type a dash, followed by the field that you want to change.

Since you are working directly with the internal table and not with a structure, there is no need for the MODIFY statement. By avoiding copying data, you can improve the performance of the loop considerably.

Runtime Comparison

assigning field symbol in abap

Here you can see the result of a runtime analysis in which an internal table is updated using a MODIFY statement and another is updated using a field symbol. In this case, the field symbol takes 40% less time than the modify statement. As a rule, you can expect to save 25–40% of the runtime when you update an internal table using a field symbol instead of a work area. The performance gain for read operations is smaller.

Try It Out: Using Field Symbols To Access Internal Tables

Save progress to your learning plan by logging in or creating an account

IMAGES

  1. SAP ABAP TECH: How to Use FIELD SYMBOLS in ABAP PROGRAM

    assigning field symbol in abap

  2. AbapForDummies: Utilizar Field-Symbols en tablas internas

    assigning field symbol in abap

  3. Relation between Data References and Field symbols with examples

    assigning field symbol in abap

  4. FPM OVP Application- Form Repeater UIBB

    assigning field symbol in abap

  5. SAP ABAP Tips and Tricks: March 2017

    assigning field symbol in abap

  6. SAP ABAP TECH: How to Use FIELD SYMBOLS in ABAP PROGRAM

    assigning field symbol in abap

VIDEO

  1. (LIVE) FORTNITE CUSTOMS MATCHMAKING SQUADS WITH VIEWERS!

  2. SAP Converting ABAP Query To ABAP Programs

  3. 5_DDIC_Table Creation_Part5

  4. Performance Improvement

  5. 16 DDIC Assigning Primary-Foreign Key Relationship

  6. Video 21

COMMENTS

  1. FIELD-SYMBOL - Inline Declaration - ABAP Keyword Documentation

    An inline declaration for a field symbol can only be made once within a context and the field symbol cannot yet be declared there using FIELD-SYMBOLS. If the type of the assigned memory area is defined by reference to a data type in ABAP Dictionary, it is used together with its semantic attributes, such as field help, input help, or conversion ...

  2. Field Symbols - ABAP Keyword Documentation

    Field Symbols. Field symbols are placeholders or symbolic names for existing data objects (or parts of existing data objects), declared by the statement FIELD-SYMBOLS or by the declaration operator FIELD-SYMBOL . A memory area is assigned to a field symbol at program runtime. A field symbol does not reserve physical space in the data area of a ...

  3. ABAP Tutorial => Field-Symbols

    FIELD-SYMBOLS: <fs_line> TYPE any, "generic <fs_struct> TYPE kna1. "non-generic Assigning. Field-Symbols are unassigned on declaration, which means that they are pointing to nothing. Accessing an unassigned Field-Symbol will lead to an exception, and, if uncaught, to a short dump.

  4. Assigning values to field symbols | SAP Community

    Mar 20, 2014 at 06:49 AM. hi, As per my understanding you need to populate the column A in the table, where the column B and C are already populated. For this you need to do as follows. Loop at itab assigning <fs>. <fs>-a = "your value". Endloop. where <fs> is the field symbol of type line of itab.

  5. Dynamic Programming in ABAP – Part 1 – Introduction to Field ...

    Dynamic programming is actually implemented using generic field symbols. The most commonly used generic types are TYPE ANY and TYPE ANY 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 ...

  6. abap - Assigning Inline declared table to Field Symbols ...

    LOOP AT lt_result ASSIGNING FIELD-SYMBOL(<fs_lt_result>). NB: if your internal table had a type decided at run time only, it would be impossible to indicate statically the component name and you'd need to refer to the component dynamically: ASSIGN COMPONENT ('CARRID') OF STRUCTURE <fs_lt_result> TO FIELD-SYMBOL(<field>). IF sy-subrc = 0.

  7. Assign field symbols | SAP Community

    data : lo_ref type data . field-symbols : <lt_table> type any table, <ls_data> type any . create data lo_ref type standard table of ( lv_type ). assign lo_ref->* to <lt_table> .""once u do this <lt_table> has type assigned. append initial line to <lt_table> assigning <ls_data>. Hope this solves your issue.

  8. Using Field Symbols To Process Internal Tables

    If you want to use a field symbol to process an internal table, you define the field symbol with the line type of the internal table, just as you would with a work area. To use the field symbol in a loop over an internal table, you use the ASSIGNING addition in the LOOP statement. In each loop pass, the pointer now points to the corresponding ...