UserSN
UserSN

Reputation: 1013

TSQL union all - error

I'm using the following query to pull some information from 2 tables to populate a drop-down field on my website with "Category" values.

I'm getting the following error.

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

This is my query:

select 'Choose a Category', 'All'

Union All

select distinct CategoryName 
from BND_Listing
inner join BND_listingCategories on BND_Listing.CatID = BND_ListingCategories.CatID

I suspect it has something to do with the inner join?

Any input appreciated!

Upvotes: 0

Views: 1153

Answers (1)

Markus Jarderot
Markus Jarderot

Reputation: 89171

select 'Choose a Category' as CategoryName
Union All
select 'All' as CategoryName
Union All
select distinct CategoryName from BND_Listing
inner join BND_listingCategories
on BND_Listing.CatID=BND_ListingCategories.CatID

or

select 'Choose a Category' as CategoryName, 'All' as Value
Union All
select distinct CategoryName, CategoryName as Value from BND_Listing
inner join BND_listingCategories
on BND_Listing.CatID=BND_ListingCategories.CatID

Upvotes: 2

Related Questions