Solved

How to combine two result sets from different databases.

Posted on 2006-07-11
8
641 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 92

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 500 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

776 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