You can just use a literal value in the queryies. The name of the fields in the result is taken from the first query, so if you want a name for the field you only have to specify it in the first query.
Use 'union all' rather than 'union' in this case, as it's more efficient. If you use 'union', it will match all the records against each other to remove duplicates between the queries, but as you have a field that has a different value for each query, you know that there are no duplicates to remove, so there is no point doing the work to look for them.
select 1 as [Table], Name, Address
from Table1
union all
select 2, Name, Address
from Table2
Main Topics
Browse All Topics





by: pcharbonPosted on 2009-03-10 at 10:57:34ID: 23849457
try this
SELECT Name, Address, table = 'Table1' FROM Table1
UNION
SELECT Name, Address, table = 'Table2' FROM Table2