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: 

help in select statment

Former Member
0 Kudos

Hallow I have select statement and if sy-subrc ne to 0 it it don’t do the else statement why? how can I solve it?

Regards

LOOP AT mb_emp_tab INTO wa_mb_emp_tab.

SELECT stdaz plans pernr " Employee Numbers & Hours

FROM pa2010

INTO (wa_mb_emp_tab-no_of_hours_mb, wa_mb_emp_tab-plans, wa_mb_emp_tab-pernr)

  • FOR ALL ENTRIES IN mb_emp_tab

WHERE pernr = wa_mb_emp_tab-pernr

AND lgart = '9EXT' " Wage Type (salary)

AND begda GE end_date_mounth

AND begda LE start_date_mounth

GROUP BY stdaz plans pernr.

if sy-subrc = 0.

IF wa_mb_emp_tab-no_of_hours_mb IS NOT INITIAL.

wa_mb_emp_tab-part_emp = ( wa_mb_emp_tab-no_of_hours_mb / 185 ).

APPEND wa_mb_emp_tab TO mb_emp_tab2.

MODIFY mb_emp_tab FROM wa_mb_emp_tab

TRANSPORTING part_emp no_of_hours_mb plans.

CLEAR wa_mb_emp_tab.

<b> ELSE.</b>

ADD 1 TO wa_mb_emp_tab-part_emp .

ADD 185 TO wa_mb_emp_tab-no_of_hours_mb.

MODIFY mb_emp_tab FROM wa_mb_emp_tab

TRANSPORTING part_emp no_of_hours_mb plans.

CLEAR wa_mb_emp_tab.

ENDIF.

ENDSELECT.

ENDLOOP.

i dont wont to remove the end select before the if statment becouse

sometime i have in the if more then 1 recored

regards

1 ACCEPTED SOLUTION

ferry_lianto
Active Contributor
0 Kudos

Hi Antonio,

Please try this.


LOOP AT mb_emp_tab INTO wa_mb_emp_tab.

  SELECT stdaz plans pernr " Employee Numbers & Hours
  FROM pa2010
  INTO (wa_mb_emp_tab-no_of_hours_mb, wa_mb_emp_tab-plans, wa_mb_emp_tab-pernr)
* FOR ALL ENTRIES IN mb_emp_tab
  WHERE pernr = wa_mb_emp_tab-pernr
    AND lgart = '9EXT' " Wage Type (salary)
    AND begda GE end_date_mounth
    AND begda LE start_date_mounth
  GROUP BY stdaz plans pernr.

  wa_mb_emp_tab-part_emp = ( wa_mb_emp_tab-no_of_hours_mb / 185 ).
  APPEND wa_mb_emp_tab TO mb_emp_tab2.
  MODIFY mb_emp_tab FROM wa_mb_emp_tab
  TRANSPORTING part_emp no_of_hours_mb plans.
  CLEAR wa_mb_emp_tab.

  ENDSELECT.

  IF sy-subrc <> 0.
    ADD 1 TO wa_mb_emp_tab-part_emp . 
    ADD 185 TO wa_mb_emp_tab-no_of_hours_mb.
    MODIFY mb_emp_tab FROM wa_mb_emp_tab
    TRANSPORTING part_emp no_of_hours_mb plans.
    CLEAR wa_mb_emp_tab.
  ENDIF.
 
ENDLOOP.

Regards,

Ferry Lianto

4 REPLIES 4

Former Member
0 Kudos

sorry this is the write one

LOOP AT mb_emp_tab INTO wa_mb_emp_tab.

SELECT stdaz plans pernr " Employee Numbers & Hours

FROM pa2010

INTO (wa_mb_emp_tab-no_of_hours_mb, wa_mb_emp_tab-plans, wa_mb_emp_tab-pernr)

  • FOR ALL ENTRIES IN mb_emp_tab

WHERE pernr = wa_mb_emp_tab-pernr

AND lgart = '9EXT' " Wage Type (salary)

AND begda GE end_date_mounth

AND begda LE start_date_mounth

GROUP BY stdaz plans pernr.

if sy-subrc = 0.

wa_mb_emp_tab-part_emp = ( wa_mb_emp_tab-no_of_hours_mb / 185 ).

APPEND wa_mb_emp_tab TO mb_emp_tab2.

MODIFY mb_emp_tab FROM wa_mb_emp_tab

