SlideShare a Scribd company logo
1 of 3
Dynamic Programming with Field Symbols
May 1, 2014 · ABAP Leave a comment
Tagged: dynamic programming, field symbols, performance, snippet
Introduction
Field Symbols allows you to access content of variable to which its point to. This pointing to a variable is done
using ASSIGN statement. Field Symbol does not store any value it simply point to the memory where variable
value is store. Any operation to change value direct affects variable. Field symbols are different in a way that one
field symbol can be assigned to different variable throughout the program life time and statement executed using
field symbol will affect different variable depending on where it point to.
You can declare field symbols using
FIELD-SYMBOLS : <fs_name> TYPE ANY.
FIELD-SYMBOLS : <fs_name> TYPE mara.
FIELD-SYMBOLS : <fs_name> TYPE i .
You can use ANY to define a generic field symbol which will then inherit properties of variable its assigned to or
you can use a specific type to restrict assignment to compatible variable. This means that if you have field symbol
defined as I (integer) and you try to assign it to any other kind of variable then you will get ‘type-incompatible’
syntaxerror.
ASSIGN
Assignment of field symbol is done using ASSIGN statement. Below is an example where I have declared a
generic field symbol and assigned it different component of MARA work area.
FIELD-SYMBOLS : <fs_field> TYPE ANY .
DATA : ls_mara TYPE mara .
SELECT SINGLE *
INTO ls_mara
FROM mara
WHERE matnr = 'A1' .
ASSIGN ls_mara-matnr TO <fs_field> .
WRITE : / <fs_field> .
ASSIGN ls_mara-matkl TO <fs_field> .
WRITE : / <fs_field> .
Caution
Accessing field symbol before its assigned to a variable is something which will trigger runtime error ‘Field
symbol has not yet been assigned’.An unassigned field symbol cannot be checked when you activate the program
and because of this reason programs using field symbol, if checks are not done properly, often produce runtime
error.
Whist using field symbols you have to take extra care to check if field symbol points to a variable before you start
any operation on it. If you are accessing field symbol ASSIGN statement you must check of assignment has
executed successfully.
ASSIGN ls_mara-matnr TO <fs_field> .
IF sy-subrc = 0 .
WRITE : / <fs_field> .
ENDIF.
or
ASSIGN ls_mara-matnr TO <fs_field> .
IF <fs_field> is ASSIGNED .
WRITE : / <fs_field> .
ENDIF.
Performance Gain
Field symbols can also be used to access internal table content and it particularly have performance advantage as
content of table is not moved to workarea instead field symbol is simply pointed to right record.
DATA : i_mara TYPE TABLE OF mara .
FIELD-SYMBOLS : <fs_mara> TYPE mara .
READ TABLE i_mara ASSIGNING <fs_mara> WITH KEY matnr = 'A1' .
IF sy-subrc = 0 .
WRITE : <fs_mara>-matnr .
ENDIF.
DATA : i_mara TYPE TABLE OF mara .
FIELD-SYMBOLS : <fs_mara> TYPE mara .
LOOP AT i_mara ASSIGNING <fs_field> .
WRITE : / <fs_field>-matnr .
ENDLOOP..
Note that if you change content of field symbol after READ internal table statement or within LOOP you are
modifying the content of field symbol as there is no work area.
Dynamic Stuff
ASSIGN COMPONENT statement has two variations which allows you to access field of workarea at runtime.
In first variantion you can specify field name in terms of number. It’s like saying assign first (or second,third or
so on) component of workarea to field symbol. In below code program simply access all component of work area
within WHILE loop and exit when assignment fail, which it will once it has accessed all component.
DATA : ls_mara TYPE mara .
FIELD-SYMBOLS : <fs_field> TYPE ANY.
SELECT SINGLE *
INTO ls_mara
FROM mara
WHERE matnr = 'A1' .
WHILE sy-subrc = 0.
ASSIGN COMPONENT sy-index OF STRUCTURE ls_mara TO <fs_field>.
IF sy-subrc = 0 .
WRITE : / <fs_field> .
ENDIF.
ENDWHILE.
In second variation, which I find more useful, you can specify component name instead of number to access that
field of work area.
DATA : ls_mara TYPE mara .
FIELD-SYMBOLS : <fs_field> TYPE ANY.
DATA : lv_compnent_name TYPE char10 VALUE 'MATNR' .
SELECT SINGLE *
INTO ls_mara
FROM mara
WHERE matnr = 'A1' .
ASSIGN COMPONENT lv_compnent_name OF STRUCTURE ls_mara TO <fs_field>.
IF sy-subrc = 0 .
WRITE : / <fs_field> .
ENDIF.

