Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to combine two result sets from different databases.

Posted on 2006-07-11
8
Medium Priority
?
649 Views
Last Modified: 2012-05-05
MS SQL 2000 - SQL Query Analyzer

I have two database servers SERVER1 AND SERVER2.
SERVER1 stores realtime data in a table.
SERVER2 stores historical data and has a view to access the data.
Datasets are compatible (Same fields)

SAMPLE:
SERVER1 DB1
Table: Devices
Columns: DeviceID, DeviceName, DeviceStatus, DeviceLastUpdate

SERVER2 DB2
View: Devices
Columns: DeviceID, DeviceName, DeviceStatus, DeviceLastUpdate

I need to combine the result sets into one for reporting purposes.  If both the table and view resided on the same server I would use a union but I don't know how to do this accross servers.
0
Comment
Question by:gusdarino
8 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 17082935
Hi gusdarino,

Have you tried:

SELECT *
FROM SERVER1.DB1.Devices
UNION
SELECT *
FROM SERVER2.DB2.Devices

Regards,

Patrick
0
 
LVL 25

Accepted Solution

by:
Mr_Peerapol earned 2000 total points
ID: 17082942
--ON SERVER1:
USE master
GO
EXEC sp_addlinkedserver
    'SERVER2',
    N'SQL Server'
GO


--Then, create view using this:

SELECT * FROM Devices
UNION ALL
SELECT * FROM SERVER2.DB2.Devices
0
 
LVL 13

Expert Comment

by:Atlanta_Mike
ID: 17082964
You'll need to create a linked server

EXEC sp_addlinkedserver
    'ServerName',
    N'SQL Server'
GO

Then

SELECT *
FROM dbo.Devices
UNION
SELECT *
FROM SERVER2.DB2.dbo.Devices
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17082965
Linked servers? Union maybe?

SELECT DeviceID, DeviceName, DeviceStatus, DeviceLastUpdate
FROM SERVER1.DB1.dbo.Devices
UNION
SELECT DeviceID, DeviceName, DeviceStatus, DeviceLastUpdate
FROM SERVER2.DB2.dbo.Devices
ORDER BY DeviceID

0
 
LVL 4

Author Comment

by:gusdarino
ID: 17083051
Mr Peerapol:

I don't have permission to perform that action.  Error Msg: "User does not have permission to perform this action."  I won't be able to get permissions either.  Is there another options?

GusDarino
0
 
LVL 4

Author Comment

by:gusdarino
ID: 17083090
matthewspatrick & DireOrbAnt

I tried that and I get: "Could not find server 'SERVER2' in sysservers. Execute sp_addlinkedserver to add the server to sysservers."
0
 
LVL 4

Author Comment

by:gusdarino
ID: 17083104
Mr_Peerapol:

Is the process of creating a linked server a one time operation that our DBA can do or does it have to be executed every time I run the query, etc.?

GusDarino
0
 
LVL 4

Author Comment

by:gusdarino
ID: 17083185
Mr_Peerapol:

Disregard my previous post.  I read up on linked servers.  I will see If I can get this ackomplished though our DBA.  This should work.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

877 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