Distributed1:Linked Server 添加和删除

本文涉及的产品
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
云数据库 RDS SQL Server,基础系列 2核4GB
简介:

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 参数。

复制代码
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.

[ @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. The OLE DB provider is expected to be registered with the specified PROGID in the registry.

[@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。

添加SQL Server 作为Linked Server,将@srvproduct 设置为 N'' 字符。

复制代码
--add linked server
exec sys.sp_addlinkedserver @server= N'RemoteServerAlias'
,@srvproduct= N''
,@provider= N'SQLNCLI' ,@datasrc= N'RemoteServerName\SQLServerInstanceName' ,@location= null ,@provstr= null ,@catalog= N'Remote_DB_Name'
复制代码

2,使用 sys.servers 查看添加的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.

When you create a linked or remote server, SQL Server creates a default login mapping to the public server role. This means that by default, all logins can view all linked and remote servers. To restrict visibility to these servers, remove the default login mapping by executing sp_droplinkedsrvlogin and specifying NULL for the locallogin parameter.

select *
from sys.servers
where name=N'ExcelDataSource'

3,使用 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=N'ExcelDataSource', @droplogins='droplogins'
GO


参考doc:

MSDN:sp_addlinkedserver (Transact-SQL)

 

作者悦光阴
本文版权归作者和博客园所有,欢迎转载,但未经作者同意,必须保留此段声明,且在文章页面醒目位置显示原文连接,否则保留追究法律责任的权利。
分类: 数据库管理





本文转自悦光阴博客园博客,原文链接:http://www.cnblogs.com/ljhdo/p/5171006.html,如需转载请自行联系原作者
相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
2月前
|
SQL 安全 Oracle
SQL Server 链接服务器(Linked Servers)
【9月更文挑战第12天】SQL Server 的链接服务器功能强大,可让你在一个实例中访问其他数据源,包括其他 SQL Server 实例、Oracle 数据库等。它支持数据集成、分布式查询和数据同步,无需复制数据。创建链接服务器需在 SQL Server Management Studio 中进行,并配置名称、类型及安全性。使用时需注意权限、性能和安全性问题,确保系统稳定和数据安全。