Skip to Content
avatar image
Former Member

Dynamic Column Value using Field Symbol

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.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    avatar image
    Former Member
    Aug 10, 2010 at 08: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.

    Add comment
    10|10000 characters needed characters exceeded

  • Aug 10, 2010 at 06:41 AM

    How have you defined the field symbol <fs_col> ?

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

    BR,

    Suhas

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 10, 2010 at 07: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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 10, 2010 at 07: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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 10, 2010 at 08: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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 10, 2010 at 09: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

    Add comment
    10|10000 characters needed characters exceeded