on 08-26-2005 8:37 PM
hi,
im reading internal table to fetch records based on few conditions and the records are MORE THAN ONE. is there any way i can retrieve those records with out using LOOP and ENDLOOP. your help would be appreciated.
Thanks,
ravi.
You can use a binary read to get the first record. Then read each record sequentially until you have proccessed all the records that meet your critera. You will have to have the internal table sorted so that the binary search and subsequent reads will work:
READ TABLE his_data WITH KEY
ebeln = ekpo_int-ebeln
ebelp = ekpo_int-ebelp
BINARY SEARCH.
IF sy-subrc = 0.
his_index = sy-tabix.
DO.
IF sy-subrc = 0.
IF his_data-ebeln = ekpo_int-ebeln AND
his_data-ebelp = ekpo_int-ebelp.
his_index = his_index + 1.
menge = menge + his_data-menge.
READ TABLE his_data INDEX his_index.
ELSE.
EXIT.
ENDIF.
ELSE.
EXIT.
ENDIF.
ENDDO.
ENDIF.
Rob
Message was edited by: Rob Burbank
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Rich - this is the fastest way I know of of retieving a subset of data. Assume you have an internal table with 100000 rows. They are sorted in key sequence and you just want four records. The binary search takes you directly (almost) to the record you want. You process it, read the next record and if the key is the same process it and so on. In this example, you do one binary search and three indexed reads. Very fast.
Rob
You have to be wary of the binary search though.. ie if you are doing multiple READs on the same internal table each time with a different key.. the table needs to be sorted before each READ statement.. some times you might want it sorted in a specific order for reporting purpose.. but again, as Rob pointed out, with large amounts of data BINARY SEARCH is the fastest retrieval option..
Good Luck,
Suresh Datti
Actually if you do a loop at where on a SORTED table with the where condition matching all the keys, this will have the same results:
From the online help:
Similarly to the READ optimization, the starting point for the loop is determined by a binary search with the simple conditions, which partly or even completely cover the table key. From the starting point onwards, the loop is processed only so long as these simple conditions are still met.
When people try to tune their programs, they concentrate on database hits. But in my experience, nested loops can be just as bad if not worse. A binary read followed by a number of indexed reads can do wonders. It's a bit trickier, but worth it.
There used to be an example in the performance examples of parallel cursor looping. I don't see it there in 4.6C. You would read the first record of two internal tables. (It assumed you a many to one relationship of the keys in the second table to the first.) Rather than looping through each table, you simply read the next record of each table after comparing keys and deciding which table to read. That was difficult. I got it to work a few times, but the resutls never justified the work.
Rob
If you do not want the records that don't meet the criteria then you can simply delete them with one statement and you will have only records you want or if you want to keep them, then make a duplicate internal table and do the above operation.
DELETE itab WHERE field <> 'VALUE'.
For example, if you are looping at the table with the condition
LOOP AT itab WHERE field = value.
ENDLOOP.
Then delete the records that you don't need as follows.
DELETE itab WHERE field <> 'VALUE'.
If you don't want to do this on the original itab, then do the following
itabcopy[] = itab[].
DELETE itabcopy WHERE field <> 'VALUE'.
Srinivas
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you want to read the records of an internal table are there are multiple ones per condition, you should us the LOOP statement with the WHERE extension.
LOOP at itab WHERE FIELD = 'X'.
Do something.
ENDLOOP.
Is there a reason why you don't want to use a loop?
Regards,
Rich Heilman
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ravi,
It depends on what you wnat to do..
1. To write the contents, you HAVE to open the loop.
2. To manipulate the contents, you can either open the loop or use the READ statement & then MODIFY.
3. To MOVE them to another table, you can use the APPEND LINES OF.. without opening the loop.
Good Luck,
Suresh Datti
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.