Join results from tables with same name from different databases
Lets say we have two databases, one named db1, the other one named db2. Both databases have a table called Employees. Is there anyway to join results from both tables even though they have the same name?
Note that the two tables do NOT have conflicting field names. I know this is stupid but w开发者_JAVA百科e had to make an extension database to the existing one and to keep it simple we'd like to have the same table name in both databases.
Help is greatly appreciated, thanks
Also I know there is a similar question answered here but I didn't find it helpful in this context.
You should be able to refer to the tables by their fully-qualified names. If they're in different schemas, you can say:
SELECT * FROM Schema1.MyTable as T1 JOIN Schema2.MyTable as T2 ON T1.Something = T2.SomethingElse
If they're actually different databases, you'd need a database link in which case it becomes MyTable1@Database1 etc.
Yes you can definitely do this, you'll just need to alias them like so:
SELECT ... FROM [db1].[dbo].[Employees] as e1 INNER JOIN [db2].[dbo].[Employees] as e2 ON ...
Then you can get what you need by referencing e1.MyColumn
or e2.MyOtherColumn
.
You should be able to reference them by database.owner.table, e.g. db1.dbo.Employees and db2.dbo.Employees (assuming the tables are owned by dbo)
select * from db1,db2 where db1.employee=db2.employee Is this what you mean?
精彩评论