Solved

MS SQL Query Over 2 Different Instances

Posted on 2009-06-29
5
245 Views
Last Modified: 2012-05-07
Hi,

I would like to query two sql databases that are on the same server but in different SQL instances.

For example:
 
SELECT * FROM instance1.Database1.Table1
INNER JOIN instance2.Database2.Table2 ON (Table1.Col1 = Table2.Col2)

How would I do this? Would I have to configure a linked server by using sp_addlinkedserver?

Thanks
0
Comment
Question by:spen_lang
  • 2
  • 2
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24734599
>How would I do this? Would I have to configure a linked server by using sp_addlinkedserver?
yes.
if you are on instance1, you configure the linked server to instance2, and the query then would go like this:
SELECT * 
  FROM Database1.dbo.Table1 t1
INNER JOIN instance2.Database2.dbo.Table2 t2
  ON (t1.Col1 = t2.Col2)

Open in new window

0
 

Author Comment

by:spen_lang
ID: 24734627
I thought as much, thanks for your quick response. Do you have anymore information that may help me in setting up the linked servers? Any useful resources?
0
 

Author Closing Comment

by:spen_lang
ID: 31597839
Great response, thanks.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24734711
I thought I posted the links already:

generate topic:
msdn.microsoft.com/en-us/library/aa213778(SQL.80).aspx

sp_addlinkedserver:
http://msdn.microsoft.com/en-us/library/aa259589(SQL.80).aspx
0
 
LVL 14

Expert Comment

by:Jagdish Devaku
ID: 24734802
using linked server we can do this...
below is the syntax to add the linked server...
once you add the linked server on an instance to access the database on another instance... then you can run the queries as below
SELECT * FROM Database1.Table1 t1
INNER JOIN <'remoteip\instance'>.Database2.Table2 t2 ON (t1.Col1 = t2.Col2)
 let me know if you have any issues...

exec sp_addlinkedserver 'remoteip\instance'   -- accept defaults for all other params
exec sp_addlinkedsrvlogin @rmtsrvname='remoteip\instance', @useself=false, @locallogin=null,  @rmtuser='username', @rmtpassword='password'

Open in new window

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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