More Related Content

What's hot

BAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 LectureBAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 LectureWake Tech BAS
 
Excel VBA programming basics
Excel VBA programming basicsExcel VBA programming basics
Excel VBA programming basicsHang Dong
 
PLSQL CURSOR
PLSQL CURSORPLSQL CURSOR
PLSQL CURSORArun Sial
 
Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Techglyphs
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Blue Elephant Consulting
 
Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2Blue Elephant Consulting
 
New Dynamic Array Functions. Excel Tutorial
New Dynamic Array Functions. Excel TutorialNew Dynamic Array Functions. Excel Tutorial
New Dynamic Array Functions. Excel TutorialIlgar Zarbaliyev
 
Using Functions - R.D.Sivakumar
Using Functions - R.D.SivakumarUsing Functions - R.D.Sivakumar
Using Functions - R.D.SivakumarSivakumar R D .
 
Intro To C++ - Class 12 - For, do … While
Intro To C++ - Class 12 - For, do … WhileIntro To C++ - Class 12 - For, do … While
Intro To C++ - Class 12 - For, do … WhileBlue Elephant Consulting
 
Domains in IBM Maximo Asset Management
Domains in IBM Maximo Asset ManagementDomains in IBM Maximo Asset Management
Domains in IBM Maximo Asset ManagementRobert Zientara
 
Fill series. Data validation. Excel Tutorial
Fill series. Data validation. Excel TutorialFill series. Data validation. Excel Tutorial
Fill series. Data validation. Excel TutorialIlgar Zarbaliyev
 
PL/SQL - CURSORS
PL/SQL - CURSORSPL/SQL - CURSORS
PL/SQL - CURSORSIshaRana14
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++Muhammad Hammad Waseem
 
Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Blue Elephant Consulting
 

What's hot (19)

BAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 LectureBAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 Lecture
 
Excel
ExcelExcel
Excel
 
Excel VBA programming basics
Excel VBA programming basicsExcel VBA programming basics
Excel VBA programming basics
 
PLSQL CURSOR
PLSQL CURSORPLSQL CURSOR
PLSQL CURSOR
 
Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...
 
Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2
 
New Dynamic Array Functions. Excel Tutorial
New Dynamic Array Functions. Excel TutorialNew Dynamic Array Functions. Excel Tutorial
New Dynamic Array Functions. Excel Tutorial
 
Using Functions - R.D.Sivakumar
Using Functions - R.D.SivakumarUsing Functions - R.D.Sivakumar
Using Functions - R.D.Sivakumar
 
Visual Basic
Visual BasicVisual Basic
Visual Basic
 
Visual Basic
Visual BasicVisual Basic
Visual Basic
 
Formula in MS Excel
Formula in MS ExcelFormula in MS Excel
Formula in MS Excel
 
Intro To C++ - Class 12 - For, do … While
Intro To C++ - Class 12 - For, do … WhileIntro To C++ - Class 12 - For, do … While
Intro To C++ - Class 12 - For, do … While
 
LISP: Errors In Lisp
LISP: Errors In LispLISP: Errors In Lisp
LISP: Errors In Lisp
 
Domains in IBM Maximo Asset Management
Domains in IBM Maximo Asset ManagementDomains in IBM Maximo Asset Management
Domains in IBM Maximo Asset Management
 
Fill series. Data validation. Excel Tutorial
Fill series. Data validation. Excel TutorialFill series. Data validation. Excel Tutorial
Fill series. Data validation. Excel Tutorial
 
