Is there a function that returns the table name in a union query?

J

JT

I am using a union query to join three tables with the same structure. The
tables are named "H_F", "B_D", and "Croydon". I have used the following SQL
syntax:

Select * from [B_D] UNION ALL
Select * from [Croydon] UNION ALL
Select * from [H_F]

As part of the query I would like to add a field saying which table each of
the rows originated from. Would any of you be able to help me with this?
 
D

Dirk Goldgar

JT said:
I am using a union query to join three tables with the same structure. The
tables are named "H_F", "B_D", and "Croydon". I have used the following
SQL
syntax:

Select * from [B_D] UNION ALL
Select * from [Croydon] UNION ALL
Select * from [H_F]

As part of the query I would like to add a field saying which table each
of
the rows originated from. Would any of you be able to help me with this?


You have to hard-code the name as a calculated field, like this:

Select *, "B_D" AS SourceTable From [B_D]
UNION ALL
Select *, "Croydon" AS SourceTable From [Croydon]
UNION ALL
Select *, "H_F" AS SourceTable from [H_F]
 
T

Tom van Stiphout

On Wed, 27 Aug 2008 06:18:01 -0700, JT <[email protected]>
wrote:

No, but nothing prevents you from writing:
Select *, "B_D" as Tablename from [B_D] UNION ALL
Select *, "Croydon" as Tablename from [Croydon] UNION ALL
Select *, "H_F" as Tablename from [H_F]

-Tom.
Microsoft Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

UNION QUERY.. 3
Union query for 2 or many tables 2
Make table using Union Query 2
Union Query 4
UNION query question 7
Data Mismatch in Excel 2010 4
Union Query by choice.. 11
Union and group, then count 1

Top