[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

sp_addlinkedserver ??

Posted on 2001-07-26
4
Medium Priority
?
1,650 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 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

656 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