‎2014 Feb 17 2:58 PM
Hi all,
this is my situation:
The "where" clause does not work because I have only the data reference or the field symbol of the original table.
A loop with an if inside is not feasible for performance reasons (the sorted table would become useless and it is really large).
How can I obtain this loop?
Thank you
‎2014 Feb 17 9:24 PM
Hello Alberto,
I think you can achieve this using RTTI & dynamic WHERE condition for LOOP. If your ABAP kernel does not support dynamic token for LOOP...WHERE, then i don't see this solution working.
In case you are not on ABAP 740, you'll have to change the code accordingly
BR,
Suhas
‎2014 Feb 17 3:03 PM
one way, not most elegant but I have used approach before as it is only few lines of simple code and I just get the records I want to loop at
create a table copy, but as standard table...
then move all the data over...
data lt_standard type standard table of xyz.
lt_standard[] = lt_sorted[].
delete lt_standard where key ne 'VALUE1'.
loop at lt_standard assigning <fs>.
...do you work, updates, etc....
‎2014 Feb 17 3:13 PM
Hi Steve,
thank you for your answer.
However this is not a solution for me because:
‎2014 Feb 17 4:59 PM
It requires 2 times the space of the original table
? your copying the records from sorted to standard table. You will then remove all the records you dont want, and then wrok with the subset remaining!!!
It is a sequential slow approach (does not exploit the sorted table)
think again - you are just looping at the records you interested in using my way for records that equal VALUE1. Sequential is now irrelevant. I deleted all the records i did not want to loop through.
However, with your method - you are looping through a sorted table - this is still sequential - its only useful when you READ the table with a table key syntax doing it your way.
I am not able to know what is "xyz" (in your example)
come on, dont be silly - xyz is just a dummy reference name - it could be VBAK or whatever.
I will leave it here as I think you have not grasped what i was suggesting. I have used this technique a handful of times and it is fast. There are in fact several ways you can resolve your problem - but I just gave you a fast simple one.
‎2014 Feb 17 5:09 PM
Your method is really easy to understand. I think OP is dealing with dynamic table, and this is why he can't know in advance the value of "xyz". For the where clause to work, line type must be statically defined.
‎2014 Feb 17 5:11 PM
Hi Steve,
thank you again.
I will try to explain better my previous post:
1) It requires twice the original space because you are copying the original table. Suppose that the original table is 500MB, you will generate another 500MB of data and then remove some data. Generating 500MB of extra data and then removing a part of it is both time and memory consuming.
2) Sequential approach: yes, I would like to search records with the key using binary search, since I have a sorted table. Binary search is log(n) with n the number of records.
3) Of course I was not searching for "xyz", what I said is that I have only a reference to the original table as a "data ref". How can I get the type of the working area without using a dynamic allocation?
At the moment I solved the problem using a work around:
- I use READ... WITH KEY key='value1'
- I save the index returned by this reading operation;
- This index contains the first matching record.
- Inside a loop I check if the record with index+1 has the same key of the previous records.
- I continue to read the next record until the key is different.
This is possibile because I have a sorted table and I am filtering with the primary key.
‎2014 Feb 17 5:19 PM
It really is like the parallel cursor method. In case you are not already using the FROM index addition in LOOP statement, please add it. And exit the loop on first mismatch of key.
‎2014 Feb 17 5:26 PM
Hi Manish,
thank you.
So can I use the LOOP statement with FROM %index% clause and the loop will start from %index"?
At the moment I'm using a DO loop and checking the sy values. The LOOP statement would be better.
‎2014 Feb 17 5:32 PM
Yes you can. The INDEX addition does not work for hashed and any table types, but will work for sorted type.
‎2014 Feb 17 9:24 PM
Hello Alberto,
I think you can achieve this using RTTI & dynamic WHERE condition for LOOP. If your ABAP kernel does not support dynamic token for LOOP...WHERE, then i don't see this solution working.
In case you are not on ABAP 740, you'll have to change the code accordingly
BR,
Suhas
‎2014 Feb 18 3:45 AM
‎2014 Feb 18 8:38 AM
It is less showoff & more learning I need to get used to the 740 syntax & i'm luvin' it, isn't it cool?
Maybe i'll write a converter to make such statement backward compatible using search/replace
Be my guest
‎2014 Feb 18 9:45 AM
Thank you Suhas,
I will try your code as soon as possible.
Inline data declaration looks great! However we have to develop our system on 731. We are migrating everything to 740 but for the moment we are on 731.
I will try your code, I understood the RTTI approach, however your code looks really close to my first attemp with a standard LOOP AT ... ASSIGNING ... WHERE. I can not understand why a dynamic (cond) works while my code did not work.
Also please could you expalin me how to write this line:
LOOP AT tab_ref->* ASSIGNING FIELD-SYMBOL(<wa>) WHERE (cond).
on 731? I'm not sure about the (cond) part.
Thank you
‎2014 Feb 18 10:17 AM
Hello Alberto,
I hope that you got the idea about what the code is doing, although it is written in 740 syntax.
You'll have to dereference the data reference before looping on it. This means another helper variable
FIELD-SYMBOLS <tab> TYPE SORTED TABLE.
ASSIGN tab_ref->* TO <tab>.
LOOP AT <tab> ASSIGNING <wa> WHERE (dyn_cond).
ENDLOOP.
Good thing is that you are on ABAP 731 & dynamic WHERE is supported
BR,
Suhas
‎2014 Feb 18 10:33 AM
Hi Sushas,
thank you, yes I got the idea.
My question is: will the dynamic WHERE clause search in the sorted table usin binary search or will it use a linear iteration?
Thank you
EDIT: this code is also working:
DATA l_cond TYPE string VALUE 'FIELD1 = ''KEYVALUE1'''.
LOOP AT <lt_myTable> INTO <ls_table> WHERE (l_cond).
....
ENDLOOP.
But I'm not sure that it performs binary search on the sorted table using the key.
‎2014 Feb 18 10:44 AM
I also found that this code is working:
DATA l_cond TYPE string VALUE 'FIELD1 = ''KEYVALUE1'''.
LOOP AT <lt_myTab> INTO <ls_tabRow> WHERE (l_cond).
...
ENDLOOP.
But I'm not sure if it is using the binary search on the where statement.
‎2014 Feb 18 12:00 PM
Alberto Bedin wrote:
My question is: will the dynamic WHERE clause search in the sorted table usin binary search or will it use a linear iteration?
I had the same question yesterday. I checked the ABAP documentation & i don't see any thing which states that the LOOP...WHERE will not be optimized for dynamic condition I'm not sure if SAT/SE30 can be helpful
DATA l_cond TYPE string VALUE 'FIELD1 = ''KEYVALUE1'''You can use this is you know that FIELD1 will always be the keyfield. In that case you do not need RTTI
BR,
Suhas
‎2014 Feb 18 1:31 PM
Thank you. I think that I will check the dynamic WHERE performances on sorted tables and I will compare them with the READ statement followed by an index loop.
‎2014 Feb 19 9:04 AM
I tested a TABLE of 10,000,000 records with 1000 matching keys:
The third approach is the fastest one (10-100) times faster. Incredibly the second approach is the slowest one (2 times slower then the first one). Maybe ABAP is performing multiple not optimized binary searches.
This result remain the same with any number of non-unique matching keys.
On the other side the second approach is faster on small tables (10000 elements).
Hope this could help.
‎2015 Oct 07 10:58 AM