cancel
Showing results for 
Search instead for 
Did you mean: 

reading multiple records from internal table with out loop

Former Member
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Rob, do you think that this is faster than doing a regular LOOP at <itab> WHERE? I know that the initial read will be, but what about the rest of it?

LOOP AT his_data WHERE  ebeln = ekpo_int-ebeln
                   and  ebelp = ekpo_int-ebelp.

* Do whatever
ENDLOOP.

Regards,

Rich Heilman

suresh_datti
Active Contributor
0 Kudos

I think Rob was trying to meet Ravi's requirement ie avoid opening the 'LOOP'.

Suresh

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Suresh, so you are saying that you don't think it would be faster?

Your thoughts/opinions, please?

Regards,

Rich Heilman

suresh_datti
Active Contributor
0 Kudos

it depends on what/how much is inside..I would prefer your way though ie loop at itab where..endloop

Suresh

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

I agree, I'm thinking that if we are talking about a large amount of records, maybe 100,000+, that Rob's solution may have an advantage.

Anyone else have any thoughts?

Regards,

Rich Heilman

Former Member
0 Kudos

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

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Cool Rob, I need to keep this in mind next time I run across something like this. Thanks for the info.

Regards,

Rich Heilman

Former Member
0 Kudos

I think that read table .... binary search is faster than loop/endloop, but we can note the real advantage only if there are a large amount of records.

To use a sorted table have better performance on reading

suresh_datti
Active Contributor
0 Kudos

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

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

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.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

I was thinking the same thing Thomas, but didn't find the documentation to back me up till right now. Thanks for the info.

Regards,

Rich Heilman

Former Member
0 Kudos

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

Former Member
0 Kudos

Depend on what sorted table is for you.

READ TABLE .... BINARY SEARCH is used to read standard table that are sorted.

If SORTED is table type, I agree with you: there isn't a different between read table and loop/endloop.

Former Member
0 Kudos

worked for me.

Thanks.

Former Member
0 Kudos

Hi,

Get Count of internal table data!!

DATA: lv_count type i,

           lv_count1 type i .

Describe IT_DATA Line lv_count.

lv_count1  = 1.


Do Lv_count Times.

  

read it_data into wa_data index  lv_count1.

   lv_count1 = lv_count1 + 1.

enddo.

Thanks,

Anbusundaram

Answers (3)

Answers (3)

Former Member
0 Kudos

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

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

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

suresh_datti
Active Contributor
0 Kudos

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