PL/SQL - CURSORS
PL/SQL - CURSORSPL/SQL - CURSORS
PL/SQL - CURSORS
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
 
Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1
 

Similar to Fieldsymbols

Applicative Functor - Part 3
Applicative Functor - Part 3Applicative Functor - Part 3
Applicative Functor - Part 3Philip Schwarz
 
Complete list of all sap abap keywords
Complete list of all sap abap keywordsComplete list of all sap abap keywords
Complete list of all sap abap keywordsPrakash Thirumoorthy
 
Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosGetting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosNick Weisenberger
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfacsmadurai
 
LISP: Macros in lisp
LISP: Macros in lispLISP: Macros in lisp
LISP: Macros in lispLISP Content
 
Applicative Functor - Part 2
Applicative Functor - Part 2Applicative Functor - Part 2
Applicative Functor - Part 2Philip Schwarz
 
INTRODUCTION TO C PROGRAMMING - PART 2
INTRODUCTION TO C PROGRAMMING - PART 2INTRODUCTION TO C PROGRAMMING - PART 2
INTRODUCTION TO C PROGRAMMING - PART 2JEENA SARA VIJU
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes NUST Stuff
 
Sap script system_symbol
Sap script system_symbolSap script system_symbol
Sap script system_symbolmoderngladiator
 
Open sap ui51_week_2_unit_3_acdt_exercises
Open sap ui51_week_2_unit_3_acdt_exercisesOpen sap ui51_week_2_unit_3_acdt_exercises
Open sap ui51_week_2_unit_3_acdt_exercisesvikram sukumar
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macrosAnand Kumar
 
Python-review1.pdf
Python-review1.pdfPython-review1.pdf
Python-review1.pdfpaijitk
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxSahajShrimal1
 

Similar to Fieldsymbols (18)

Applicative Functor - Part 3
Applicative Functor - Part 3Applicative Functor - Part 3
Applicative Functor - Part 3
 
Complete list of all sap abap keywords
Complete list of all sap abap keywordsComplete list of all sap abap keywords
Complete list of all sap abap keywords
 
Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosGetting started with Microsoft Excel Macros
Getting started with Microsoft Excel Macros
 
Cursors.ppt
Cursors.pptCursors.ppt
Cursors.ppt
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdf
 
Abap 7.40
Abap 7.40Abap 7.40
Abap 7.40
 
LISP: Macros in lisp
LISP: Macros in lispLISP: Macros in lisp
LISP: Macros in lisp
 
LISP: Macros in lisp
LISP: Macros in lispLISP: Macros in lisp
LISP: Macros in lisp
 
Applicative Functor - Part 2
Applicative Functor - Part 2Applicative Functor - Part 2
Applicative Functor - Part 2
 
MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 
INTRODUCTION TO C PROGRAMMING - PART 2
INTRODUCTION TO C PROGRAMMING - PART 2INTRODUCTION TO C PROGRAMMING - PART 2
INTRODUCTION TO C PROGRAMMING - PART 2
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
 
Sap script system_symbol
Sap script system_symbolSap script system_symbol
Sap script system_symbol
 
Open sap ui51_week_2_unit_3_acdt_exercises
Open sap ui51_week_2_unit_3_acdt_exercisesOpen sap ui51_week_2_unit_3_acdt_exercises
Open sap ui51_week_2_unit_3_acdt_exercises
 
SAS Macros
SAS MacrosSAS Macros
SAS Macros
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
 
Python-review1.pdf
Python-review1.pdfPython-review1.pdf
Python-review1.pdf
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
 

