Alex P Alex P - 2 months ago 20
SQL Question

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!

Answer
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