Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connect to multiple servers using Query Analyzer T-SQL

Status
Not open for further replies.

newjim

Programmer
Sep 8, 2001
21
CA
I need to compare userids in tables in databases in two SQL Servers running on two machines. I would like to do this using the Query Analyzer, for speed. Something like:

--connect through Query Analyzer gui to 1stSQLServer
select userid from 1stSQLServer.users
--connect to 2nd SQL Server
foreach userid in 1stSQLServer.users
select userid in 2ndSQLServer.users
if userid is null...

How do you connect to this 2ndSQLServer using the T-SQL of Query Analyzer?
 
Create a linked server between the servers..
Then...

Select * from <table1>
Inner Join ServerName.DataBase.owner.<table2 Name> ON
<table1>.<column> = <table2>.<column>
 
Thank you.

Can you run by me the process for creating a linked server?
 
Thank you!

This stored procedure told me that I already had a link to MYLINKEDSERVER

sp_linkedservers

If I didn't, I would have created a linked server using:

sp_addlinkedserver(syntax is in Books Online)

Then I logged in to MYLINKEDSERVER by:

exec sp_addlinkedsrvlogin MYLINKEDSERVER, 'false', NULL, 'mylogin', 'mypassword'

Finally, I did a query on the mydatabase database using the openquery() method:

select * from
openquery(MYLINKEDSERVER,'SELECT id from mydatabase.dbo.users')

Note that there are no quotes around "MYLINKEDSERVER" and that you have to specify the table owner (dbo) in the remote query.

This has been a most useful posting for me, and I hope so for others.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top