Fieldsymbols

  • 1. Dynamic Programming with Field Symbols May 1, 2014 · ABAP Leave a comment Tagged: dynamic programming, field symbols, performance, snippet Introduction Field Symbols allows you to access content of variable to which its point to. This pointing to a variable is done using ASSIGN statement. Field Symbol does not store any value it simply point to the memory where variable value is store. Any operation to change value direct affects variable. Field symbols are different in a way that one field symbol can be assigned to different variable throughout the program life time and statement executed using field symbol will affect different variable depending on where it point to. You can declare field symbols using FIELD-SYMBOLS : <fs_name> TYPE ANY. FIELD-SYMBOLS : <fs_name> TYPE mara. FIELD-SYMBOLS : <fs_name> TYPE i . You can use ANY to define a generic field symbol which will then inherit properties of variable its assigned to or you can use a specific type to restrict assignment to compatible variable. This means that if you have field symbol defined as I (integer) and you try to assign it to any other kind of variable then you will get ‘type-incompatible’ syntaxerror. ASSIGN Assignment of field symbol is done using ASSIGN statement. Below is an example where I have declared a generic field symbol and assigned it different component of MARA work area. FIELD-SYMBOLS : <fs_field> TYPE ANY . DATA : ls_mara TYPE mara . SELECT SINGLE * INTO ls_mara FROM mara WHERE matnr = 'A1' . ASSIGN ls_mara-matnr TO <fs_field> . WRITE : / <fs_field> . ASSIGN ls_mara-matkl TO <fs_field> . WRITE : / <fs_field> . Caution Accessing field symbol before its assigned to a variable is something which will trigger runtime error ‘Field symbol has not yet been assigned’.An unassigned field symbol cannot be checked when you activate the program and because of this reason programs using field symbol, if checks are not done properly, often produce runtime error. Whist using field symbols you have to take extra care to check if field symbol points to a variable before you start any operation on it. If you are accessing field symbol ASSIGN statement you must check of assignment has executed successfully.
  • 2. ASSIGN ls_mara-matnr TO <fs_field> . IF sy-subrc = 0 . WRITE : / <fs_field> . ENDIF. or ASSIGN ls_mara-matnr TO <fs_field> . IF <fs_field> is ASSIGNED . WRITE : / <fs_field> . ENDIF. Performance Gain Field symbols can also be used to access internal table content and it particularly have performance advantage as content of table is not moved to workarea instead field symbol is simply pointed to right record. DATA : i_mara TYPE TABLE OF mara . FIELD-SYMBOLS : <fs_mara> TYPE mara . READ TABLE i_mara ASSIGNING <fs_mara> WITH KEY matnr = 'A1' . IF sy-subrc = 0 . WRITE : <fs_mara>-matnr . ENDIF. DATA : i_mara TYPE TABLE OF mara . FIELD-SYMBOLS : <fs_mara> TYPE mara . LOOP AT i_mara ASSIGNING <fs_field> . WRITE : / <fs_field>-matnr . ENDLOOP.. Note that if you change content of field symbol after READ internal table statement or within LOOP you are modifying the content of field symbol as there is no work area. Dynamic Stuff ASSIGN COMPONENT statement has two variations which allows you to access field of workarea at runtime. In first variantion you can specify field name in terms of number. It’s like saying assign first (or second,third or so on) component of workarea to field symbol. In below code program simply access all component of work area within WHILE loop and exit when assignment fail, which it will once it has accessed all component. DATA : ls_mara TYPE mara . FIELD-SYMBOLS : <fs_field> TYPE ANY. SELECT SINGLE * INTO ls_mara FROM mara WHERE matnr = 'A1' . WHILE sy-subrc = 0. ASSIGN COMPONENT sy-index OF STRUCTURE ls_mara TO <fs_field>. IF sy-subrc = 0 . WRITE : / <fs_field> . ENDIF. ENDWHILE.
  • 3. In second variation, which I find more useful, you can specify component name instead of number to access that field of work area. DATA : ls_mara TYPE mara . FIELD-SYMBOLS : <fs_field> TYPE ANY. DATA : lv_compnent_name TYPE char10 VALUE 'MATNR' . SELECT SINGLE * INTO ls_mara FROM mara WHERE matnr = 'A1' . ASSIGN COMPONENT lv_compnent_name OF STRUCTURE ls_mara TO <fs_field>. IF sy-subrc = 0 . WRITE : / <fs_field> . ENDIF.