Solved

MS SQL Query Over 2 Different Instances

Posted on 2009-06-29
5
264 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
[X]
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
  • 2
  • 2
5 Comments
 
LVL 143

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 143

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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