Solved

sp_addlinkedserver ??

Posted on 2001-07-26
4
1,597 Views
Last Modified: 2012-06-27

 Hi all!

 I am writing an online remote database migration tool.
 I need to write a stored procedure which can connect
 to a remote SQL Server entered by the web-user.
 The user will enter the hostname, username and password for the remote server.
 How do I connect to this server?
 I found something: sp_addlinkedserver - It seems to link to another server :) But how do I use it and how do I perform my SELECT and DELETE statements?

 Regards,


 D. Walsarie
0
Comment
Question by:dwalsarie
  • 2
4 Comments
 
LVL 1

Expert Comment

by:zahid_synentia
ID: 6321392
e.g.

select *
from LINKEDSERVERNAME.databasename.dbo.tablename
0
 
LVL 2

Author Comment

by:dwalsarie
ID: 6321424

 Could you be complete, please?
0
 
LVL 1

Expert Comment

by:zahid_synentia
ID: 6321528
You just query the tables on the linked server as normal except you need to prefix the table name with the linked server name and the databasename on the linked server

so
-------------------------------------------------
select *
from LINKEDSERVERNAME.DATABASENAME.dbo.TABLENAME
------------------------------------------------
where

1, LINKEDSERVERNAME - name of linked server
2, DATABASENAME-  name of database table belongs to on the server
3, dbo - database owner
4, TABLENAME- name of table you want to query on the linked database
0
 

Accepted Solution

by:
DecaysChampion earned 200 total points
ID: 6324823
Dwalsarie,

If you're asking how to use the command...
This is a sample piece of code I wrote for a stored procedure that compares two databases on different servers based on input of the sa user name and password.

Sorry about the mess.  If you copy/paste it into notepad it will look a lot better.

-DecaysChampion

begin
     exec ('sp_addlinkedserver '+@targetsrv)      /* Adds target server as a linked server */
          create table ##tempt2 (w varchar(35),x varchar(35),y varchar(35),z int)
          set @cmd1='%data access%'                                   /* Checks if 'data access' option is set */
          insert into ##tempt2 exec('sp_helpserver '+@targetsrv)
          if ((select y from ##tempt2 where y like @cmd1) is null)
               begin
                    set @cmd2='sp_serveroption '''+@targetsrv+''',''data access'','+'''true'''
                    set @remaccset=1
                    exec (@cmd2)  /* Configures target server for remote data access */
                    drop table ##tempt2
               end
         
     end

/* Sets default mapping to created login */

set @cmd2='sp_addlinkedsrvlogin '''+@targetsrv+''',''false'',''sa'',''scratchlogin'',''xyz123'''
exec (@cmd2)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

707 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

13 Experts available now in Live!

Get 1:1 Help Now