Solved

How to combine two result sets from different databases.

Posted on 2006-07-11
8
639 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
 
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
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 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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

746 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

12 Experts available now in Live!

Get 1:1 Help Now