Online Tutorials & Training Materials | STechies.com
Register Login

Changing Field Name Dynamically

|| || 4

Changing Field Name Dynamically
Stechies

Dear All,

I have to change the field name dynamically in the loop. Database Name is PA9002 having fields ZFAMSA1, ZFAMSA2, ZFAMSA3 up to ZFAMSA10. I want to pass the data to these fields in loop and update.

Following is my Code.

LOOP AT GT_PA0021 INTO GS_PA0021
MOVE GA_PA0021-SUBTY TO P9002-FAMSA1.
ENDLOOP.

In above code I have to change the Field name in each loop pass that P9002-FAMSA1, then in next loop P9002-FAMSA2 then next loop pass P9002-FAMSA3. So please suggest me how to change the field name dynamically in Loop.

Waiting for reply.

Thank You

Regards

 


Comments

  • 28 Sep 2010 10:07 am
    I think u can go for dynamic internal tables.
  • 28 Sep 2010 10:07 am
    Hi,

    One thing u can do is have a counter in the loop in order to increment the value of field as ZFAMSA1, ZFAMSA2, ZFAMSA3 store the internal table name along with field in one variable as I mentioned in the below code.

    Data : g_counter(1) type n value 0,
    g_field_init( 11) type c value 'P9002-FAMSA' ,
    g_field_final( 12) type c.

    LOOP AT GT_PA0021 INTO GS_PA0021
    g_counter = g_counter + 1.

    concatenate g_field_init
    g_counter
    into
    g_field_final.
    MOVE GA_PA0021-SUBTY TO g_field_final.

    if u want to exit from loop once the values reaches 10 then

    if g_counter = 10
    exit.
    endif.
    ENDLOOP.
    Hope this code should help.Try this.
    Regards,
  • 28 Sep 2010 10:08 am
    Hi,

    A slight change in code.Use field symbol.

    Data : g_counter(1) type n value 0,
    g_field_init( 11) type c value 'P9002-FAMSA' ,
    g_field_final( 12) type c.

    field-symbols: type any.

    LOOP AT GT_PA0021 INTO GS_PA0021
    g_counter = g_counter + 1.

    concatenate g_field_init
    g_counter
    into
    g_field_final.
    assign g_field_final to .
    if sy-subrc eq 0.
    MOVE GA_PA0021-SUBTY TO .
    clear .
    endif.
    if u want to exit from loop once the values reaches 10 then

    if g_counter = 10
    exit.
    endif.
    ENDLOOP.
  • 22 Oct 2010 11:37 am
    Try using ASSIGN COMPONENT using field symbol that will work......
    In this case You may need to define 2 fields symbols...
    one is for the whole structure and the other is for component(type ANY).