Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL 7.0 T-SQL error with UNION

Status
Not open for further replies.

bbosley

Programmer
Apr 3, 2001
12
0
0
CA
What is wrong with the following T-SQL running in SQL 7.0 against the pubs database?:

Select top 1
4 AS JustANumber, au_id, au_lname, au_fname, state, contract
from authors
where state = 'CA'
order by contract ASC
UNION
Select top 1
5 as JustANumber, au_id, au_lname, au_fname, state, contract
from authors
where state = 'UT'
order by contract ASC

This gives me an error:
Server: Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'UNION'.

If I remove the ORDER BY clause from the first SELECT it runs. But I only want one record returned from each SELECT.

Any ideas?

Bryan
 
taking out the first 'order by' clause should eliminate the error and give you the expected result. when i run the same query, i only get one record for 'CA' and one record for 'UT'. maybe i misunderstood tho... X-)
 
Silly question I know but... why are you tring to Order a single record ??????
 
order by must be at the end of whole query. This clause inside each union segment is not supported. John Fill
1c.bmp


ivfmd@mail.md
 
I used the following:

SELECT *
FROM (Select top 1
4 AS JustANumber, au_id, au_lname, au_fname, state, contract
from authors
where state = 'CA'
order by contract ASC) AS TEMP1
UNION
SELECT *
FROM (Select top 1
5 as JustANumber, au_id, au_lname, au_fname, state, contract
from authors
where state = 'UT'
order by contract ASC) AS TEMP2

This gives me the desired result in my more complex query.

Thanks for the help.

bbosley
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top