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
 2 DBs on 2 Different Servers

Author  Topic 

VJG
Starting Member

17 Posts

Posted - 2012-03-23 : 12:03:24
Guys I need help again. Im trying to query two databases on different servers.

On one server, (server1) I have a database,(database1) with a table (table1) that has studentID, FName, LName.

On the other (server2) in its database2,. i have a table (table2) that has StudentID, and StudentUserName.

I'm trying to pull something like this: table1.StudentID, table1.Fname, table1.LName, table2.StudentUserName.

I've searched the internet some examples, but i still haven't been able to figure it out.

I appreciate any advice.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-23 : 12:24:34
Probably the simplest (conceptually at least) is to set the two servers as linked servers ( http://msdn.microsoft.com/en-us/library/ms188279.aspx ) and then query like this:
SELECT 
table1.StudentID, table1.Fname, table1.LName, table2.StudentUserName
FROM
server1.database1.dbo.Table1 AS Table1
INNER JOIN server2.database2.dbo.Table2 AS Table2
ON table1.StudentId = table2.StudentId
Depending on how you set it up, you would not need the server qualifier for the table on the server you are running the query from.
Go to Top of Page

VJG
Starting Member

17 Posts

Posted - 2012-03-23 : 14:11:55
Thanks. I'll give it a try..
Go to Top of Page

VJG
Starting Member

17 Posts

Posted - 2012-03-23 : 14:47:45
Thanks again. It worked perfectly. I'm new to SQL and this site has been really helpful. Thanks Sunita!!
Go to Top of Page
   

- Advertisement -