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)
 Query multiple databases

Author  Topic 

medtech26
Posting Yak Master

169 Posts

Posted - 2009-06-02 : 19:32:16

What's the best way to query two (or more) tables in two different databases (on the same server)?

Is there a way to open a connection string for two databases?

I have found this ([url]http://www.databasejournal.com/features/mssql/article.php/3085211/Linked-Servers-on-MS-SQL-Part-1.htm[/url]) and although it make scene it's pretty old.

This is for SQL Server 2005 Standard Ed.

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2009-06-02 : 19:49:02
If the two databases are on the same server then you can just query them like this:

SELECT * FROM database1..table a
JOIN database2..table b on a.id = b.id

Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

medtech26
Posting Yak Master

169 Posts

Posted - 2009-06-02 : 20:51:22
Thanks Skorch.

If not mistaking this will work only in the QA and I need to connect to the DBs through third party (server side) app., therefor looking for something leaning more to the connection string solution. In case this is impossible, I was thinking to create a view on DB_A to bring relevant data from DB_B.
Go to Top of Page

medtech26
Posting Yak Master

169 Posts

Posted - 2009-06-03 : 12:20:33
Trying my luck again as this went to the second page ... anyone?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-03 : 12:28:49
If I don't misunderstood, you think it is a problem to query a table in a different database (db_2) while your connection in app is set to db_1?
To query that table in db_2 try to give the object_name including the databasename.

select * from db_2.dbo.table

This should work...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

medtech26
Posting Yak Master

169 Posts

Posted - 2009-06-03 : 13:07:12
quote:
Originally posted by webfred

If I don't misunderstood, you think it is a problem to query a table in a different database (db_2) while your connection in app is set to db_1?
To query that table in db_2 try to give the object_name including the databasename.

select * from db_2.dbo.table

This should work...


No, you're never too old to Yak'n'Roll if you're too young to die.



OK, got it. Just needed to edit user permissions on the server , Thanks.
Go to Top of Page
   

- Advertisement -