Skip to Content
0
Former Member
May 27, 2010 at 10:29 AM

How to avoid Select statements inside loops In general

160 Views

Please give me some general idea on how to avoid selectstatements inside loops .Is there any other way . Please consider the below code for sample .

In general please tell me how to avoid selects in a loop if we need to write a code as shown below .

Can u please tell me some general coding practices ??

LOOP AT tl_vbak INTO wl_vbak.

CASE wl_vbak-auart.

WHEN c_auart_zrit.

*C-Here 4 cases are there so select inside loop is used

SELECT lead_aufnr maufnr FROM m_ordna INTO TABLE tl_m_ordna WHERE aufnr EQ wl_vbak-xblnr.

IF sy-subrc EQ 0.

LOOP AT tl_m_ordna INTO wl_m_ordna.

IF wl_m_ordna-lead_aufnr = wl_m_ordna-maufnr.

CONCATENATE c_ord_cat wl_m_ordna-lead_aufnr INTO l_add_data2.

LOOP AT tl_gen1 INTO wl_gen1.

wl_gen1-add_data2 = l_add_data2.

APPEND wl_gen1 TO tl_gen2.

cs_itm_cdoc-gen = tl_gen2.

CLEAR wl_gen1.

CLEAR l_add_data2.

CLEAR tl_gen2.

ENDLOOP.

ENDIF.

CLEAR wl_m_ordna.

ENDLOOP.

CLEAR wl_vbak.

ENDIF.

WHEN c_auart_zrpr.

*C-Here 4 cases are there so select inside loop is used

SELECT aufnr FROM vbep INTO TABLE tl_vbep WHERE vbeln EQ wl_vbak-vbeln

AND ettyp EQ c_ettyp_cd.

IF sy-subrc EQ 0.

LOOP AT tl_vbep INTO wl_vbep.

CONCATENATE c_ord_cat wl_vbep-aufnr INTO l_add_data2.

LOOP AT tl_gen1 INTO wl_gen1.

wl_gen1-add_data2 = l_add_data2.

APPEND wl_gen1 TO tl_gen2.

cs_itm_cdoc-gen = tl_gen2.

CLEAR wl_gen1.

CLEAR l_add_data2.

CLEAR tl_gen2.

ENDLOOP.

CLEAR wl_vbep.

ENDLOOP.

CLEAR wl_vbak.

ENDIF.