Application Development 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: 

Difference between read line and loops used for two or more tables

Karan_Chopra_
Active Participant
0 Kudos

please tell me the difference between:

displaying table entries using loop in base table then loop in next table :

LOOP AT IKNA1.

LOOP AT IKNVP WHERE KUNNR = IKNA1-KUNNR.

Code....

endloop.

endloop.

and

displaying table entries using loop in base table then read in next table :

LOOP AT IKNA1.

READ TABLE IKNVP WITH KEY KUNNR = IKNA1-KUNNR.

Code.....

Endloop.

1 ACCEPTED SOLUTION

Karan_Chopra_
Active Participant
0 Kudos

guys prob is still not answered

i am gettin multiple entries in case of read NOT loop and these are not different entries but they are previous repeated entry may be wen we get an entry in main table which is not in secondary table then entries in second tables shows some <b>repeated</b> entries

15 REPLIES 15

Former Member
0 Kudos

LOOP AT IKNA1.

LOOP AT IKNVP WHERE KUNNR = IKNA1-KUNNR.

Code....

endloop.

endloop.

This can get multiple records from IKNVP,if there are multiple records satisfying where condition.

LOOP AT IKNA1.

READ TABLE IKNVP WITH KEY KUNNR = IKNA1-KUNNR.

Code.....

Endloop.

This can get only single record.

If there are multiple records with the same KEY then the first record which satisfied the WITH KEY condition will be fetched.

Both return sy-subrc = 4 if no records are found with specified condition.

Former Member
0 Kudos

Both are same and performance of point of view we use read table in Loop .

Loop means - we can read multiple data from internal table..

Read table meand - we can read one record from internal table.

Karan_Chopra_
Active Participant
0 Kudos

i am gettin redundant entries in case of READ not in loop

0 Kudos

Redundant in what sense?

READ statement will fetch the matching KUNNR record into header line of IKNVP.

Former Member
0 Kudos

LOOP AT IKNA1.

LOOP AT IKNVP WHERE KUNNR = IKNA1-KUNNR.

Code....

endloop.

endloop.

This code fetches the multiple entires from the table IKNVP which statisfy the condtions .

LOOP AT IKNA1.

READ TABLE IKNVP WITH KEY KUNNR = IKNA1-KUNNR.

Code.....

Endloop.

This code fetches the single entry from the table IKNVP which statisfy the condtions .

This second method is more efficient compare to first one .

Former Member
0 Kudos

Hi

The second method is advisable.Because insted of looking line-by-line in the second internal table, According to the key you have give in The READ TABLE it will do a search and this will save a lot of time..Put a break-point here and goto debugg mode you can see what is happening....Looping through the internal table is always will take a longer time that that of READ table,,...

Reward ALL helpfull Answers........

Former Member
0 Kudos

hi karan,

in first case we use move corresponding for moving whole data to the final output table but in second case data move by key and line by line.

in first case we use sy-tabix for tracking the index. while in second case we use key as keyword.

thanks and regards

vijay dwivedi

rewards if answer is helpful to you

former_member196280
Active Contributor
0 Kudos

LOOP AT IKNA1.

LOOP AT IKNVP WHERE KUNNR = IKNA1-KUNNR.

Code....

endloop.

endloop.

<b>above will fetches multiple records from IKNVP</b>

LOOP AT IKNA1.

READ TABLE IKNVP WITH KEY KUNNR = IKNA1-KUNNR.

Code.....

Endloop.

<b>It fetches single record from IKNVP

Depending on your requirement you should use accordingly.

If you want to fetch one record from second internal table then read statment will me useful. (performance wise)</b>

Reward to all useful answers.

Regards,

SaiRam

Karan_Chopra_
Active Participant
0 Kudos

doono sy-tabix for second table becomes 0 then again to original value

i mean wen entry of first table is not found in second the previous entry of second tables is displayed again

0 Kudos

Use sy-subrc check after READ statement before writing any other logic.

Loop..

READ..

if sy-subrc = 0.

..code..

endif.

Endloop.

The reason why the previous record is displayed is because if it doesnt find any record in the other table, it will not clear the header line and hence it will contain values of the last record.

Former Member
0 Kudos

1)

LOOP AT IKNA1.

LOOP AT IKNVP WHERE KUNNR = IKNA1-KUNNR.

Code....

endloop.

endloop.

here, u are using a loop inside a loop.

in the second loop, every entry of IKNVP is selected where KUNNR = IKNA1-KUNNR.

2)

LOOP AT IKNA1.

READ TABLE IKNVP WITH KEY KUNNR = IKNA1-KUNNR.

Code.....

Endloop.

here, there is only one loop.

the READ statement selects the first entry of IKNVP where KUNNR = IKNA1-KUNNR.

for example, the IKNVP table may contain more than more entries of the same value of KUNNR, but only the first one is selected.

please let me know if ur doubt still exists.

thanks,

viji.

Former Member
0 Kudos

In the first case( loop and loop), For 1 IKNA1 record, you are trying to fetch all those records in IKNVP which satisfy the condition IKNA1-Kunnr - IKNVP-kunnr.

In the second case , You are trying to read the first record from IKNVP that satisfies the condition IKNA1-Kunnr - IKNVP-kunnr for each reecord of IKNA1.

For example, lets say you are trying to build an internal table TEST in the CODE section of the loop endloop.

let the records of IKNA1 be

KUNNR(field name)

1

2

Let the records of IKNVP be

Example field(fieldname) KUNNR

A 1

B 1

C 2

Then if you consider the first case ie (Loop-loop-endloop-endloop),then the records in TEST(let it contain 2 field example field and kunnr) would be

Kunnr Example field

1 A

1 B

2 C

if you had used READ inside LOOP then TEST table will have

Kunnr Example field

1 A

2 C

Hope this helps You.

Karan_Chopra_
Active Participant
0 Kudos

guys prob is still not answered

i am gettin multiple entries in case of read NOT loop and these are not different entries but they are previous repeated entry may be wen we get an entry in main table which is not in secondary table then entries in second tables shows some <b>repeated</b> entries

0 Kudos

Hey did you try as per my earlier reply?

Can you please paste your code here?

Karan_Chopra_
Active Participant
0 Kudos

thanks a lot Darshil Shah

u answered my prob thanx a lot dude i got rite entries now

thanx to alllll