SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL Code Problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

viperbyte
Posting Yak Master

USA
132 Posts

Posted - 06/17/2013 :  08:48:00  Show Profile  Reply with Quote
Good morning everybody,

I'm following along in a book and I have entered the following select statement and I get and error that says "Incorrect syntax near 'ProductCategoryID'". Do you folks know how I can get this bit of code to work?

SELECT -1 AS ProductCategoryID, '(All Categories)' AS NAME
UNION ProductCategoryID, NAME
FROM Production.ProdcutCategory
ORDER BY NAME

viperbyte
Posting Yak Master

USA
132 Posts

Posted - 06/17/2013 :  09:05:09  Show Profile  Reply with Quote
I rewrote it like this and it works. Now I just hope that as I get nearer to the end of the tutorial I get the expected results.

SELECT -1 AS ProductCategoryID, '(All Categories)' AS NAME
FROM Production.ProductCategory

UNION

SELECT ProductCategoryID, Name
FROM Production.ProductCategory
ORDER BY NAME

Edited by - viperbyte on 06/17/2013 09:05:40
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4612 Posts

Posted - 06/17/2013 :  14:12:27  Show Profile  Reply with Quote
NO need for the FROM clause on teh first query:
SELECT -1 AS ProductCategoryID, '(All Categories)' AS NAME

UNION

SELECT ProductCategoryID, Name
FROM Production.ProductCategory
ORDER BY NAME
Go to Top of Page

viperbyte
Posting Yak Master

USA
132 Posts

Posted - 06/17/2013 :  16:29:53  Show Profile  Reply with Quote
Thanks.
Go to Top of Page

djj55
Constraint Violating Yak Guru

USA
308 Posts

Posted - 06/18/2013 :  15:40:07  Show Profile  Reply with Quote
Also you may need UNION ALL to get your "header"

djj
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 06/19/2013 :  03:42:23  Show Profile  Reply with Quote
quote:
Originally posted by djj55

Also you may need UNION ALL to get your "header"

djj


>> your "header"
means you are talking about column headers.. right?
In that case there is no need UNION ALL ... UNION itself gives the column headers....

--
Chandu
Go to Top of Page

djj55
Constraint Violating Yak Guru

USA
308 Posts

Posted - 06/19/2013 :  12:30:43  Show Profile  Reply with Quote
No I just meant the -1 and '(All Categories)', for lack of better name. I have a query that I had headers in that used a union but the headers were not the first row, until I used UNION ALL.

Thus the "may need"

djj
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 06/20/2013 :  03:14:33  Show Profile  Reply with Quote
quote:
Originally posted by djj55

No I just meant the -1 and '(All Categories)', for lack of better name. I have a query that I had headers in that used a union but the headers were not the first row, until I used UNION ALL.

Thus the "may need"

djj


they are ordering result set based on NAME (2nd column)...
so it won't depend on UNION or UNION ALL...

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000