Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Dynamic Column Value using Field Symbol

Former Member
0 Likes
4,427

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.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,204

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.

9 REPLIES 9
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,204

How have you defined the field symbol <fs_col> ?

It should be generic i.e., TYPE ANY.

BR,

Suhas

Read only

Former Member
0 Likes
2,204

Yes, i have declared it as "type any".

Read only

Former Member
0 Likes
2,204

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

Read only

Former Member
0 Likes
2,204

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

Read only

0 Likes
2,204

The names of the columns are col1,col2,col3..etc..

They are all of numeric type.

Read only

Former Member
0 Likes
2,204

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.

Read only

Former Member
0 Likes
2,205

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.

Read only

0 Likes
2,204

That solved the problem! Thanx!

Read only

Former Member
0 Likes
2,204

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