首页 > 其他 > 详细

Linked Server 添加和删除命令

时间:2016-01-30 17:56:42      阅读:175      评论:0      收藏:0      [点我收藏+]

A linked server allows for access to distributed, heterogeneous queries against OLE DB data sources. After a linked server is created by using sp_addlinkedserver, distributed queries can be run against this server. If the linked server is defined as an instance of SQL Server, remote stored procedures can be executed.

1,添加Linked Server

使用 sys.sp_addlinkedserver 添加Linked Server,除了必须在本地自定义一个Linked Server的Alias,还必须指定 Linked Server的Product name,Provider name,Data Source, location, provider string 和 catalog 参数。The data_source, location, provider_string, and catalog parameters identify the database or databases the linked server points to. If any one of these parameters is NULL, the corresponding OLE DB initialization property is not set.

sp_addlinkedserver [ @server= ] server [ , [ @srvproduct= ] product_name ] 
     [ , [ @provider= ] provider_name ]
     [ , [ @datasrc= ] data_source ] 
     [ , [ @location= ] location ] 
     [ , [ @provstr= ] provider_string ] 
     [ , [ @catalog= ] catalog ] 

Arguments    

[ @server= ] server              

Is the name of the linked server to create. server is sysname, with no default.

[ @srvproduct= ] product_name              

Is the product name of the OLE DB data source to add as a linked server. product_name is nvarchar(128), with a default of NULL. If SQL Server, provider_name, data_source, location, provider_string, and catalog do not have to be specified.

[ @provider= ] provider_name              

Is the unique programmatic identifier (PROGID) of the OLE DB provider that corresponds to this data source. provider_name must be unique for the specified OLE DB provider installed on the current computer. provider_name is nvarchar(128), with a default of NULL; however, if provider_name is omitted, SQLNCLI is used. (Use SQLNCLI and SQL Server will redirect to the latest version of SQL Server Native Client OLE DB Provider.) The OLE DB provider is expected to be registered with the specified PROGID in the registry.

[ @datasrc= ] data_source              

Is the name of the data source as interpreted by the OLE DB provider. data_source is nvarchar(4000). data_source is passed as the DBPROP_INIT_DATASOURCE property to initialize the OLE DB provider.

[ @location= ] location              

Is the location of the database as interpreted by the OLE DB provider. location is nvarchar(4000), with a default of NULL. location is passed as the DBPROP_INIT_LOCATION property to initialize the OLE DB provider.

[ @provstr= ] provider_string              

Is the OLE DB provider-specific connection string that identifies a unique data source. provider_string is nvarchar(4000), with a default of NULL. provstr is either passed to IDataInitialize or set as the DBPROP_INIT_PROVIDERSTRING property to initialize the OLE DB provider.

When the linked server is created against the SQL Server Native Client OLE DB provider, the instance can be specified by using the SERVER keyword as SERVER=servername\instancename to specify a specific instance of SQL Server. servername is the name of the computer on which SQL Server is running, and instancename is the name of the specific instance of SQL Server to which the user will be connected.

技术分享                         Note                      

To access a mirrored database, a connection string must contain the database name. This name is necessary to enable failover attempts by the data access provider. The database can be specified in the @provstr or @catalog parameter. Optionally, the connection string can also supply a failover partner name.

[ @catalog= ] catalog              

Is the catalog to be used when a connection is made to the OLE DB provider. catalog is sysname, with a default of NULL. catalog is passed as the DBPROP_INIT_CATALOG property to initialize the OLE DB provider. When the linked server is defined against an instance of SQL Server, catalog refers to the default database to which the linked server is mapped

Provider 需要安装,可以从SQL Server 的Linked Servers Catalog下查看已经安装的Providers。

技术分享

添加 Linked Server的脚本示例

Script1,添加 Excel作为Linked Server

exec sys.sp_addlinkedserver @server= NExcelDataSource
    ,@srvproduct= NOLE DB Provider for ACE
    ,@provider= NMicrosoft.ACE.OLEDB.12.0
    ,@datasrc=  ND:\excel_data.xlsx  
    ,@location= null
    ,@provstr= null 
    ,@catalog= null

Script2,添加SQL Server 作为Linked Server

--add linked server
exec sys.sp_addlinkedserver @server= NRemoteServerDB
    ,@srvproduct= NRemoteServerDB
    ,@provider= NSQLNCLI
    ,@datasrc=  NServerName\SQLServerInstanceName  
    ,@location= null
    ,@provstr= null 
    ,@catalog= NDB_name

2,使用 sys.servers 或 在GUI中打开 Linked Servers catalog 查看添加的Linked Server

