Solved

Single query from 2 databases

Posted on 2006-11-29
6
416 Views
Last Modified: 2008-02-01
Hi,

I am using SQL 2005

If I have 2 database

say database1 and database2

In database1 I have table1 and in database2 I have table 2

can I make query like this

select table1.field1, table1.field2, table2.field1, table2.field2 from dba.dbo.database1.table1 as table1 inner join dba.dbo.database2.table2 as table2

I mean can I make query from 2 databases

It gives me error saying that

Msg 7202, Level 11, State 2, Line 1
Could not find server 'DBA' in sysservers. Execute sp_addlinkedserver to add the server to sysservers.

Can anyone help me

Kind Regards
0
Comment
Question by:dinesh_bali
6 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18036371
> from dba.dbo.database1.table1
the syntax is servername.databasename.username.tablename.

so, I guess that there is some confusion?
assuming that both databases are on the samer server/instance:
from database1.dbo.table1 table1 inner join database2.dbo.table2 as table2

0
 
LVL 28

Expert Comment

by:imran_fast
ID: 18036399
dba.dbo.database2.table2

servername.databasename.ownername.tablename

dba.database2.dbo.table2
0
 
LVL 20

Expert Comment

by:Sirees
ID: 18036421
Are both databases on the same server?

If not, you need to add a linked server.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 18036430
ur other server should be linked server
u must first add the server as  a linked server and then run the queries posted aboveif it's a one time job then u could use
openquery
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 18036449
opendatasource i mean

SELECT   *
FROM      OPENDATASOURCE(
         'SQLOLEDB',
         'Data Source=ServerName;User ID=MyUID;Password=MyPass'
         ).Northwind.dbo.Categories
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 18036460
to use open query
first add linked server
EXEC sp_addlinkedserver 'OracleSvr',
   'Oracle 7.3',   'MSDAORA',
   'ORCLDB'
GO
and then use open query or the querie posted above
SELECT *
FROM OPENQUERY(OracleSvr, 'SELECT name, id FROM joe.titles')
GO

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now