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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Getting Data From 2 Databases

Author  Topic 

rcr69er
Constraint Violating Yak Guru

327 Posts

Posted - 2008-10-01 : 07:18:16
Hi

I have two databases – Database1 and Database2 (Original names I know!!!). Both have the exact same structure but different data as each database belongs to a different company.

I have produced a Select statement for one of the databases in which I want to Union using the same Select statement with the other database. Is this possible to do?

Thanks

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2008-10-01 : 07:25:26
SELECT <columns>
FROM
[database1].dbo.table
UNION
SELECT <columns>
FROM
[database2].dbo.table
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-01 : 07:27:17
Are databases in same server? If yes just use four part naming convention as db.schema.tablename.
If not add one as linked server on other server and use as above.

or use OPENROWSET
Go to Top of Page

rcr69er
Constraint Violating Yak Guru

327 Posts

Posted - 2008-10-01 : 07:44:38
Hi

Thanks for that guys.

Yes the databases are going to be on the same server.

Thanks
Go to Top of Page
   

- Advertisement -