Solved

How to combine two result sets from different databases.

Posted on 2006-07-11
8
640 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

867 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

16 Experts available now in Live!

Get 1:1 Help Now