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 inside a loop .

Former Member
0 Likes
1,859

I know that the instruction read table reads only one row but if I put it inside a loop it will read multiple rows right ?

refer to my code below :


select vbpa~parvw kna1~kunnr vbap~netwr from vbap

    inner join vbpa on vbap~vbeln = vbpa~vbeln

    inner join kna1 on vbpa~kunnr = kna1~kunnr

    into corresponding fields of table gt_local

    where ( vbpa~parvw = 'AG' ) and

    kna1~kunnr in s_kunnr .



  loop at gt_local into gs_local .

    collect gs_local into gt_local2 .

  endloop .



  select  *

  from vbpa as a inner join vbap as b on ( a~vbeln = b~vbeln )

     inner join vbfa as c on ( c~vbelv = b~vbeln  )

    inner join lips as d on ( d~vbeln = c~vbeln and d~posnr = c~posnn  )

    into corresponding fields of table gt_local3

  where ( a~parvw = 'WE'  )

  and c~vbtyp_n = 'J' or c~vbtyp_n = 'R' .



  clear gt_local .

  clear gs_local .

  loop at gt_local3 into gs_local3 where vbtyp_n = 'J'.

    collect gs_local3 into gt_local.

  endloop.

*

  loop at gt_local2 into gs_local2  .

    move sy-tabix to lv_index .

    read table gt_local into gs_local with key kunnr = gs_local2-kunnr  .

    gs_local2-lfimg = gs_local-lfimg.

    modify  gt_local2 from gs_local2 index lv_index .

  endloop.

*

  clear gt_local .

  clear gs_local .



    loop at gt_local3 into gs_local3 where vbtyp_n ='R'.

    collect gs_local3 into gt_local.

  endloop.





  loop at gt_local2 into gs_local2 .

    move sy-tabix to idx .

    read table gt_local into gs_local with key kunnr = gs_local2-kunnr  .

    gs_local2-rfmng = gs_local-rfmng.

    modify  gt_local2 from gs_local2 index idx .

  endloop.

1 ACCEPTED SOLUTION
Read only

rameez_khan
Active Participant
1,584

Read statement used inside or outside the loop will give you only 1 record. Read statement will never return multiple records back.

If you want to access multiple records then go for loop.

4 REPLIES 4
Read only

SimoneMilesi
Active Contributor
1,584

What is the reason of this question? ABAP has its statement to read multiple rows from internal table.

And the answer to your question is "maybe", because if the read condition doesn't change, you can have a 100000000 cycles loop and still read the same record.

Read only

Former Member
0 Likes
1,584

Edited my question.

Read only

SimoneMilesi
Active Contributor
0 Likes
1,584

Apart the naming convention totally scarying from my point of view (G... for global and name local, local2, local3) which makes really hard for me trying to understand what are you trying to achieve, the first question i have is: did you set a breakpoint on the read and debug it?

Read only

rameez_khan
Active Participant
1,585

Read statement used inside or outside the loop will give you only 1 record. Read statement will never return multiple records back.

If you want to access multiple records then go for loop.