Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to connect 2 databases on 2 servers

Author  Topic 

mab
Starting Member

3 Posts

Posted - 2013-08-23 : 16:12:26
We are wanting to know how to connect 2 databases residing on 2SQL servers. The workstation has access to both databases.
Join on Cols 1 and 2.

If you could give it to us in a statement form using the following data that really helps.


Server1, Database1, Table1, Column1 (Char)
Server2, Database2, Table2, Column2 (VarChar)

Thank you

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2013-08-23 : 18:10:05
You can do it at the application layer using two separate connections but then you'd need to perform the joining at that layer also. If you want to do it at the database layer, you can create a "linked server" from one server to the other. Once this is set up you can reference the second table by using a four part nameing convention. The reference will be in the format of Server.Database.Schema.Table. Using your sample and assuming that the linked server has been set up on Server1 to access Server2 you could write a select statement like:
select here.*, there.*
from Table1 here
inner join
Server2.Database2.dbo.Table2 there
on here.Column1 = there.Column2
I am assuming dbo as the schema. You will need to make some decisions on what security you want to apply when setting up the linked server. Also, if you want a transaction to wrap around the linked server access, you'll need to configure MS-DTC. Here is a link to a how-to article for that: http://www.sqlvillage.com/Articles/Distributed%20Transaction%20Issue%20for%20Linked%20Server%20in%20SQL%20Server%202008.asp

HTH

=================================================
The cure for anything is salt water -- sweat, tears, or the sea. -Isak Dinesen
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-08-24 : 11:11:06
see the steps to create linked server here

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=164892

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -