Dynamic Programming in
Dynamic Programming in
Dynamic Programming in
Programming in
SAP ABAP
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:
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.
Modifying internal table records – We can declare a field symbol of type any structure, which we
can use while looping through an internal table.
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 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:
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:
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 TABLEfield
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.
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.
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.
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.
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.
I think the VALUE operator will make the use of ASSIGNING as in the idiom APPEND INITIAL LINE TO ..
ASSIGNING less popular.