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

regarding internal table loops

Former Member
0 Likes
772

hi gurus,

i have a situation, i need to filter records by adding on the where conditon in a internal table itab1 as i go down the program .

here is a sample,

read table itab1 with key col1 = '123'

col2 = 'abc'.

while sy-subrc = 0.

append itab1 to itab2.

sort itab2 by col1 col2 col3.

read table itab1 with key col1 = '123'

col2 = 'abc'.

endwhile.

in the next step i want to use the same logic but one more variable in where conditon.. <b>col3 = 'xyz'</b>, but here, i am going in to infinite while loop selecting same record ...how do i overcome that?

when i come to next step i want to select form itab2 and now what is situation of itab1 outside the loop? is it empty , can i use it to select from itab2 or should i declare itab3 ?

plz help me...i am not a abap guy..so kindaa stuck at this simple situation....

Message was edited by:

ravi a

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
741

An alternative solution may be as given below:

Given itab1 is already declared. Also the selection from itab1 based on col1 and col2 is using fixed values like.'123' etc.


Data: itab2 like itab1,
         itab3 like itab1.

* copy itab1 into the other table
  itab2 = itab1.

* delete the irrelevant rows from itab2 based on the where clause
  delete itab2 where col1 ne '123' and col2 ne 'ABC'.

* copy itab2 into the other table
  itab3 = itab2.

* delete the irrelevant rows from itab3 based on the where clause
  delete itab3 where col1 ne '123' and col2 ne 'ABC' and col3 ne 'XYZ'.

After this, itab2 will have records that match with value in col1 and col2 as '123' and 'ABC' respectively.

itab3 will have records that match with value in col1, col2 and col3 as '123', 'ABC' and 'XYZ' respectively. Also it will have only those records which were also selected in itab2 as it was a copy of itab2.

Hope this helps.

Thanks

Sanjeev

7 REPLIES 7
Read only

Former Member
0 Likes
741
  • Below code moves data from itab to itab2 provided both have the same structure with same field names.

loop at itab where col1 = '123' and col2 = 'abc'.

move corresponding fields of itab1 to itab2.

append itab2.

clear itab2.

endloop.

Is this what you are looking for?

Thanks,

Santosh

Read only

Former Member
0 Likes
741

Hi ravi,

Could you please give some details of the situation you are handelling?

You can give contents of table or there might be other good ways to handle this.

From your explaination its purpose is not much clear.

Thanks,

ags..

Read only

Former Member
0 Likes
741

Are you trying to do something like:



DATA new_index LIKE sy-tabix.

READ TABLE itab1 WITH KEY col1 = '123'.
col2 = 'abc'.
new_index = sy-tabix.
WHILE sy-subrc = 0.
  APPEND itab1 TO itab2.
  new_index = new_index + 1.
  READ TABLE itab1 INDEX new_index.
  col2 = 'abc'.
ENDWHILE.
SORT itab2 BY col1 col2 col3.

Rob

Read only

Former Member
0 Likes
742

An alternative solution may be as given below:

Given itab1 is already declared. Also the selection from itab1 based on col1 and col2 is using fixed values like.'123' etc.


Data: itab2 like itab1,
         itab3 like itab1.

* copy itab1 into the other table
  itab2 = itab1.

* delete the irrelevant rows from itab2 based on the where clause
  delete itab2 where col1 ne '123' and col2 ne 'ABC'.

* copy itab2 into the other table
  itab3 = itab2.

* delete the irrelevant rows from itab3 based on the where clause
  delete itab3 where col1 ne '123' and col2 ne 'ABC' and col3 ne 'XYZ'.

After this, itab2 will have records that match with value in col1 and col2 as '123' and 'ABC' respectively.

itab3 will have records that match with value in col1, col2 and col3 as '123', 'ABC' and 'XYZ' respectively. Also it will have only those records which were also selected in itab2 as it was a copy of itab2.

Hope this helps.

Thanks

Sanjeev

Read only

Former Member
0 Likes
741

Hi,

Its better to have one more internal table with the final result you want:

Try this:

loop at itab1 where col1 = '123' and col2 = 'abc'.

loop at itab2 where col1 = itab1-col1

and col2 = itab1-col2

and col3 = 'xyz'.

move-corresponding itab1 to itab3.

move-corresponding itab2 to itab3.

append itab3.

endloop.

endloop.

Regards

Subramanian

Read only

0 Likes
741

Thanks a lot guys for all the helpful and fast answers.

Read only

Former Member
0 Likes
741

Hi,

the obvious answer is it goes into infinite loop. why? the logic u wrote for the read table id always true. and the read table written inside the while condition will not effect the SY-SUBRC for the while condition. what is the logic u actually want to write.

regards,

Aravind