Linked servers allow submitting a T-SQL statement on a SQL Server instance, which returns data from other SQL Server instances. A linked server allows joining data from several SQL Server instances using a single T-SQL statement when data exists on multiple databases on different SQL instances. By using a linked server to retrieve data from several SQL instances, the only thing that should be done is to connect to one SQL instance.
链接服务器允许在SQL Server实例上提交T-SQL语句 ,该语句从其他SQL Server实例返回数据。 当数据存在于不同SQL实例上的多个数据库上时,链接服务器允许使用单个T-SQL语句从多个SQL Server实例联接数据。 通过使用链接服务器从多个SQL实例检索数据,唯一要做的就是连接到一个SQL实例。
There are two ways of configuring linked server in SSMS. One way is by using sp_addlinkedserver system stored procedure and another is by using SQL Server Management Studio (SSMS) GUI interface.
在SSMS中有两种配置链接服务器的方法。 一种方法是使用sp_addlinkedserver系统存储过程,另一种方法是使用SQL Server Management Studio (SSMS)GUI界面。
In this article will be explained how to configure a linked server using a SQL Server data source. More information about other data sources can be found on this link.
在本文中,将说明如何使用SQL Server数据源配置链接服务器。 可以在此链接上找到有关其他数据源的更多信息。
To see all created linked servers in SSMS, under Object Explorer, chose the Server Objects folder and expand the Linked Servers folder:
要在SSMS中查看所有已创建的链接服务器,请在“ 对象资源管理器”下 ,选择“ 服务器对象”文件夹并展开“ 链接服务器”文件夹:
To create a linked server in SSMS, right click on the Linked Servers folder and from the context menu select the New Linked Server option:
要在SSMS中创建链接服务器,请右键单击“ 链接服务器”文件夹,然后从上下文菜单中选择“ 新建链接服务器”选项:
The New Linked Server dialog appears:
出现“ 新建链接服务器”对话框:
In this dialog, the name of a linked server and server type must be identified. The linked servers can be defined for different kind of data source if the Other data source radio button is chosen. For the purpose of this article, the SQL Server radio button under the Server type section will be chosen and in the Linked server text box, a name of the server will be entered:
在此对话框中,必须标识链接服务器的名称和服务器类型。 如果选择其他数据源单选按钮,则可以为不同类型的数据源定义链接的服务器。 出于本文的目的,将选择“ 服务器类型”部分下的“ SQL Server”单选按钮,并在“ 链接的服务器”文本框中,输入服务器的名称:
If the SQL Server type is chosen to configure a SQL Server linked server, the name specified in the Linked server text box must be the name of the remote SQL Server.
如果选择了SQL Server类型来配置SQL Server链接服务器,则在“ 链接服务器”文本框中指定的名称必须是远程SQL Server的名称。
If a SQL Server instance is default instance, type the name of the computer that hosts the instance of SQL Server (. WSERVER2012). If the SQL Server is a named instance, type the name of the computer and the name of the instance separated by slash (. WSERVER2012\SQLEXPRESS).
如果SQL Server实例是默认实例,请键入承载SQL Server实例的计算机的名称(例如WSERVER2012) 。 如果SQL Server是命名实例,请键入计算机的名称和以斜杠分隔的实例的名称( 例如WSERVER2012 \ SQLEXPRESS )。
Otherwise the following error may occur when the OK button is pressed:
否则,当按下“ 确定”按钮时,可能会发生以下错误:
To set how a user would authenticate to the WSERVER2012\SQLEXPRESS instance, under the Select a page section on upper left corner of the New Linked Server dialog, select the Security item:
若要设置用户对WSERVER2012 \ SQLEXPRESS实例进行身份验证的方式 ,请在“ 新建链接服务器”对话框左上角的“ 选择页面”部分下,选择“ 安全性”项:
Here, different ways to authenticate the linked server can be set.
在这里,可以设置认证链接服务器的不同方法。
Under the Local server login to remote server login mappings, two ways of local logging to a remote login can be set. One way is to associate a local login with a remote login and other way is to impersonate.
在本地服务器登录到远程服务器登录的映射下,可以设置本地登录到远程登录的两种方式。 一种方法是将本地登录名与远程登录名相关联,另一种方法是模拟。
本地登录 (Local Login)
In the Local Login field, will be listed all the local logins. The local login can be an SQL Server Authentication local login:
在本地登录字段中,将列出所有本地登录。 本地登录名可以是SQL Server身份验证本地登录名:
Or a Windows Authentication login:
或Windows身份验证登录名:
Now, when clicking the OK button on the New Linked Server dialog, the following error message will appear:
现在,在“新建链接服务器”对话框上单击“ 确定”按钮时,将出现以下错误消息:
The login mappings should either be impersonate or have a remote login name.
登录映射应该是模拟的或具有远程登录名。
See the image below:
参见下图:
This happens because the Impersonate check box isn’t checked.
发生这种情况是因为未选中“ 模拟”复选框。
模仿 (Impersonate)
The Impersonate check box when is checked passes the local login credentials to the linked server. For SQL Server Authentication, the same login with the exact credentials must exist on the linked server, otherwise when connected to the server with the SQL Server Authentication, the list of the databases under the Catalogs folder may look like this:
选中“模拟”复选框时,会将本地登录凭据传递给链接服务器。 对于SQL Server身份验证,链接服务器上必须存在具有完全凭据的相同登录名,否则,当使用SQL Server身份验证连接到服务器时,“ 目录”文件夹下的数据库列表可能如下所示:
For Windows logins, the login must be a valid login on the linked server. In order to use impersonation, the delegation between the local server and the linked server must be set.
对于Windows登录名,该登录名必须是链接服务器上的有效登录名。 为了使用模拟,必须设置本地服务器和链接服务器之间的委托 。
Let’s create a linked server using the local Windows login. From the Local Login combo box, choose the local Windows login and check the Impersonate checkbox and press the OK button:
让我们使用本地Windows登录名创建一个链接服务器。 在“ 本地登录”组合框中,选择本地Windows登录并选中“ 模拟”复选框,然后按“确定”按钮:
Under the Catalogs folder, all databases that are located on the linked server will be listed:
在目录文件夹下,将列出位于链接服务器上的所有数据库:
远程用户 (Remote User )
The remote user option allows users from the local SQL server to connect to the linked SQL server even though their credentials aren’t present on the remote server by using the credentials from the user that exists on the remote server. Basically, it allows local logins to connect to a remote server as a different login that must exist on a remote server.
远程用户选项允许来自本地SQL Server的用户连接到链接SQL Server,即使使用远程服务器上存在的用户的凭据也无法在远程服务器上显示其凭据。 基本上,它允许本地登录名作为远程服务器上必须存在的其他登录名连接到远程服务器。
远程密码 (Remote Password)
Specify the password of the remote user.
指定远程用户的密码。
From the Local Login drop down list, choose a local login which should map to a remote login. On the Remote User field, enter the name of the remote user that exists on the remote server and in the Remote Password filed, enter a password of that remote user. Then, press the OK button:
从“ 本地登录名”下拉列表中,选择一个应映射到远程登录名的本地登录名。 在“ 远程用户”字段上,输入远程服务器上存在的远程用户的名称,在“ 远程密码”字段中,输入该远程用户的密码。 然后,按确定按钮:
Now, when connected to the local server using SQL Server Authentication, with Miki or Zivko credentials, under the Catalogs folder, all databases that are available on a remote server for the Nenad remote login will be listed:
现在,当使用带有Miki或Zivko凭据SQL Server身份验证连接到本地服务器时,在目录文件夹下,将列出远程服务器上可用于Nenad远程登录的所有数据库:
Additionally, on the Linked Server dialog, it can be identified how logins that are not set in the Local server login to remote server login mappings list will connect to the linked server, for that there are four options that can be used and they are located under the For a login not defined in the list above, connections will section:
此外,在“链接服务器”对话框上,可以确定在“ 本地服务器登录到远程服务器登录映射”列表中未设置的登录将如何连接到链接服务器,因为可以使用四个选项,并且它们位于对于上面列表中未定义的登录名,“连接”部分如下:
不被制造 (Not be made )
If this radio button is chosen, any users that aren’t identified in the Local server login to remote server login mappings list cannot establish connection to the linked server.
如果选择此单选按钮,则“ 本地服务器登录到远程服务器登录映射”列表中未标识的任何用户都无法建立与链接服务器的连接。
For example, if login with different account (. Ben) that not set in the login mapping list the list of the databases under the Catalogs folder will look like this:
例如,如果使用未在登录映射列表中设置的其他帐户(例如Ben)登录,则Catalogs文件夹下的数据库列表将如下所示:
If you attempt to execute a linked server query:
如果您尝试执行链接服务器查询:
-
-
SELECT * FROM [WSERVER2012\SQLEXPRESS].AdventureWorks2014. e
-
The following result will appear:
将显示以下结果:
Msg 7416, Level 16, State 1, Line 1
Access to the remote server is denied because no login-mapping exists.
消息7416,第16级,状态1,第1行
由于不存在登录映射,因此拒绝访问远程服务器。
在不使用安全上下文的情况下进行 (Be made without using a security context)
The Be made without using a security context option is used for connecting to data sources that do not require any authentication, for example like a text file. When this option is selected for connect to a linked server, will have the same effect as selecting the “Not be made” option.
“ 不使用安全上下文进行创建”选项用于连接到不需要任何身份验证的数据源,例如文本文件。 当选择此选项以连接到链接服务器时,将与选择“不进行”选项具有相同的效果。
If you attempt to execute a linked server query:
如果您尝试执行链接服务器查询:
-
-
SELECT * FROM [WSERVER2012\SQLEXPRESS].AdventureWorks2014. e
-
The following message e may appear:
可能会出现以下消息e:
OLE DB provider “SQLNCLI11” for linked server “WSERVER2012\SQLEXPRESS” returned message “Invalid authorization specification”.
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider “SQLNCLI11” for linked server “WSERVER2012\SQLEXPRESS” reported an error. Authentication failed.
Msg 7303, Level 16, State 1, Line 1
链接服务器“ WSERVER2012 \ SQLEXPRESS”的OLE DB提供程序“ SQLNCLI11”返回了消息“无效的授权规范”。
消息7399,级别16,状态1,第1行
链接服务器“ WSERVER2012 \ SQLEXPRESS”的OLE DB提供程序“ SQLNCLI11”报告错误。 验证失败。
消息7303,第16级,状态1,第1行
Cannot initialize the data source object of OLE DB provider “SQLNCLI11” for linked server “WSERVER2012\SQLEXPRESS”.
无法为链接服务器“ WSERVER2012 \ SQLEXPRESS”初始化OLE DB提供程序“ SQLNCLI11”的数据源对象。
使用登录名的当前安全上下文 (Be made using the login’s current security context)
If this option is chosen, it will pass the current security context of the local login to the remote login. If Windows Authentication is used, the windows credentials will be used to connect to a remote SQL server. If SQL Server Authentication is used, then the local login credentials will be passed to remote SQL Server. Note, to establish connection to remote server successfully, then the user with the exact same credentials must exist on the remote server otherwise when execute a linked server query:
如果选择此选项,它将把本地登录名的当前安全上下文传递给远程登录名。 如果使用Windows身份验证 ,则Windows凭据将用于连接到远程SQL Server。 如果使用SQL Server身份验证 ,则本地登录凭据将传递到远程SQL Server。 请注意,要成功建立与远程服务器的连接,则远程服务器上必须存在具有完全相同凭据的用户,否则在执行链接服务器查询时:
-
-
SELECT * FROM [WSERVER2012\SQLEXPRESS].AdventureWorks2014. e
-
The following message will appear:
将显示以下消息:
Msg 18456, Level 14, State 1, Line 1
Login failed for user ‘Ben’.
讯息18456,第14级,州1,第1行
用户“ Ben”的登录失败。
使用此安全上下文 (Be made using this security context)
The fourth option under the For a login not defined in the list above, connections will section is Be made using this security context. In the Remote login and With password fields, enter the credentials of the SQL Server Authentication login that exist on a remote server, otherwise the following error may occur:
对于上面列表中未定义的登录名,将 使用此安全上下文进行 连接部分下的第四个选项。 在“ 远程登录”和“ 使用密码”字段中,输入远程服务器上存在SQL Server身份验证登录的凭据,否则可能会发生以下错误:
The last item under the Select a page menu is the Server Options item. When selecting this option, the following window will be shown:
“ 选择页面”菜单下的最后一项是“ 服务器选项”项。 选择此选项时,将显示以下窗口:
Here, additional options for linked server can be seen or set.
在这里,可以看到或设置链接服务器的其他选项。
兼容排序规则 (Collation Compatible)
The first option is the Collation Compatible option. This option is used to identify if the linked server has the same collation as the local server. This option should set to True only if is known that the linked server has the same collation as the local, otherwise it should be set to False (default).
第一个选项是“ 排序规则兼容”选项。 此选项用于标识链接服务器是否与本地服务器具有相同的排序规则。 仅当已知链接服务器与本地服务器具有相同的排序规则时,才应将此选项设置为True,否则应将其设置为False (默认值)。
资料存取 (Data Access)
This option is used to allow/deny access to the linked server data. If this option is set to False, the access to remote will be denied. This option is useful to disable access to a remote server temporally. The following message will appear when execute a linked server query and this option is set to False:
此选项用于允许/拒绝访问链接的服务器数据。 如果将此选项设置为False,将拒绝对远程的访问。 此选项对于暂时禁用对远程服务器的访问很有用。 当执行链接服务器查询并且此选项设置为False时,将出现以下消息:
Msg 7411, Level 16, State 1, Line 1
Server ‘WSERVER2012\SQLEXPRESS’ is not configured for DATA ACCESS.
消息7411,第16级,状态1,第1行
未为数据访问配置服务器'WSERVER2012 \ SQLEXPRESS'。
By default, the option is set to True
默认情况下,该选项设置为True
RPC和RCP输出 (RPC and RCP Out)
This RCP (Remote Procedure Call) is used to enable the access to remote procedures to be called from the linked server or to be called to the linked server.
此RCP( 远程过程调用 )用于使对远程过程的访问可以从链接服务器调用或被调用到链接服务器。
If these options are set to False, the following error will appear when some procedures from the linked server are called:
如果将这些选项设置为False,则当调用链接服务器中的某些过程时,将出现以下错误:
Msg 7411, Level 16, State 1, Line 4
Server ‘WSERVER2012\SQLEXPRESS’ is not configured for RPC.
消息7411,第16层,状态1,第4行
未为RPC配置服务器“ WSERVER2012 \ SQLEXPRESS”。
By default, the False value are set for the RPC and RCP Out options
默认情况下,为RPC和RCP Out选项设置为False值
使用远程整理 (Use Remote Collation)
When this option is set to True, the collection of remote columns will be used and collection specified in the Collation Name filed will be used for data source that are not SQL Server data source, but if the option is set to False then the collation for the local server will be used. By default, is set to False.
当此选项设置为True时,将使用远程列的集合 ,并且在归类名称字段中指定的集合将用于不是SQL Server数据源的数据源 ,但是如果该选项设置为False,则对将使用本地服务器。 默认情况下,设置为False。
归类名称 (Collation Name)
If the Use Remote Collation filed set to True, this option is used to specify the collation name of the linked server for the data source that is not SQL Server data source. When chose a collation name, it must be a collation that SQL Server supports.
如果“ 使用远程排序规则”文件设置为True,则此选项用于为不是SQL Server数据源的数据源指定链接服务器的排序规则名称。 选择排序规则名称时,它必须是SQL Server 支持的排序规则。
连接超时 (Connection Timeout)
This option is used to set the maximum time the local server should wait for to get a connection to the linked server SQL Server instance. If 0 (zero) is set, then the server option remote login timeout is used. By default, 10 second is set for this option. Note, the default value for SQL Server 2008 is 20 seconds.
此选项用于设置本地服务器等待与链接服务器SQL Server实例的连接所需的最长时间。 如果设置为0(零),则使用服务器选项远程登录超时 。 默认情况下,此选项设置为10秒。 注意,SQL Server 2008的默认值为20秒。
查询超时 (Query Timeout)
This option is used to set how long, in seconds, a remote process can take before time is out. The default value is 600 second (10 minutes). To disable query timeout put 0 (zero) in this field and the query will wait until it is completed.
此选项用于设置远程进程在超时之前可以花费的时间(以秒为单位)。 默认值为600秒(10分钟)。 要禁用查询超时,请在此字段中输入0(零),查询将等待直到完成。
发行人 (Distributor )
In this option, it can be specified whether the linked server is participating in replication as a distribution Publisher.
在此选项中,可以指定链接服务器是否以分发发布者的身份参与复制。
The Distributor is a database instance, that acts as a store for replication specific data associated with one or more Publishers
分发服务器是一个数据库实例,充当与一个或多个发布服务器关联的复制特定数据的存储
发行人 (Publisher )
In this option, it can be set whether the linked server to be a replication publisher or not. If True, the linked server is a publisher. Otherwise, is not.
在此选项中,可以设置链接服务器是否为复制发布者。 如果为True,则链接的服务器是发布者。 否则,不是。
The Publisher is a database instance, that makes data available to other locations through replication.
发布者是一个数据库实例,它通过复制使数据可用于其他位置。
订户 (Subscriber )
In this option, it can be specified whether the linked server is a replication subscriber or not.
在此选项中,可以指定链接服务器是否是复制订户。
A Subscriber is a database instance, that receives replicated data.
订阅服务器是一个数据库实例,它接收复制的数据。
More information about Distributor, Publisher, Subscriber can be found on the Replication Publishing Model Overview page.
有关Distributor ,Publisher, Subscriber的更多信息,请参见 复制发布模型概述页面。
惰性模式验证 (Lazy schema validation)
This option checks schema changes that have occurred since compilation in the remote tables. If this option is set to False (default state), SQL Server checks changes before the execution of a query and if there are some changes, it recompiles the query. If the Lazy schema validation is set to True, an SQL Server delay schema checks the remote tables until query execution.
此选项检查自远程表中进行编译以来发生的架构更改。 如果此选项设置为False(默认状态),则SQL Server将在执行查询之前检查更改,并且如果存在某些更改,它将重新编译查询。 如果将惰性模式验证设置为True,则SQL Server延迟模式将检查远程表,直到执行查询为止。
启用促销分布式交易 (Enable Promotion of Distributed Transactions)
This option is used to protect the actions of a server-to-server procedure through a Microsoft Distributed Transaction Coordinator (MS DTC) transaction. If this option is set to True calling a remote stored procedure starts a distributed transaction and enlists the transaction with MS DTC.
此选项用于通过Microsoft分布式事务处理协调器 (MS DTC)事务保护服务器到服务器过程的操作。 如果此选项设置为True,则调用远程存储过程将启动分布式事务,并使用MS DTC登记该事务。
Now, when everything is set, click the OK button on the New Linked Server dialog. A newly created linked server will appear under the Linked Server folder.
现在,完成所有设置后,单击“新建链接服务器”对话框上的“确定”按钮。 新创建的链接服务器将出现在“链接服务器”文件夹下。
To test that linked server if it works properly, go right-clicking on that linked server and choose Test Connection:
要测试该链接服务器是否正常运行,请右键单击该链接服务器,然后选择“ 测试连接” :
If a connection with linked server is established successfully, the following info message box will appear:
如果与链接服务器的连接成功建立,将出现以下信息消息框:
Otherwise, an error message will be displayed that shows a problem that prevents connection to be successfully established:
否则,将显示一条错误消息,显示阻止成功建立连接的问题:
使用链接服务器查询数据 (Querying data using a linked server)
Querying data using the linked server is a little bit different then querying data from the local SQL Server. In the normal queries, usually, two part notation is used [Schema].[ObjectName], for example :
使用链接服务器查询数据与从本地SQL Server查询数据有些不同。 在常规查询中,通常使用两部分符号[Schema]。[ObjectName],例如:
-
-
SELECT * FROM e
-
When querying a table from a linked server, the fourth part notation is used . To get data from the Employee table which is located in a database on the linked server, querying code will look like this:
从链接服务器查询表时,使用第四部分表示法 。 要从链接服务器上数据库中的Employee表中获取数据,查询代码将如下所示:
-
-
SELECT * FROM [ZIVKO\SQLEXPRESS2016].[AdventureWorks2014].[HumanResources].[ Employee]
-
删除链接的服务器 (Deleting a linked server)
To delete a linked server, under the Linked Servers folder, right click on the linked server and from the context menu choose the Delete command:
要删除链接服务器,请在“链接服务器”文件夹下,右键单击链接服务器,然后从上下文菜单中选择“删除”命令:
This will open the Delete Object dialog:
这将打开“删除对象”对话框:
Click the OK button and from the message box, choose the Yes button:
单击确定按钮,然后从消息框中选择是按钮:
If everything goes well the linked server will be removed from the Linked Servers folder.
Next articles in this series:如果一切顺利,则链接服务器将从“链接服务器”文件夹中删除。
本系列的下一篇文章:- How to create, configure and drop a SQL Server linked server using Transact-SQL 如何使用Transact-SQL创建,配置和删除SQL Server链接服务器
- How to query Excel data using SQL Server linked servers 如何使用SQL Server链接服务器查询Excel数据
- How to configure a Linked Server using the ODBC driver 如何使用ODBC驱动程序配置链接服务器
- How to create a linked server to an Azure SQL database 如何创建到Azure SQL数据库的链接服务器
翻译自: /how-to-create-and-configure-a-linked-server-in-sql-server-management-studio/