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
 Querying across database instances.

Author  Topic 

dougbroadwell
Starting Member

4 Posts

Posted - 2010-01-28 : 17:19:46
I'm writing an application in C++ & Qt accessing a SQL Server instance via ODBC. I need to query data with joins from two different databases in that instance and don't know how to do it. Is there a way to create an ODBC connection that can access both? Is there a way to create views on the server, or procedures on the server, that can access both databases? Other options?

Thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-01-28 : 20:11:21
If the databases are on the same SQL instance, then you can reference the "remote" database by using the three-part naming convention of the objects. Here is an example:

USE db1
GO

SELECT *
FROM Table1 t1
JOIN db2.dbo.Table2 t2
ON t1.Column1 = t2.Column1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -