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

Exiting nested select

Former Member
0 Likes
1,333

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
Read only

Former Member
0 Likes
1,274

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
Read only

Former Member
0 Likes
1,274

Read about EXIT command.

BR,

Jacek

Read only

Former Member
0 Likes
1,274

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.

Read only

0 Likes
1,274

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.

Read only

0 Likes
1,274

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

Read only

suresh_datti
Active Contributor
0 Likes
1,274

HI Varun,

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

Regards,

Suresh Datti

Read only

Former Member
0 Likes
1,275

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

Read only

0 Likes
1,274

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

Suresh

Read only

0 Likes
1,274

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.

Read only

0 Likes
1,274

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

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

Regards,

Suresh Datti

Read only

0 Likes
1,274

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

Rob

Read only

Former Member
0 Likes
1,274

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

Read only

Former Member
0 Likes
1,274

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