‎2010 Aug 10 7:32 AM
I have a requirement in which i need to access columns of an internal table dynamically, manipulate them using work area and update back onto the internal table.
The code looks something like this.
loop at it_table1.
if condition1 = '1'.
lv_colname = 'col1'.
elseif condition2 = '2'.
lv_colname = 'col2'.
elseif..
..
..
concatenate 'wa_itab-' lv_colname into lv_field.
assign (lv_field) to <fs_col>.
if <fs_col> is assigned.
<fs_col> = <fs_col> + 1.
endif.
endloop.
append wa_itab to itab.
The above code gives a runtime error "Unable to Interpret as Number" as the field symbol doe not correspond to the correct datatype. I basically need to increase the value of a column dynamically by 1. Please suggest a way to do this.
‎2010 Aug 10 9:24 AM
Here is an example:
DATA : wa0001 TYPE pa0001.
wa0001-pernr = 1.
DATA : lv_pernr TYPE string VALUE 'pernr',
lv_fs TYPE string.
FIELD-SYMBOLS : <fs> TYPE ANY.
CONCATENATE 'wa0001-' lv_pernr INTO lv_fs.
ASSIGN (lv_fs) TO <fs>.
IF <fs> IS ASSIGNED.
<fs> = <fs> + 1.
ENDIF.
WRITE <fs>.This is working fine...
KR
Veeranji Reddy P.
‎2010 Aug 10 7:41 AM
How have you defined the field symbol <fs_col> ?
It should be generic i.e., TYPE ANY.
BR,
Suhas
‎2010 Aug 10 8:19 AM
‎2010 Aug 10 8:38 AM
Hi,
What is the name of the column? And what is the type of that field? That might give you a clue to why it is going wrong? Perhaps a column of the wrong type is clicked upon and not excluded from the conditions?
Roy
‎2010 Aug 10 8:40 AM
Hello,
check the type of your fields col1 , col2 they should be numberic if char or string while execution of the statement
<fs_col> = <fs_col> + 1.
it will fail.
Regards,
Gunjan
‎2010 Aug 10 9:03 AM
The names of the columns are col1,col2,col3..etc..
They are all of numeric type.
‎2010 Aug 10 9:10 AM
Hi,
I guess the use of Work area is not correct..
I can see
loop at it_table1.but not
loop at it_table1 into wa_itab.Please check once...
KR
Veeranji Reddy P.
‎2010 Aug 10 9:24 AM
Here is an example:
DATA : wa0001 TYPE pa0001.
wa0001-pernr = 1.
DATA : lv_pernr TYPE string VALUE 'pernr',
lv_fs TYPE string.
FIELD-SYMBOLS : <fs> TYPE ANY.
CONCATENATE 'wa0001-' lv_pernr INTO lv_fs.
ASSIGN (lv_fs) TO <fs>.
IF <fs> IS ASSIGNED.
<fs> = <fs> + 1.
ENDIF.
WRITE <fs>.This is working fine...
KR
Veeranji Reddy P.
‎2010 Aug 10 1:34 PM
‎2010 Aug 10 10:09 AM
Hi,
TYPES : BEGIN OF ty_table1,
condition TYPE n,
col1 TYPE n,
col2 TYPE n,
END OF ty_table1.
TYPES : BEGIN OF ty_itab,
col1 TYPE n,
col2 TYPE n,
END OF ty_itab.
TYPES : ty_table1_t TYPE TABLE OF ty_table1.
TYPES : ty_itab_t TYPE TABLE OF ty_itab.
DATA : lt_table1 TYPE STANDARD TABLE OF ty_table1.
DATA : lt_itab TYPE STANDARD TABLE OF ty_itab.
DATA : wa_table1 TYPE ty_table1.
DATA : wa_itab TYPE ty_itab.
DATA : lv_colname TYPE string.
DATA : lv_field TYPE string.
FIELD-SYMBOLS : <fs_col> TYPE ANY.
START-OF-SELECTION.
wa_table1-condition = '1'.
wa_table1-col1 = '1'.
wa_table1-col2 = '1'.
APPEND wa_table1 TO lt_table1.
wa_table1-condition = '2'.
wa_table1-col1 = '2'.
wa_table1-col2 = '2'.
APPEND wa_table1 TO lt_table1.
LOOP AT lt_table1 INTO wa_table1.
IF wa_table1-condition = '1'.
lv_colname = 'col1'.
ELSEIF wa_table1-condition = '2'.
lv_colname = 'col2'.
ENDIF.
CONCATENATE 'wa_itab-' lv_colname INTO lv_field.
MOVE-CORRESPONDING wa_table1 to wa_itab.
ASSIGN (lv_field) TO <fs_col>.
IF <fs_col> IS ASSIGNED.
<fs_col> = <fs_col> + 1.
ENDIF.
ENDLOOP.
APPEND wa_itab TO lt_itab.
Try this..
Regards,
Gunjan
Edited by: Gunjan Tyagi on Aug 10, 2010 11:09 AM