sys.servers Contains a row per linked or remote server registered, and a row for the local server that has server_id = 0.

select *
from sys.servers
where name=NExcelDataSource

技术分享

4,使用 sys.sp_dropserver 删除Linked Server

Syntax

sp_dropserver [ @server = ] server 
     [ , [ @droplogins = ] { droplogins | NULL} ]

[ @droplogins = ] ‘droplogins‘ | NULL

Indicates that related remote and linked server logins for server must also be removed if droplogins is specified. @droplogins is char(10), with a default of NULL.

删除Linked Server,并将login 一起删除

EXEC sys.sp_dropserver @server=NExcelDataSource, @droplogins=droplogins
GO


5,通过 sys.sp_addlinkedsrvlogin 创建Linked Server的Login

当在local Server 上需要访问Linked Server时,Local Server 必须log on 到linked server,sys.sp_addlinkedsrvlogin 就是指定Local Server 登陆Linked Server的Login。

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.

Creates or updates a mapping between a login on the local instance of SQL Server and a security account on a remote server.

sp_addlinkedsrvlogin [ @rmtsrvname = ] rmtsrvname 
     [ , [ @useself = ] TRUE | FALSE | NULL ] 
     [ , [ @locallogin = ] locallogin ] 
     [ , [ @rmtuser = ] rmtuser ] 
     [ , [ @rmtpassword = ] rmtpassword ] 

Arguments                                        

[ @rmtsrvname = ] rmtsrvname              

Is the name of a linked server that the login mapping applies to. rmtsrvname is sysname, with no default.

[ @useself = ] TRUE| ‘FALSE‘ | ‘NULL‘              

Determines whether to connect to rmtsrvname by impersonating local logins or explicitly submitting a login and password. The data type is varchar(8), with a default of TRUE.

A value of TRUE specifies that logins use their own credentials to connect to rmtsrvname, with the rmtuser and rmtpassword arguments being ignored. FALSE specifies that the rmtuser and rmtpassword arguments are used to connect to rmtsrvname for the specified locallogin. If rmtuser and rmtpassword are also set to NULL, no login or password is used to connect to the linked server.

[ @locallogin = ] locallogin              

Is a login on the local server. locallogin is sysname, with a default of NULL. NULL specifies that this entry applies to all local logins that connect to rmtsrvname. If not NULL, locallogin can be a SQL Server login or a Windows login. The Windows login must have been granted access to SQL Server either directly, or through its membership in a Windows group granted access.

[ @rmtuser = ] rmtuser              

Is the remote login used to connect to rmtsrvname when @useself is FALSE. When the remote server is an instance of SQL Server that does not use Windows Authentication, rmtuser is a SQL Server login. rmtuser is sysname, with a default of NULL.

[ @rmtpassword = ] rmtpassword              

Is the password associated with rmtuser. rmtpassword is sysname, with a default of NULL.

Remarks

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.

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.

 

6,Examples to add linked server login

6.1, Connecting all local logins to the linked server by using their own user credentials

EXEC sp_addlinkedsrvlogin Accounts, true;

If there are explicit mappings created for individual logins, they take precedence over any global mappings that may exist for that linked server.

6.2, 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;


7,通过 sys.linked_logins 查看添加的Linked Server Login

Returns a row per linked-server-login mapping, for use by RPC and distributed queries from local server to the corresponding linked server.

 

8,通过 sys.sp_droplinkedsrvlogin 删除Linked Server Login

Removes an existing mapping between a login on the local server running SQL Server and a login on the linked server.

sp_droplinkedsrvlogin [ @rmtsrvname= ] rmtsrvname , 
   [ @locallogin= ] locallogin

[ @rmtsrvname = ] rmtsrvname

Is the name of a linked server that the SQL Server login mapping applies to. rmtsrvname is sysname, with no default. rmtsrvname must already exist.

[ @locallogin = ] locallogin              

Is the SQL Server login on the local server that has a mapping to the linked server rmtsrvname. locallogin is sysname, with no default. A mapping for locallogin to rmtsrvname must already exist. If NULL, the default mapping created by sp_addlinkedserver, which maps all logins on the local server to logins on the linked server, is deleted.

 

示例:Removing the login mapping for an existing user

The following example removes the mapping for the login Mary from the local server to the linked server Accounts. Therefore, login Mary uses the default login mapping.

EXEC sp_droplinkedsrvlogin Accounts, Mary


参考文档:

MSDN:sp_addlinkedsrvlogin

MSDN:sp_addlinkedserver (Transact-SQL)

 

Linked Server 添加和删除命令

原文:http://www.cnblogs.com/ljhdo/p/5171006.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!