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

How to LOOP AT dynamic internal tables.

Former Member
0 Likes
16,606

I have an internal table name declared in field I_DYN_TAB with me and I want to loop at this I_DYN_TAB.

( 4 example:

DATA: I_DYN_TAB(10) VALUE 'I_TAB'.

where I_TAB is my internal table with data in it.)

I already tried couple of things.

A. READ TABLE (I_DYN_TAB) WITH KEY I_DYN_TAB-FIELD1

but, it is not allowing me to read it dynamically this way.

B. LOOP AT (I_DYN_TAB). Again this is also not working.

Another option I tried is to create a field symbol for the internal table, which again is not useful or may be I am doing it wrong.

So if anybody can help me in solving this problem, it would be great.

Waiting for reply.

Regards

Arpit

1 ACCEPTED SOLUTION
Read only

Former Member
5,797

Ok try this >

REPORT ZTEST .

DATA : BEGIN OF I_ITAB OCCURS 0,

TEST(5),

END OF I_ITAB.

DATA: I_DYN_TAB(10) VALUE 'I_ITAB[]'.

DATA: DTAB TYPE REF TO DATA.

FIELD-SYMBOLS : <ITAB> TYPE ANY TABLE ,

<WA> TYPE ANY.

I_ITAB-TEST = 'TEST'.

APPEND I_ITAB.

ASSIGN (I_DYN_TAB) TO <ITAB>.

LOOP AT <ITAB> ASSIGNING <WA>.

WRITE <WA>.

ENDLOOP.

9 REPLIES 9
Read only

Former Member
0 Likes
5,797

hi, if your internal table is named 'I_TAB', do like following:


DATA: NEW_LINE TYPE REF TO DATA.

FIELD-SYMBOLS:<FS_1> TYPE ANY TABLE,
              <FS_2>.

ASSIGN <b>I_TAB</b> TO <FS_1>.
CREATE DATA NEW_LINE LIKE LINE OF <FS_1>.
ASSIGN NEW_LINE->*  TO <FS_2>.

LOOP AT <FS_1> ASSIGNING <FS_2>.
* add your handling logic here
ENDLOOP.

Hope it will be helpful

thanks

Read only

0 Likes
5,797

Hi Jhenglin,

Thanks for your reply....appreciate it.

But I_TAB is stored in I_DYN_TAB in a variable as I mentioned earlier so I dont think I can use your statement ASSIGN I_TAB TO <FS_1>.

Is there any other way.

Thanks and Regards,

Arpit.

Read only

Former Member
0 Likes
5,797

U can use

ASSIGN COMPONENT...

Eg:

FIELD-SYMBOLS: <dyn_field>,
                 <dyn_field1>,
                 <dyn_field2>,
                 <dyn_field3> TYPE ANY .

* Test when UOM, 'KMEIN' is '%'
*        <b>ASSIGN COMPONENT 'KMEIN' OF STRUCTURE p_line1 TO <dyn_field1>.</b>
*        ASSIGN COMPONENT 'KBETR' OF STRUCTURE p_line1 TO <dyn_field2>.
*        ASSIGN COMPONENT 'KBETR' OF STRUCTURE p_line1 TO <dyn_field>.

* Test when Curr, 'KONWA' is '%'
        ASSIGN COMPONENT 'KONWA' OF STRUCTURE p_line1 TO <dyn_field1>.
        ASSIGN COMPONENT 'KBETR' OF STRUCTURE p_line1 TO <dyn_field2>.
        ASSIGN COMPONENT 'KBETR' OF STRUCTURE p_line1 TO <dyn_field>.

        IF <dyn_field1> = '%'.
          <dyn_field> =  <dyn_field2> / 10.
        ENDIF.

        if <dyn_field2> LE -1.
          <dyn_field> = <dyn_field> * -1.
        endif.
          INSERT p_line1 INTO TABLE p_output1.

Hope this helps.

Read only

0 Likes
5,797

Hi Judith,

Thanks for your quick response.

I think I cannot use assign component because my internal table name is dynamic and so I do not know till runtime which fields are present in the internal table.For e.g I have a set of code attached to a number of user exits. This code is a dynamic code which assigns itself the properties of all the internal tables that are transparent in the customer function exit and executes certain function.

Now I am not aware till runtime how many tables/workareas a user-exit has and I want to read the contents of the internal table available and perform my logic.

I hope I have made it clear about my requirement and will appreciate if you can help me .

Message was edited by: Arpit Gattani

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
5,797

Hi,

Check this thread.

Kindly assign points by clicking the star on the left of reply,if it is useful.

Message was edited by: Jayanthi Jayaraman

Read only

Former Member
0 Likes
5,797

Hi,

DATA: I_DYN_TAB(10) VALUE 'I_TAB'.

data: dtab type ref to data.

field-symbols: <itab> type any table,

<wa> type any.

assign (I_DYN_TAB) to <itab>.

loop at <itab> assigning <wa>.

endloop.

Svetlin

Read only

0 Likes
5,797

Hi Svetlin,

Thanks for your response.

But I have tried this code several times but the program gives a short dump saying " You attempted to assign a field to a typed field symbol,

but the field does not have the required type. ".

I believe we cannot assign a field to field symbol of type table.

Can you suggest something else.

Thanks and Regards,

Arpit

Message was edited by: Arpit Gattani

Read only

Former Member
5,798

Ok try this >

REPORT ZTEST .

DATA : BEGIN OF I_ITAB OCCURS 0,

TEST(5),

END OF I_ITAB.

DATA: I_DYN_TAB(10) VALUE 'I_ITAB[]'.

DATA: DTAB TYPE REF TO DATA.

FIELD-SYMBOLS : <ITAB> TYPE ANY TABLE ,

<WA> TYPE ANY.

I_ITAB-TEST = 'TEST'.

APPEND I_ITAB.

ASSIGN (I_DYN_TAB) TO <ITAB>.

LOOP AT <ITAB> ASSIGNING <WA>.

WRITE <WA>.

ENDLOOP.

Read only

0 Likes
5,797

Hi Sanjay,

Thanks a lot. It worked absolutely fine.

Thanks,

Arpit