Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to combine two result sets from different databases.

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

861 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