Solved

sp_addlinkedserver ??

Posted on 2001-07-26
4
1,602 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

863 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

25 Experts available now in Live!

Get 1:1 Help Now