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: 

Exiting nested select

Former Member
0 Kudos

HI,

I have two nested select statements. In the inner select statement if the retrieved value does not correspond to a specific criteria then I have to come out of both the select statments. How do I do that ?

Regards,

Varun.

1 ACCEPTED SOLUTION

former_member181962
Active Contributor
0 Kudos

use a flag.

set that flag.

based on that flag exit the second select also.

select.....

select......

if condition fails.

flag = 'x'.

exit.

endif.

endselect.

if flag = 'x'.

exit.

endif.

endselect.

Regards,

Ravi

12 REPLIES 12

Former Member
0 Kudos

Read about EXIT command.

BR,

Jacek

Former Member
0 Kudos

Hi,

In the innermost select set a flag = 'X' and then exit.

in the outer select check for this flag and if it is 'X'

(say) then exit.

select

select

if <specific criteria> " inner select

flag = 'X'.

exit.

endif. " inner select

if flag = 'X'. " outer select

clear flag.

exit.

endif. " outer select

Regards,

GSR.

0 Kudos

HI,

This is what I have done.

CLEAR : w_recnroot.

SELECT recnroot INTO w_recnroot

FROM estmj

WHERE matnr = t_outtab-matnr

AND delflg = space.

IF sy-subrc EQ 0.

SELECT subid subcat

INTO (t_outtab-subid, w_subcat)

FROM estrh

WHERE recnroot = w_recnroot

AND delflg = space.

if w_subcat in s_subcat.

w_value = 0.

else.

w_value = -1.

clear t_outtab-subid.

exit.

endif.

ENDSELECT.

ENDIF.

if w_value < 0.

exit.

endif.

ENDSELECT .

CLEAR : w_value.

Buytthis doesn't work for me.

Regards,

Varun.

0 Kudos

Try this code

TABLES: KNA1.

DATA: L_FIRST,

L_KUNNR LIKE KNA1-KUNNR,

L_KUNNR2 LIKE KNB1-KUNNR.

SELECT-OPTIONS: S_KUNNR FOR KNA1-KUNNR.

  • first

SELECT KUNNR

FROM KNA1

INTO L_KUNNR

WHERE KUNNR IN S_KUNNR.

CLEAR L_FIRST.

L_FIRST = '1'.

  • Second

SELECT KUNNR

FROM KNB1

INTO L_KUNNR2

WHERE KUNNR IN S_KUNNR.

L_FIRST = '2'.

ENDSELECT.

  • check - still flag is 1 then exit....

IF L_FIRST = '1'.

EXIT.

ENDIF.

ENDSELECT.

Regards,

Naimesh Patel

suresh_datti
Active Contributor
0 Kudos

HI Varun,

In the inner select update a flag & EXIT.. in the outer select check for this flag & EXIT if necessary..

Regards,

Suresh Datti

former_member181962
Active Contributor
0 Kudos

use a flag.

set that flag.

based on that flag exit the second select also.

select.....

select......

if condition fails.

flag = 'x'.

exit.

endif.

endselect.

if flag = 'x'.

exit.

endif.

endselect.

Regards,

Ravi

0 Kudos

Remove the sy-subrc check after the first select.. it si not necessary as you are already inside the SELECT by then..

Suresh

0 Kudos

HI,

I need that sy-subrc because it is an deoendent query. Only if first select is ok then only I need to work with second query.

REgards,

Varun.

0 Kudos

then change <i>if sy-subrc eq 0</i> to

<b>if not w_recnroot is initial</b>

Regards,

Suresh Datti

0 Kudos

No - you don't need it. If the outer select finds nothing, it won't go to the inner select.

Rob

former_member188685
Active Contributor
0 Kudos

Hi,

REPORT  ZTEST                              .

data: x_vbeln like vbak-vbeln,
      x_posnr like vbap-posnr.
data: flag .
select vbeln
       from vbak
       into x_vbeln .


select single posnr
       from vbap
       into x_posnr
     where vbeln = x_vbeln.

 if sy-subrc <> 0.
 flag = 'X'.
 exit.
 endif.
  if flag = 'X'.
  clear flag .
  exit.
  endif.
 endselect.

Regards

vijay

Message was edited by: Vijay Babu Dudla

Former Member
0 Kudos

Hi,

Set a flag equal zero. If the innner select statement succeeds, then set it to an integer value .

otherwise put exit statement in the inner select and outer select also.

regards.

Rakesh