TRANSPORTING part_emp no_of_hours_mb plans.

CLEAR wa_mb_emp_tab.

<b>ELSE.</b>

ADD 1 TO wa_mb_emp_tab-part_emp .

ADD 185 TO wa_mb_emp_tab-no_of_hours_mb.

MODIFY mb_emp_tab FROM wa_mb_emp_tab

TRANSPORTING part_emp no_of_hours_mb plans.

CLEAR wa_mb_emp_tab.

ENDIF.

ENDSELECT.

ENDLOOP.

0 Kudos

You don'want but i fear you must.

In the SELECT ... ENDSELECT block, SY-SUBRC <b>ALWAYS</b> begin with 0. You go into the block only for records read from database.

Two solutions

1-the "bad" one :

LOOP AT  mb_emp_tab  
" do single select and check sy-subrc.
ENDLOOP.

2-The "good" one

SELECT for all ENTRIES into OUT_TABLE.
LOOP At mb_emp_tab.
" search in OUT_TABLE for relative records
" if not found, treatment for sy-subrc <> 0.
ENDLOOP.

Regards

0 Kudos

Hi Antonio

Here if sy-subrc NE 0, then it wont go inside the select-endselect statement, The processing will go inside the select-endselect only if there is data, Otherwise it wont go. There is no need to check sy-subrc EQ 0 inside select-endselect. Instead write like this,

LOOP AT mb_emp_tab INTO wa_mb_emp_tab.

SELECT stdaz plans pernr " Employee Numbers & Hours

FROM pa2010

INTO (wa_mb_emp_tab-no_of_hours_mb, wa_mb_emp_tab-plans, wa_mb_emp_tab-pernr)

  • FOR ALL ENTRIES IN mb_emp_tab

WHERE pernr = wa_mb_emp_tab-pernr

AND lgart = '9EXT' " Wage Type (salary)

AND begda GE end_date_mounth

AND begda LE start_date_mounth

GROUP BY stdaz plans pernr.

if sy-subrc = 0.

wa_mb_emp_tab-part_emp = ( wa_mb_emp_tab-no_of_hours_mb / 185 ).

APPEND wa_mb_emp_tab TO mb_emp_tab2.

MODIFY mb_emp_tab FROM wa_mb_emp_tab

TRANSPORTING part_emp no_of_hours_mb plans.

CLEAR wa_mb_emp_tab.

ENDIF.

ENDSELECT.

IF sy-subrc NE 0.

ADD 1 TO wa_mb_emp_tab-part_emp .

ADD 185 TO wa_mb_emp_tab-no_of_hours_mb.

MODIFY mb_emp_tab FROM wa_mb_emp_tab

TRANSPORTING part_emp no_of_hours_mb plans.

CLEAR wa_mb_emp_tab.

ENDIF.

ENDLOOP.

ferry_lianto
Active Contributor
0 Kudos

Hi Antonio,

Please try this.


LOOP AT mb_emp_tab INTO wa_mb_emp_tab.

  SELECT stdaz plans pernr " Employee Numbers & Hours
  FROM pa2010
  INTO (wa_mb_emp_tab-no_of_hours_mb, wa_mb_emp_tab-plans, wa_mb_emp_tab-pernr)
* FOR ALL ENTRIES IN mb_emp_tab
  WHERE pernr = wa_mb_emp_tab-pernr
    AND lgart = '9EXT' " Wage Type (salary)
    AND begda GE end_date_mounth
    AND begda LE start_date_mounth
  GROUP BY stdaz plans pernr.

  wa_mb_emp_tab-part_emp = ( wa_mb_emp_tab-no_of_hours_mb / 185 ).
  APPEND wa_mb_emp_tab TO mb_emp_tab2.
  MODIFY mb_emp_tab FROM wa_mb_emp_tab
  TRANSPORTING part_emp no_of_hours_mb plans.
  CLEAR wa_mb_emp_tab.

  ENDSELECT.

  IF sy-subrc <> 0.
    ADD 1 TO wa_mb_emp_tab-part_emp . 
    ADD 185 TO wa_mb_emp_tab-no_of_hours_mb.
    MODIFY mb_emp_tab FROM wa_mb_emp_tab
    TRANSPORTING part_emp no_of_hours_mb plans.
    CLEAR wa_mb_emp_tab.
  ENDIF.
 
ENDLOOP.

Regards,

Ferry Lianto