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

READ Internal Table

Former Member
0 Likes
1,058

Hi

I face many situations where i have to issue a READ statement on an Internal Table where i have to use "<" or ">" operators. READ just allows to use keys with all "=" operators.But at the same time i need to get just one record. I know that READ is to get only one record.

Any suggestion ?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,038

Use

Loop at Itab where <CONDITION>

Exit.

ENDLOOP.

The first entry that passes the condition will be available in Itab.

10 REPLIES 10
Read only

Former Member
0 Likes
1,039

Use

Loop at Itab where <CONDITION>

Exit.

ENDLOOP.

The first entry that passes the condition will be available in Itab.

Read only

Former Member
0 Likes
1,038

Previously i was using a Loop at Where <Condition>.Due to performance reasons i removed it. But anyway, i need to issue this READ within a Loop and get the correct record, before the next loop pass. I hope i am clear.

Read only

Former Member
0 Likes
1,038

Hi,

I think you can use COMPARING option with READ Statement

READ TABLE <itab> <key> INTO <wa> [COMPARING <f1> <f 2> .]

If you use the COMPARING addition, the specified table fields <f i > of the structured line type are compared with the corresponding fields of the work area before being transported.

Also check the Sample code,

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL1 = 2. LINE-COL2 = 3.

READ TABLE ITAB FROM LINE INTO LINE COMPARING COL2.

WRITE: 'SY-SUBRC =', SY-SUBRC.

SKIP.

WRITE: / LINE-COL1, LINE-COL2.

The output is:

SY-SUBRC = 2

2 4

The program fills a hashed table with a list of square numbers. The work area LINE, which is compatible with the line type, is filled with the numbers 2 and 3. The READ statement reads the line of the table in which the key field COL1 has the same value as in the work area and copies it into the work area. SY-SUBRC is set to 2, because the contents of field COL2 were different.

Hope it helps u.

Thanks&Regards,

Ruthra.R

Read only

Former Member
0 Likes
1,038

With Read it is not possible to use "<" or ">" .

Are you using "<" ">" on amount fields .

You can use index table ( instead of std table ) for enhanced performance of read or loop. But all depends on what your exact requirement is . Can you be more specific.

Cheers

Read only

0 Likes
1,038

The requirement is as follows

I have got internal Table A with Date and Time amount.

1-apr-05 1:00 50

1-apr-05 4:00 60

2-apr-05 6:00 40

I have got another internal table B with date & Time & Amount. Amount is yet to be filled.

1-apr-05 2:00

1-apr-05 5:00

Now i loop over internal table B to find the amount at <b>THAT</b> time, comparing it with Internal table A values.

So my result is for Itab B,

1-apr-05 2:00 50

1-apr-05 5:00 60

I am basically looping over ITAB B and issuing a READ

over ITAB A to get the correct amount.

Read only

0 Likes
1,038

The exampe which you mentioned for table A and Table B will not give you desire result which you mentioned here for Table B, BUT yes if your Table A will have values -

1-apr-05 1:00 50

1-apr-05 4:00 60

2-apr-05 6:00 40

and Table B will have values -

1-apr-05 1:00

1-apr-05 2:00

Then you can get values for Table B as you mentioned like -

1-apr-05 2:00 50

1-apr-05 5:00 60

Here is the logic to get such result.

data g_tabix like sy-tabix.

Sort itab_a by date time.

Sort itab_b by date time.

loop at itab_b.

g_tabix = sy-tabix.

clear itab_a.

read table itab_a with key date = itab_b-date

time = itab_b-time

binary search.

if sy-subrc = 0.

itab_b-amount - itab_a-amount.

modify itab_b index g_tabix.

endif.

endloop.

Hope this logic helps.

Read only

Former Member
0 Likes
1,038

I have achieved this req. already using LOOP at itab <WHERE> Condition. But i want to do the same using READ statement as i suppose this is more efficient.

Read only

Former Member
0 Likes
1,038

