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 execute query

Author  Topic 

chbala85
Starting Member

49 Posts

Posted - 2014-01-03 : 03:52:19
Hi All,

I have two servers.

server1 having db1 database.

server2 having db2 database.

here server1,server2 having different IP address.

i login into server1 and then execute a query of server2 db2 tables.

Please Help Me............




bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2014-01-03 : 04:34:46
You need to create linked server for server2 in server1
then use fully qualified names for objects as follows:
Server2.DatabaseName.SchemaName.ObjectName

--
Chandu
Go to Top of Page

chbala85
Starting Member

49 Posts

Posted - 2014-01-03 : 04:40:17
Thank you,

The two servers in different network. how it is possible to create a linked server.

Thanks

krish
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2014-01-03 : 05:16:29
If the server is remote you may have to set up an Alias for the remote server first (how to do that depends on what version of SQL you are using; in modern versions its in "SQL Server Configuration Manager", that allows you to specify TCP/IP or something else, IP Address, etc.), then you can set up a Linked Server in SSMS or Enterprise Manager (old versions of SQL) using that Alias name, and then once you have done that you can use

SELECT Col1, Col2
FROM LinkedServerName.RemoteDatabase.dbo.TableName

however, you may find performance using that 4-part naming style if not good enough, so you make need to use something like OPENQUERY instead

SELECT Col1, Col2
FROM OPENQUERY(LinkedServerName, 'SELECT Col1, Col2 FROM RemoteDatabase.dbo.TableName')
Go to Top of Page
   

- Advertisement -