Solved

MS SQL Query Over 2 Different Instances

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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