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
 General SQL Server Forums
 New to SQL Server Programming
 Run identical query on 3 tables

Author  Topic 

kangle
Starting Member

5 Posts

Posted - 2015-02-18 : 13:28:55
I have a database with three different tables having the exact same fields. New records are written to table1, before moving to table2 and ultimately table3. I was wondering if it's possible to run the same query on all three tables at the same time. I need to get all unique instances in the JC field from each table after a specified date. I get an "Ambiguous column name" error on the JC and TimeID fields.

SELECT distinct [JC]
FROM [table1], [table2], [table3]
where timeid > '20090900';

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-02-18 : 13:36:38
SELECT distinct [JC]
FROM [table1]
where timeid > '20090900'
UNION ALL
SELECT distinct [JC]
FROM [table2]
where timeid > '20090900'
SELECT distinct [JC]
FROM [table3]
where timeid > '20090900';

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -