sp_addlinkedsrvlogin (Transact-SQL)
Creates or updates a mapping between a login on the local instance of SQL Server and a security account on a remote server.
When a user logs on to the local server and executes a distributed query that accesses a table on the linked server, the local server must log on to the linked server on behalf of the user to access that table. Use sp_addlinkedsrvlogin to specify the login credentials that the local server uses to log on to the linked server.
Note
|
|---|
|
To create the best query plans when you are using a table on a linked server, the query processor must have data distribution statistics from the linked server. Users that have limited permissions on any columns of the table might not have sufficient permissions to obtain all the useful statistics, and might receive a less efficient query plan and experience poor performance. If the linked server is an instance of SQL Server, to obtain all available statistics, the user must own the table or be a member of the sysadmin fixed server role, the db_owner fixed database role, or the db_ddladmin fixed database role on the linked server. |
A default mapping between all logins on the local server and remote logins on the linked server is automatically created by executing sp_addlinkedserver. The default mapping states that SQL Server uses the user credentials of the local login when connecting to the linked server on behalf of the login. This is equivalent to executing sp_addlinkedsrvlogin with @useself set to true for the linked server, without specifying a local user name. Use sp_addlinkedsrvlogin only to change the default mapping or to add new mappings for specific local logins. To delete the default mapping or any other mapping, use sp_droplinkedsrvlogin.
Instead of having to use sp_addlinkedsrvlogin to create a predetermined login mapping, SQL Server can automatically use the Windows security credentials (Windows login name and password) of a user issuing the query to connect to a linked server when all the following conditions exist:
-
A user is connected to SQL Server by using Windows Authentication Mode.
-
Security account delegation is available on the client and sending server.
-
The provider supports Windows Authentication Mode; for example, SQL Server running on Windows.
Note
|
|---|
|
Delegation does not have to be enabled for single-hop scenarios, but it is required for multiple-hop scenarios. |
After the authentication has been performed by the linked server by using the mappings that are defined by executing sp_addlinkedsrvlogin on the local instance of SQL Server, the permissions on individual objects in the remote database are determined by the linked server, not the local server.
sp_addlinkedsrvlogin cannot be executed from within a user-defined transaction.
A. Connecting all local logins to the linked server by using their own user credentials
The following example creates a mapping to make sure that all logins to the local server connect through to the linked server Accounts by using their own user credentials.
EXEC sp_addlinkedsrvlogin 'Accounts'
Or
EXEC sp_addlinkedsrvlogin 'Accounts', 'true'
Note |
|---|
If there are explicit mappings created for individual logins, they take precedence over any global mappings that may exist for that linked server. |
B. Connecting a specific login to the linked server by using different user credentials
The following example creates a mapping to make sure that the Windows user Domain\Mary connects through to the linked server Accounts by using the login MaryP and password d89q3w4u.
EXEC sp_addlinkedsrvlogin 'Accounts', 'false', 'Domain\Mary', 'MaryP', 'd89q3w4u'
Security Note |
|---|
This example does not use Windows Authentication. Passwords will be transmitted unencrypted. Passwords may be visible in data source definitions and scripts that are saved to disk, in backups, and in log files. Never use an administrator password in this kind of connection. Consult your network administrator for security guidance specific to your environment. |
-- here is sample of create linked server and how retrive data and insert, delete, update
EXEC sp_addlinkedserver
@server='BSIRP',
@srvproduct='',
@provider='SQLNCLI',
@datasrc='jayant_PROD_2005'
EXEC sp_addlinkedsrvlogin 'BSIRP', 'True', 'jayant.com\jayant.das', 'Password'
--select Query
SELECT * FROM OPENQUERY (bsirp, 'SELECT * FROM a')
--Insert query
INSERT INTO bsirp.master.dbo.a values (1,'jayant')
--update Query
UPDATE bsirp.master.dbo.a set a=25
--delete query
delete from bsirp.master.dbo.a where a=25
or
DELETE OPENQUERY (bsirp, 'SELECT * FROM a') WHERE a = 25
Thanks & Regards
jayant Dass
jayant.dass@gmail.com
9313406257
- 6/7/2011
- Jayant dass
-- here is sample of create linked server and how retrive data and insert, delete, update
EXEC sp_addlinkedserver
@server='BSIRP',
@srvproduct='',
@provider='SQLNCLI',
@datasrc='jayant_PROD_2005'
EXEC sp_addlinkedsrvlogin 'BSIRP', 'True', 'jayant.com\jayant.das', 'Password'
--select Query
SELECT * FROM OPENQUERY (bsirp, 'SELECT * FROM a')
--Insert query
INSERT INTO bsirp.master.dbo.a values (1,'jayant')
--update Query
UPDATE bsirp.master.dbo.a set a=25
--delete query
delete from bsirp.master.dbo.a where a=25
or
DELETE OPENQUERY (bsirp, 'SELECT * FROM a') WHERE a = 25
Thanks & Regards
jayant Dass
jayant.dass@gmail.com
9313406257
- 6/7/2011
- Jayant dass
Does any one face the issue same as me:
On windows XP SP3 + SQL Server 2008 R2 Express Edition + ODBC (IBM Data Server Driver V9.7 Update 3) (all 32 bit versions)
running:
EXEC master.dbo.sp_addlinkedserver @server = N'AAA', @srvproduct=N'AAA', @provider=N'MSDASQL', @datasrc=N'ODBC_SYSTEM_DNS_NAME'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'AAA',@useself=N'False',@locallogin=NULL,@rmtuser=NULL,@rmtpassword=NULL
The user name, password are store in the ODBC settings, ODBC can connect the database successfully. linked server connected successfully.
However,
When promote to server:
Windows 2008 R2 + SQL 2008 R2 Developer Edition + ODBC (IBM Data Server Driver V9.7 Update 3), all 64 bit
ODBC set up (on 64 bit), test connection, successfully.
the above statement can run successfully.However, when calling the linked server, after some time, server response:
Msg 15281, Level 16, State 1, Line 1SQL server services STOPPED UNEXPECTEDLY!!!!!!
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.
What's happen??
Any body can help???
Originally, I wish to post on MSDN SQL server session, but it shows me I don't have access right.
Regards,
A very disappointed SQL server developer.
- 3/22/2011
- skho