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 table

Former Member
0 Likes
721

Dear SDN Members,

Please explain me the diff between read table & loop at itab.

Diff between read table with index & with key.

Thanx in Advance.

Regards,

Johnn.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
670

Hello,

READ TABLE is used specifically if you want to read a particular row with key element. This statement can also be used inside loop statement.

Case 1:

=======

Consider there are two internal tables ITAB and ITAB2. For every entry in ITAB3 ( depending up on key field F1) assume that there are many entries in ITAB. So in this case if you want to read ITAB2 with respect to ITAB then,

LOOP AT ITAB into WA.

READ TABLE ITAB2 into WA WITH KEY F1 = WA-F1.

If sy-subrc EQ 0.

endif.

ENDLOOP.

Case 2:

=======

In some cases you may wish to read a particular row in the internal table. Assume that you want to read 9th row,

READ TABLE ITAB into WA index 3.

In the above statement no need to mention the key because you directly want the 9th row.

Case 3:

=======

In some cases depending up on the internal table you may have to read,

LOOP AT ITAB into WA.

IF WA-F1 EQ 'A'.

READ TABLE ITAB2 into WA index sy-tabix

with key f1 = WA-F1.

If sy-subrc EQ 0.

endif.

ENDLOOP.

LIke these it all depends on how u do your logic.

Regs,

Venkat

7 REPLIES 7
Read only

Former Member
0 Likes
670

hello,

let me explain.

both <b>loop</b> and <b>read</b> are used to access internal table records from body to header.

<b>loop..endloop</b> gets the records one by one.

read.. statement gets only one record from internal table.

in read option,

<b>READ TABLE <ITAB> INDEX <I>.</b>

here u r trying to read row number I.

<b>READ TABLE <ITAB> WITH KEY <x1> = <x2>.</b>

here u r accessing internal table record based on a condition. in the condition only <b>EQUALITY</b> can be checked.

reward if useful....

Read only

Former Member
0 Likes
670

<b>LOOP AT</b> sets SY-TABIX to the index of the current line at the beginning of each loop lass. At the end of the loop, SY-TABIX is reset to the value that it had before entering the loop. It is set to 0 if the table has the type HASHED TABLE.

<b>READ TABLE</b> sets SY-TABIX to the index of the table line read. If you use a binary search, and the system does not find a line, SY-TABIX contains the total number of lines, or one more than the total number of lines. SY-INDEX is undefined if a linear search fails to return an entry.

Please note you have to sort the table before using READ with binary search ( This improves performance a lot)

regards

vivek

reward points for all the useful answers

Read only

Former Member
0 Likes
670

hi,

READ TABLE: reads the table where condition satisfies and return the single record.

read table at index 1 or 2 ( sy-tabix or sy-index) or with key like matnr = '000000000000023455'.

returns the record from table where matnr.

Loop at itab.

it starts the loop from the first record to last record in itab where the condition satisfies or without condition too.

loop at itab.

UR STATEMENTS.

endloop.

or

loop at itab where matnr = '000000000000002345'.

UR STATEMENTS.

endloop.

Regards

Read only

Former Member
0 Likes
670

READ TABLE :

to read ONLY ONE record from the internal table we use this.

option 1:

you can give WHERE clause to fetch the record

READ TABLE ITAB WITH KEY F1 = V1

F2 = V2

*--and so on.

you will get only 1 record into ITAB (header of that internal table).

OPTION 2:

read table itab INDEX 1.

read the first record. you can give any index here.

like

READ TABLE ITAB INDEX V_TABIX.

Here V_TABIX is of type SY-TABIX.

LOOP AT and ENDLOOP.

to process all the records of the internal table we use this.

LOOP AT ITAB

*--This will loops thru all the records of the internal table ITAB.

ENDLOOP.

LOOP AT ITAB WHERE F1 = V1

F2 = V2.

*--records comes in which satisfying the above where clause.

ENDLOOP.

These many ways we can use this statements.

Regards

Srikanth

Message was edited by: Srikanth Kidambi

Read only

Former Member
0 Likes
670

Read Table is used to get one record that matches a key or index reference.

LOOP at table is used to process all or multiple records within the int table.

Read only

Former Member
0 Likes
670

Hi Johnn,

loop at table gets the each record into the header depends on the no. of records in the table.

in this case

sy-tabix = current index of the table.

read table will give a single record depend on the condition u are going to give.

if sy-subrc = 0.

means u got the record with the satisfied condition.

sy-tabix = current index of the table.

u can read table with index or usign key values.

Read only

Former Member
0 Likes
671

Hello,

READ TABLE is used specifically if you want to read a particular row with key element. This statement can also be used inside loop statement.

Case 1:

=======

Consider there are two internal tables ITAB and ITAB2. For every entry in ITAB3 ( depending up on key field F1) assume that there are many entries in ITAB. So in this case if you want to read ITAB2 with respect to ITAB then,

LOOP AT ITAB into WA.

READ TABLE ITAB2 into WA WITH KEY F1 = WA-F1.

If sy-subrc EQ 0.

endif.

ENDLOOP.

Case 2:

=======

In some cases you may wish to read a particular row in the internal table. Assume that you want to read 9th row,

READ TABLE ITAB into WA index 3.

In the above statement no need to mention the key because you directly want the 9th row.

Case 3:

=======

In some cases depending up on the internal table you may have to read,

LOOP AT ITAB into WA.

IF WA-F1 EQ 'A'.

READ TABLE ITAB2 into WA index sy-tabix

with key f1 = WA-F1.

If sy-subrc EQ 0.

endif.

ENDLOOP.

LIke these it all depends on how u do your logic.

Regs,

Venkat