Solved

sp_addlinkedserver ??

Posted on 2001-07-26
4
1,619 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

691 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