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

Loop at Internal Table

Former Member
0 Likes
3,431

HI Gurus,

can u provide logic for me...

Loop At itab1.

loop at stab where fld = itab1-fld and fld2 in s_fld2.

loop at ttab where fld = itab1-fld and fld3 in s_fld3.

loop at ptab where fld = itab1-fld.

move itab1 to otab.

move stab to otab.

move ttab to otab.

move ptab to otab.

append otab.

endloop.

endloop.

endloop.

endloop.

the problem is either stab or ttab may contain data or not ..filled in runtime.

if stab dont have any records then it exiting the loop and not checking for ttab.

i want logic that even stab do not have records that it goes further in ttab ad ptab.

we can place loop at stab with read table .. stab contains only 1 record with particular itab1-fld but it shud also check fld2 in s_fld2 (select options)... is it possible to place IN condition in read statement?

hope u understand the problem..

let me know if u need some more information.

<b>i cant change the nested loops... those data all r coming from BAPI and must be nested to allot the records properly.</b>

Regards

SAB

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,029

Hi

you can check the table before entering itin loop ( check the internal table is empty ).

If the internal table is empty append it with one dumy record to push it to next loop. and set flag. if you are appending dummy data in internal table basically you are pushing the program to go to next iteration of loop . at last you are moving to final internal table in that case check the flag again to ensure that it is dummy data or real data accordingly you can append the record..

*Please mark useful answers

7 REPLIES 7
Read only

Former Member
0 Likes
2,029

Hi,

Use read table instead of nested loop.

Message was edited by:

senthil kumar

Read only

Former Member
0 Likes
2,030

Hi

you can check the table before entering itin loop ( check the internal table is empty ).

If the internal table is empty append it with one dumy record to push it to next loop. and set flag. if you are appending dummy data in internal table basically you are pushing the program to go to next iteration of loop . at last you are moving to final internal table in that case check the flag again to ensure that it is dummy data or real data accordingly you can append the record..

*Please mark useful answers

Read only

0 Likes
2,029

I am not sure how the nested loops from bapi come from...

you can do one thing..

Loop At itab1.

loop at stab. " where fld = itab1-fld and fld2 in s_fld2.

if fld = itab1-fld and fld2 in s_fld2. " check with if condition

else.

endif.

loop at ttab where fld = itab1-fld and fld3 in s_fld3.

loop at ptab where fld = itab1-fld.

move itab1 to otab.

move stab to otab.

move ttab to otab.

move ptab to otab.

append otab.

endif.

endloop.

endloop.

endloop.

endloop.

like this instead of where clause do with if statements. i don't know if this is feasible to you.

Read only

0 Likes
2,029

Thanx Guys...

I solved it in other way... using same loop and read statement and in the end delete the otab which is not in respective s_fld.

Vinni: I mean those data r coming from BAPI means not the nested loops r coming from BAPI. From BAPI i get 6 internal tables from header level to item level in Specification Management. I need to sort all internal tables into 1 record from each table and each table has only 1 link between them.. so nested loops r must for me.

Pradeep: Read statement is only useful when using parameters cos of single entry.. when multiple entry need to loop all substances.

Regards

SAB

Read only

Former Member
0 Likes
2,029

Hi,

Try this..


DATA: v_flag_stab.
DATA: v_flag_ttab.
DATA: v_flag_ptab.

* remove the records that are not in the select-options.
DELETE stab WHERE NOT fld2 IN S_FLD2.
DELETE ttab WHERE NOT fld2 IN S_FLD2.
DELETE ptab WHERE NOT fld2 IN S_FLD2.


loop at itab1.

* Have a do loop.
  DO.

* Clear the work area.
   CLEAR: otab,
               v_flag_stab,
               v_flag_ttab,
               v_flag_ptab.                

* read the table stab.
    READ TABLE STAB WITH KEY FLD = ITAB1-FLD.
    IF SY-SUBRC = 0.
* Move the values to the output tab..
       MOVE stab TO otab.
* Delete that record.
       DELETE STAB INDEX SY-TABIX.
    ELSE.
* Set the flag.
       v_flag_stab = 'X'.
    ENDIF.

* read the table ttab.
    READ TABLE TTAB WITH KEY FLD = ITAB1-FLD.
    IF SY-SUBRC = 0.
* Move the values to the output tab..
       MOVE ttab TO otab.
* Delete that record.
       DELETE TTAB INDEX SY-TABIX.
    ELSE.
* Set the flag.
       v_flag_ttab = 'X'.
    ENDIF.

* read the table ptab.
    READ TABLE PTAB WITH KEY FLD = ITAB1-FLD.
    IF SY-SUBRC = 0.
* Move the values to the output tab..
       MOVE ptab TO otab.
* Delete that record.
       DELETE PTAB INDEX SY-TABIX.
    ELSE.
* Set the flag.
       v_flag_ptab = 'X'.
    ENDIF.

* IF all the flags are set...Then exit..
    IF v_flag_stab = 'X' AND 
        v_flag_ttab = 'X' AND 
        v_flag_ptab = 'X'.
      EXIT.
    ENDIF.

* APpend the output internal table.
    APPEND otab.

  ENDDO.

endloop.

Thanks,

Naren

Read only

Former Member
0 Likes
2,029

Hi,

  • remove the records that are not in the select-options.

DELETE stab WHERE NOT fld2 IN S_FLD2.

DELETE ttab WHERE NOT fld2 IN S_FLD2.

DELETE ptab WHERE NOT fld2 IN S_FLD2.

loop at itab1.

  • read the table stab.

READ TABLE STAB WITH KEY FLD = ITAB1-FLD.

IF SY-SUBRC = 0.

  • read the table ttab.

READ TABLE TTAB WITH KEY FLD = ITAB1-FLD.

IF SY-SUBRC = 0.

  • read the table ptab.

READ TABLE PTAB WITH KEY FLD = ITAB1-FLD.

IF SY-SUBRC = 0.

  • Move the values to the output tab..

MOVE ptab TO otab.

  • Move the values to the output tab..

MOVE stab TO otab.

  • Move the values to the output tab..

MOVE ttab TO otab.

  • APpend the output internal table.

APPEND otab.

ENDIF.

ENDIF.

ENDIF.

endloop.

Please close the Message if this answers your query

Regards,

Pradeep Chintala.

Read only

Former Member
0 Likes
2,029

Hi Syed,

didn't u try this logic

Loop At itab1.

loop at stab where fld = itab1-fld and fld2 in s_fld2.

loop at ttab where fld = itab1-fld and fld3 in s_fld3.

loop at ptab where fld = itab1-fld.

move itab1 to otab.

move stab to otab.

move ttab to otab.

move ptab to otab.

append otab.

endloop.

endloop.

endloop.

<b>IF sy-subrc NE 0.

loop at ttab where fld = itab1-fld and fld3 in s_fld3.

loop at ptab where fld = itab1-fld.

move itab1 to otab.

move stab to otab.

move ttab to otab.

move ptab to otab.

append otab.

endloop.

endloop.

ENDIF.</b>

endloop.