Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL Code Problem

Author  Topic 

viperbyte
Posting Yak Master

132 Posts

Posted - 2013-06-17 : 08:48:00
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

132 Posts

Posted - 2013-06-17 : 09:05:09
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
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-06-17 : 14:12:27
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

132 Posts

Posted - 2013-06-17 : 16:29:53
Thanks.
Go to Top of Page

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2013-06-18 : 15:40:07
Also you may need UNION ALL to get your "header"

djj
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-06-19 : 03:42:23
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

352 Posts

Posted - 2013-06-19 : 12:30:43
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-06-20 : 03:14:33
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
   

- Advertisement -