Solved

MS SQL Query Over 2 Different Instances

Posted on 2009-06-29
5
238 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ms sql stored procedure 22 87
Oracle - Query Insert and Update multiple tables 5 57
MySQL left join performance 4 30
mySQL Syntax 7 34
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

13 Experts available now in Live!

Get 1:1 Help Now