Ok if you have exactly pasted your problem then try this , it will be much more fast . Also see F1 help on read and sy-subrc value , you will get the clue here.

<i>REPORT ZTEST.

DATA : BEGIN OF WA_TAB,

DATE LIKE SY-DATUM,

TIME LIKE SY-UZEIT,

AMOUNT TYPE P DECIMALS 2,

END OF WA_TAB.

DATA ITAB1 LIKE SORTED TABLE OF WA_TAB WITH UNIQUE DEFAULT KEY WITH HEADER LINE..

DATA ITAB2 LIKE SORTED TABLE OF WA_TAB WITH UNIQUE DEFAULT KEY WITH HEADER LINE..

DATA COUNT TYPE I.

ITAB1-DATE = '20050919'.

ITAB1-TIME = '010000'.

ITAB1-AMOUNT = 50.

APPEND ITAB1.

ITAB1-DATE = '20050919'.

ITAB1-TIME = '020000'.

ITAB1-AMOUNT = 60.

APPEND ITAB1.

ITAB1-DATE = '20050919'.

ITAB1-TIME = '030000'.

ITAB1-AMOUNT = 70.

APPEND ITAB1.

ITAB2-DATE = '20050918'.

ITAB2-TIME = '021000'.

APPEND ITAB2.

LOOP AT ITAB2 .

READ TABLE ITAB1 WITH KEY DATE = ITAB2-DATE

TIME = ITAB2-TIME.

IF SY-SUBRC = 4 OR SY-SUBRC = 8 .

COUNT = SY-TABIX - 1 .

ENDIF.

IF COUNT GE 1.

READ TABLE ITAB1 INDEX COUNT .

MOVE ITAB1-AMOUNT TO ITAB2-AMOUNT.

MODIFY ITAB2.

WRTIE ITAB2-AMOUNT.

ENDIF.

ENDLOOP.</i>

Try changing the date and time values in ITAB2 and see the desired result.

Cheers

Read only

Former Member
0 Likes
1,038

hi, you can use RANGE in LOOP. code like following:


DATA: R_BUKRS  TYPE RANGE OF T001-BUKRS.
DATA: LR_BUKRS LIKE LINE OF R_BUKRS.

LR_BUKRS-SIGN = 'I'.
LR_BUKRS-OPTION = 'EQ'.
LR_BUKRS-LOW = '1000'.
APPEND LR_BUKRS TO R_BUKRS.

LR_BUKRS-SIGN = 'I'.
LR_BUKRS-OPTION = 'EQ'.
LR_BUKRS-LOW = '0100'.
APPEND LR_BUKRS TO R_BUKRS.

LOOP AT IT_BUKRS INTO LC_BUKRS WHERE BUKRS IN R_BUKRS.
* will go into here when LR_BUKRS 1000 or 0100
ENDLOOP.

I think the effect is you need.

Hope it will be helpful

thanks

Read only

Former Member
0 Likes
1,038

I posted someting about this acouple of weeks ago. Try something like:


SORT: bkpf_int BY bukrs belnr gjahr,
        bseg_int BY bukrs belnr gjahr.
  LOOP AT bkpf_int.
    READ TABLE bseg_int WITH KEY
      bukrs = bkpf_int-bukrs
      belnr = bkpf_int-belnr
      gjahr = bkpf_int-gjahr
      BINARY SEARCH.
    bkpf_reads = bkpf_reads + 1.
    bseg_index = sy-tabix.
    WHILE sy-subrc = 0.
      bseg_index = bseg_index + 1.
      bseg_reads = bseg_reads + 1.
      READ TABLE bseg_int INDEX bseg_index.
      IF bseg_int-bukrs <> bkpf_int-bukrs OR
         bseg_int-belnr <> bkpf_int-belnr OR
         bseg_int-gjahr <> bkpf_int-gjahr.
        sy-subrc = 99.
      ELSE.
      ENDIF.
    ENDWHILE.
  ENDLOOP

A binary search followed by indexed reads is one of the fastet ways to retrieve records from an internal table.

Rob