• 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

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.

Got any ABAP Question?

pdf

  • Advertise with us
  • Privacy Policy

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

ビズドットオンライン

【ABAP】FIELD-SYMBOLS命令―フィールドシンボルを3分で

assign to field symbol

本ページで FIELD-SYMBOLS命令 (フィールドシンボルとは何か?)をご説明します。

assign to field symbol

フィールドシンボルとは、その名の通り「シンボル」です。つまり、他の変数や・構造・内部テーブルを指し示すリンクやショートカットのようなイメージです。

このページでは、初心者泣かせのフィールドシンボルについて1から分かりやすく解説します。

フィールドシンボルとは何か?

  • FIELD-SYMBOLS命令の使い方
  • FIELD-SYMBOLS命令を用いたサンプルコード
  • FIELD-SYMBOLS命令の注意点・落とし穴

ABAPerを目指す方であれば 知らないと恥ずかしい基本知識 です。是非最後までご覧ください。

構文ルール:FIELD-SYMBOLS命令

Field-symbols命令:サンプルコード, loop ~ assigning <fs>, sap / abapを1から学習したい方は.

まずは、フィールドシンボルの宣言方法から。

FIELD-SYMBOLS命令では <> が必須。 <> によって、それがフィールドシンボルであると識別されます。

また、フィールドシンボルの宣言は DATA命令 などと同様に、ABAPプログラム中の任意の場所で行うことができますが、 クラス やインターフェースの宣言部分に記述することはできません。

また、フィールドシンボルの割り当て先のデータ型が分かっている場合はDATA命令と同じようにTYPEオプションを利用してデータ型を指定することができます。

フィールドシンボルとは?

一言で言うと、変数や構造などのデータオブジェクトを指し示すシンボルです。

assign to field symbol

上記の図では、<fs>が「変数A」を指し示しています。つまり、<fs>は「変数A」の代わりとして扱えるということを示しています。

assign to field symbol

もし、フィールドシンボル<fs>に値を代入しようとすれば、それはすなわち「変数A」を操作しているのと同じになるのです。これが、フィールドシンボルの基本的なイメージです。

より専門的な表現をすると、フィールドシンボルは変数Aのメモリのアドレス値を保持しているということができます。

フィールドシンボルの実際の利用方法を見ていきながら、より具体的なイメージを頭に思い浮かべましょう。

フィールドシンボルの宣言と、参照先変数への割り当て(ASSIGN命令)のサンプルコードです。

変数aを<FS1>に割り当てました。図で表すと以下のような状態です。

assign to field symbol

<FS1>は変数aを参照している状態ということ。すなわち、 <FS1>に対して何らかの処理を行った場合は、変数aに対して処理を行っていると同意 です。そのため、<FS1>を WRITE すれば変数aの値が表示されるということになります。

もう1つサンプルコードを見てみましょう。

今度は逆に<FS1>に対して「Hello World」を格納しています。<FS1>は変数aを参照していますので、変数aに値が格納しているのと同じ状態になります。

これが、フィールドシンボルの初歩的な使い方です。

LOOP処理では、オプション「ASSIGNING」を利用することで処理対象レコードをフィールドシンボルに割り当てることができます。

以下は内部テーブルに格納したユーザ情報を LOOP しながら、パスワードを変更するプログラムの抜粋です。

同じ処理をフィールドシンボルを利用せずにコーディングしたのがこちら。

フィールドシンボルを利用することで、作業用の構造を定義する必要がなくなり、プログラム実行中に消費するメモリを削減することができます。

このフィールドシンボルの使い方は、もっとも頻繁に目にする形です。しっかりと理解しておきましょう。

assign to field symbol

初めてABAPを勉強するのは結構難しいですよね。

でもその悩みを抱えているのは一人じゃありません。全てのABAP使いが同じ道を進んできました。

ABAPをはじめとするプログラミングスキルを武器に、 時間と場所に捉われない自由な生き方 を目指してみませんか?

assign to field symbol

あなたの技術、もっと価値ある場所で活かしませんか? SAPエンジニア のキャリア支援はこちらから↓

読者料典   【完全無料】ABAP:学習カリキュラム  ←こちらから!

IMAGES

  1. Video 22: ABAP Field Symbols

    assign to field symbol

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

    assign to field symbol

  3. Relation between Data References and Field symbols with examples

    assign to field symbol

  4. Relation between Data References and Field symbols with examples

    assign to field symbol

  5. SAP Certified ABAP Consultant: Dynamic Programming with Field Symbols

    assign to field symbol

  6. ABAP ASSIGN component to a Field Symbol

    assign to field symbol

VIDEO

  1. what is field symbol in sap abap|SAP abap tutorial

  2. حل مشكلة رسالة الخطأ field symbol has not been assigned yet على بوابة الضرائب الرئيسية الجديدة SAP

  3. 4

  4. Assign Domain to Field in ArcGIS

  5. Working with Field Symbols ABAP Tutorials

  6. FIELD SYMBOLS

