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: 

Syntax for Nested Select Statement

Former Member
0 Kudos

Gday Guys,

I am having some dramas with this select statement. For the records in cust_info, when agencynum ne 0, I want to get the corresponding name field from stravelag into cust_info-name and write it. (along with all the other data in that row of cust_info)

I keep getting errors in the final select statement. Any ideas would be much appreciated.

Thanks

Stuart

at line-selection.

check not scustom-id is initial.

select fldate bookid carrid connid class agencynum from sbook

into cust_info

where customid = scustom-id

order by fldate.

if cust_info-agencynum = 0.

write:/ 'Agency Unknown',

40 cust_info-fldate,

55 cust_info-bookid,

70 cust_info-carrid,

85 cust_info-connid,

100 cust_info-class.

elseif cust_info-agencynum > 0.

select single name from stravelag

into cust_info

where agencynum = cust_info-agencynum.

write:/ cust_info-name.

endselect.

endif.

endselect.

write:/ sy-dbcnt.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Stuart,

U have mentioned endselect for the second select statement..

Do not use ENDSELECT for SELECT SINGLE query..

SELECT... ENDSELECT is used when u r using a loop generally for many records..

For SELECT SINGLE, do not use ENDSELECT.

Regard,

Tanveer.

<b>Please mark helpful answers</b>

13 REPLIES 13

Former Member
0 Kudos

welcome to SDN!!!!!

remove endselect for the second select as it is select single

modify ur code like this

Syntax for Nested Select Statement

Posted: May 5, 2006 4:06 AM Reply E-mail this post

Gday Guys,

I am having some dramas with this select statement. For the records in cust_info, when agencynum ne 0, I want to get the corresponding name field from stravelag into cust_info-name and write it. (along with all the other data in that row of cust_info)

I keep getting errors in the final select statement. Any ideas would be much appreciated.

Thanks

Stuart

at line-selection.

check not scustom-id is initial.

select fldate bookid carrid connid class agencynum from sbook

into cust_info

where customid = scustom-id

order by fldate.

if cust_info-agencynum = 0.

write:/ 'Agency Unknown',

40 cust_info-fldate,

55 cust_info-bookid,

70 cust_info-carrid,

85 cust_info-connid,

100 cust_info-class.

elseif cust_info-agencynum > 0.

select single name from stravelag

into cust_info

where agencynum = cust_info-agencynum.

write:/ cust_info-name.

<b>removed endselect</b>

endif.

endselect.

write:/ sy-dbcnt.

<b>Reward points if helpful</b>

Message was edited by: Sekhar

Former Member
0 Kudos

Hi Stuart,

U have mentioned endselect for the second select statement..

Do not use ENDSELECT for SELECT SINGLE query..

SELECT... ENDSELECT is used when u r using a loop generally for many records..

For SELECT SINGLE, do not use ENDSELECT.

Regard,

Tanveer.

<b>Please mark helpful answers</b>

Former Member
0 Kudos
sorry , remove single statement from select single

elseif cust_info-agencynum > 0.
select  name from stravelag
into cust_info
where agencynum = cust_info-agencynum.
write:/ cust_info-name.
endselect.

Former Member
0 Kudos

Just remove the First ENDSELECT (The one for the second SELECT statement inside the IF condition)

Regards,

Ravi

Note : Please close the thread if the issue is resolved

former_member188685
Active Contributor
0 Kudos
at line-selection.

check not scustom-id is initial.

select fldate bookid carrid connid class agencynum from sbook
into cust_info
where customid = scustom-id
order by fldate.

if cust_info-agencynum = 0.
write:/ 'Agency Unknown',
40 cust_info-fldate,
55 cust_info-bookid,
70 cust_info-carrid,
85 cust_info-connid,
100 cust_info-class.

elseif cust_info-agencynum > 0.
select single name from stravelag
into cust_info
where agencynum = cust_info-agencynum.
write:/ cust_info-name.
<b>*endselect.</b>
endif.
endselect.

write:/ sy-dbcnt.

don't give endselect for select single.

remove endselect ,then try

regards

vijay

Former Member
0 Kudos

hai Stuart

elseif cust_info-agencynum > 0.

select single name from stravelag

into cust_info

where agencynum = cust_info-agencynum.

write:/ cust_info-name.

endselect.----


> Not use this Endselect statement

endif.

Check the bellow code

data : begin of it_mara1 occurs 0,

matnr like mara-matnr,

end of it_mara1.

This is wrong

select single matnr from mara

into it_mara1

where mtart = 'ROH'.

write:/ it_mara1-matnr.

endselect.

This is write

select single matnr from mara

into it_mara1

where mtart = 'ROH'.

write:/ it_mara1-matnr.

Thanks & Regards

Sreenivasulu P

Former Member
0 Kudos

Thanks for your quick responses and suggestions.

I have changed my code to: (removed endselect and single).

how ever i am still getting an incorrect nesting error?

*newcode*

select fldate bookid carrid connid class agencynum from sbook

into cust_info

where customid = scustom-id

order by fldate.

if cust_info-agencynum = 0.

write:/ 'Agency Unknown',

40 cust_info-fldate,

55 cust_info-bookid,

70 cust_info-carrid,

85 cust_info-connid,

100 cust_info-class.

elseif cust_info-agencynum ne 0.

select name from stravelag

into cust_info

where agencynum = cust_info-agencynum.

write:/ cust_info-name.

endif.

endselect.

write:/ sy-dbcnt.

0 Kudos
Hi stuart .

try this 

select name from stravelag
into cust_info
where agencynum = cust_info-agencynum.
write:/ cust_info-name.
endselect.

here u have to use select ... endselect bcoz u may get many agency numbers

0 Kudos
**newcode**
select fldate bookid carrid connid class agencynum from sbook
  into cust_info
  where customid = scustom-id
  order by fldate.

if cust_info-agencynum = 0.
write:/ 'Agency Unknown',
  40 cust_info-fldate,
  55 cust_info-bookid,
  70 cust_info-carrid,
  85 cust_info-connid,
  100 cust_info-class.

<b>else.</b>
  select single name from stravelag
    into cust_info
    where agencynum = cust_info-agencynum.
    write:/ cust_info-name.
endif.
endselect.

write:/ sy-dbcnt.

try the above code......

Message was edited by: Vijay Babu Dudla

Former Member
0 Kudos

The problem is you have taken out both SINGLE as well as ENDSELECT for the second SELECT.

You will have to retain one of them.

Regards,

Ravi

Note : Please mark the helpful answers

Former Member
0 Kudos

Thanks for the tips.

I have modified the code and can now run the report, however another error occurs, the reason being:

The data read during the SELECT access could not be inserted into the target field. Either converstion is not supported for the target fields type or the target field is too short.

How can this be the case when I have defined cust_info as:

data: begin of cust_info occurs 0,

fldate like sbook-fldate,

bookid like sbook-bookid,

carrid like sbook-carrid,

connid like sbook-connid,

class like sbook-class,

agencynum like sbook-agencynum,

name like stravelag-name,

end of cust_info.

The target field is defined as like stravelag-name?

0 Kudos

Change your statement to this

select fldate bookid carrid connid class agencynum from sbook

<b>into corresponding fields of </b>

cust_info

where customid = scustom-id

order by fldate.

Regards,

RAvi

Former Member
0 Kudos

Problem solved! Thanks team. As a rookie ABAPer your help is very much appreciated.