COMMENTS

  1. Assigning Data Objects to Field Symbols

    To assign a data object to a field symbol, use the ASSIGN statement. The ASSIGN statement has several variants and parameters. The Basic Form of the ASSIGN Statement Assigning Components of Structures to a Field Symbol Casting Data Objects Data Areas for Field Symbols You can assign single lines from internal tables to field symbols.

  2. Assigning Data Objects to Field Symbols

    Data Areas for Field Symbols. You can only assign data objects from the data areas of the ABAP program to a filed 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 are:

  3. ABAP Tutorial => Field-Symbols

    Unassigning Sometimes it could be useful to reset a Field-Symbol. This can be done using UNASSIGN. UNASSIGN <fs>. * Access on <fs> now leads to an exception again Use for internal tables Field-Symbols may be used to modify internal tables. LOOP AT itab INTO DATA (wa). * Only modifies wa_line wa-name1 = 'Max'. ENDLOOP.

  4. Solved: Assign Field-Symbol with new Cond # Syntax

    Is there any way to assign a field-symbol to the output, like: "ASSIGN <fs_estrh> TO VALUE # / COND # ( .... )" ? Then I could just Assign the fieldsymbol via constructor operator and modify it in the next statement. Hope this explaines my question and thanks for any considerations to my question! Best regards Julian Horn SAP Managed Tags:

  5. Assigning Inline declared table to Field Symbols

    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. LOOP AT <lt_result> ASSIGNING FIELD-SYMBOL (<line>). ... ENDLOOP. source: https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abenfield-symbol_inline.htm Share Follow

  6. Assign value to a field symbol when this field symbol contains the name

    Assign value to a field symbol when this field symbol contains the name of a variable. 3743 Views. Follow RSS Feed ... v_tabix TYPE c LENGTH 2, v_num_c TYPE c LENGTH 9, v_text TYPE c LENGTH 7 VALUE 'line-at', FIELD-SYMBOLS <var> TYPE any. LOOP AT t_message INTO l_message. IF sy-tabix LE 100. v_tabix = sy-tabix. CONCATENATE v_text v_tabix INTO v ...

  7. How to assign ranges ( select-option)to field symbol

    i want to assign (v_rname ) to <field -symbol> ie is range to field symbol. and i want to update the <field -symbol>-sign ='I' <field -symbol>-LOW ='some value' append <field -symbol>. This is the logic i want to follow, for this how should i have to declare the field symbol ? I couldn't assign a range to field symbol . Please help me. thanks ...

  8. FIELD-SYMBOL

    ... FIELD-SYMBOL (<fs>) ... Effect A declaration expression with the declaration operator FIELD-SYMBOL declares a field symbol <fs> to which a memory area is assigned in the current operand position. The declared field symbol is visible statically in the program from FIELD-SYMBOL (<fs>) and is valid in the current context.

  9. 【Abap】Field-symbols命令―フィールドシンボルを3分で

    フィールドシンボルとは? FIELD-SYMBOLS命令:サンプルコード LOOP ~ ASSIGNING <FS> SAP / ABAPを1から学習したい方は 構文ルール:FIELD-SYMBOLS命令 まずは、フィールドシンボルの宣言方法から。 * フィールドシンボルの宣言 FIELD-SYMBOLS <フィールドシンボル名> . FIELD-SYMBOLS命令では <> が必須。 <> によって、それがフィールドシンボルであると識別されます。 * フィールドシンボルの宣言NG例「<>なしはダメ」 FIELD-SYMBOLS フィールドシンボル名 .

  10. how to append data into table using field symbol

    You can APPEND by this way - - "APPEND <field-symbol> TO internal_table." before that you need to assign the field-symbol. You can try this code - DATA : t_itab1 TYPE TABLE OF spfli, t_itab2 TYPE TABLE OF sflight, wa_sflight TYPE sflight. FIELD-SYMBOLS : <fs_spfli> TYPE spfli, <fs_sflight> TYPE sflight.

  11. Assignments to Field Symbols

    Assignments to Field Symbols Show TOC Assignments to Field Symbols Until now the ASSIGN statement made it possible to define addresses past field limits by specifying the offset or length. There was only a runtime error when addressing past the limits of the data segment.

  12. MODIFY TABLE of type ANY TABLE from field-symbol of type ANY

    Remove this and declare the field symbol inline to get a pointer to the actual output: DATA: rt_data TYPE REF TO data, ls_key TYPE /bobf/s_frw_key, lt_key TYPE /bobf/t_frw_key. FIELD-SYMBOLS: <fs_feld_fu_key> TYPE any, <fs_feld_walzzyk> TYPE any. IF <fs_data> IS ASSIGNED. LOOP AT ct_data ASSIGNING FIELD-SYMBOL(<fs_data>).

  13. How to assign a field symbol to a table type?

    Solved: Hi experts, I am having a problem to assign a field symbol to a table type: DATA: lt_bapiadtel TYPE TABLE OF bapiadtel,

  14. Assigning Components of Structures to a Field Symbol

    If you assign a data object to a field symbol generically, or using Casting, or pass it generically to the parameter interface of a procedure, you cannot address its components either statically or dynamically. Instead, you must use the above statement. This allows indirect access either using the component name or its index number.

  15. Create a structure from a dynamically assigned <itab>

    Create a structure from a dynamically assigned <itab>. In a method I have a reference to a table that was declared like this: DATA: tabname TYPE tabname, dref TYPE REF TO data, FIELD-SYMBOLS: <itab> TYPE ANY TABLE. CREATE DATA dref TYPE TABLE OF (tabname). ASSIGN dref->* TO <itab>. SELECT * FROM (tabname) UP TO 5 ROWS INTO TABLE <itab>.