transact-sql

Linked servers allow getting data from a different SQL Server instance using single T-SQL statement.

链接服务器允许使用单个T-SQL语句从其他SQL Server实例获取数据。

This article will explain how to create, configure and drop a SQL Server linked server using system stored procedures.

本文将说明如何使用系统存储过程创建,配置和删除SQL Server链接服务器。

Also, there is a way to create and configure a linked server to another SQL Server instance using SQL Server Management Studio (SSMS). To see how to configure a linked server by using the SSMS GUI, see the article How to create and configure a linked server in SQL Server Management Studio.

另外,还有一种使用SQL Server Management Studio (SSMS)创建和配置链接服务器到另一个SQL Server实例的方法。 若要查看如何使用SSMS GUI配置链接服务器,请参阅文章如何在SQL Server Management Studio中创建和配置链接服务器 。

创建一个SQL Server链接服务器 (Creating a SQL Server linked server)

To create a linked server, use the sp_addlinkedserver procedure.

若要创建链接服务器,请使用sp_addlinkedserver过程。

Executing the following code:

执行以下代码:


USE master;
GO
EXEC sp_addlinkedserver   N'TestServer',N'SQL Server';
GO

This will create a linked server with the name TestServer under the Linked Servers folder:

这将在Linked Servers文件夹下创建一个名称为TestServer链接服务器

In order to get databases from the remote SQL server the exact name of the SQL Server need be entered.

为了从远程SQL Server获取数据库,需要输入SQL Server的确切名称。

For default instance of the SQL Server type the name of the computer that hosts the instance of SQL Server (e.g. WSERVER2012):

对于SQL Server的默认实例,键入承载SQL Server实例的计算机的名称(例如WSERVER2012)


USE master;
GO
EXEC sp_addlinkedserver   N'WSERVER2012',  N'SQL Server';
GO

For the SQL Server name instance, type the name of the computer and the name of the instance separated by slash (e.g. WSERVER2012\SQLEXPRESS):

对于SQL Server名称实例,键入计算机的名称和该实例的名称,并用斜杠分隔( 例如WSERVER2012 \ SQLEXPRESS ):


USE master;
GO
EXEC sp_addlinkedserver   N'WSERVER2012\SQLEXPRESS',  N'SQL Server';
GO

The results will be something like this:

结果将是这样的:

When creating a linked server like this by default, it will pass the current security context of the local login to the remote login.

默认情况下,在创建像这样的链接服务器时,它将把本地登录名的当前安全上下文传递给远程登录名。

Window or the local login credentials, will be used if Windows or SQL Server authentication is selected, respectively. In order to successfully connect to a remote SQL Server, the exact user with the same username and password must exist on the remote server:

如果分别选择了Windows或SQL Server身份验证,则将使用Window或本地登录凭据。 为了成功连接到远程SQL Server,远程服务器上必须存在具有相同用户名和密码的确切用户:

Besides defining a parameter for the name of the linked server and the product name, while using sp_addlinkedserver, other parameters such are: provider name, data source, location, provider string, catalog can be set:

另外,限定用于连接服务器产品名称名称的参数,而使用的sp_addlinkedserver,其它参数,例如有: 提供者的名称数据源 的位置提供程序字符串目录可设置为:


USE master;
GO
EXEC sp_addlinkedserver@server = 'server',@srvproduct = 'product name',@provider = 'provider name',@datasrc = 'data source',@location = 'location',@provstr = 'provider string',@catalog = 'catalog';
GO

@server is a name of the linked server:

@server是链接服务器的名称:


USE master;
GO
EXEC sp_addlinkedserver   @server = N'WSERVER2012\SQLEXPRESS',

@srvproduct is the product name of the OLE DB data source you’re adding as a linked server (e. g ‘SQL Server’, ‘Microsoft.Jet.OLEDB.4.0’,’ Oracle’ etc.). The @srvproduct parameter is nvarchar(128) data type, by default this parameter is NULL. When using SQL Server as the product name, the provider name, data source, location, provider string, and catalog parameters do not have to be specified:

@srvproduct是您要添加为链接服务器的OLE DB数据源的产品名称(例如,“ SQL Server”,“ Microsoft.Jet.OLEDB.4.0”,“ Oracle”等)。 @srvproduct参数为nvarchar(128) 数据类型,默认情况下此参数为NULL。 当使用SQL Server作为产品名称时 ,不必指定提供程序名称数据源位置提供程序字符串目录参数:


USE master;
GO
EXEC sp_addlinkedserver   @server = N'WSERVER2012\SQLEXPRESS',  @srvproduct = N'SQL Server';
GO

@provider is the OLE DB provider name and must be unique for the specified OLE DB provider installed on the current computer:

@provider是OLE DB 提供程序名称 ,对于在当前计算机上安装的指定OLE DB提供程序必须是唯一的:

The @provider parameter is nvarchar(128) data type, by default this parameter is NULL. If the provider name parameter is omitted, SQLNCLI (SQL Server Native Client) is used:

@provider参数为nvarchar(128)数据类型,默认情况下此参数为NULL。 如果省略提供程序名称参数,则使用SQLNCLI (SQL Server本机客户端):


EXEC sp_addlinkedserver     @server=N'WSERVER2012\SQLEXPRESS',   @srvproduct=N'',  @provider=N'SQLNCLI',  @datasrc=N'WSERVER2012\SQLEXPRESS';

@datasrc is the name of the data source as interpreted by the OLE DB provider. If you are connecting to an instance of SQL Server, provide the instance name:

@datasrc是由OLE DB提供程序解释的数据源的名称。 如果要连接到SQL Server的实例,请提供实例名称:


EXEC sp_addlinkedserver     @server=N'OtherSQLServer',   @srvproduct=N'',  @provider=N'SQLNCLI',  @datasrc=N'WSERVER2012\SQLEXPRESS';

When creating a linked server like this, a name of the server in the @server parameter can be anything, does not need to be a name of a remote server:

当创建这样的链接服务器时,@ server参数中的服务器名称可以是任何名称,而不必是远程服务器的名称:

If you are using another data source (e.g. Excel file), provide the full path to the Microsoft Excel file in the @datasrc parameter:

如果使用其他数据源(例如Excel文件),请在@datasrc参数中提供Microsoft Excel文件的完整路径:


EXEC sp_addlinkedserver@server = 'ExcelData',@srvproduct = 'Microsoft.Jet.OLEDB.4.0',@provider = 'Microsoft.Jet.OLEDB.4.0',@datasrc = 'C:LinkedExcelData.xls';

@location is the location of the database (source), if required by the OLE DB provider. The @location parameter is nvarchar(4000) data type, by default this parameter is NULL.

@location是数据库(源)的位置,如果需要的话通过OLE DB提供。 @location参数为nvarchar(4000)数据类型,默认情况下此参数为NULL。


EXEC sp_addlinkedserver     @server=N'OtherSQLServer',   @srvproduct=N'',  @provider=N'SQLNCLI',  @datasrc=N'WSERVER2012\SQLEXPRESS',@location= NULL;

@provstr is the OLEDB string that identifies the source. The @provstr parameter is nvarchar(4000) data type, by default this parameter is NULL. If the linked server created using SQLNCLI then in the @provstr parameter can be specified the instance of SQL Server to which the user will be connected:

@provstr是标识源的OLEDB字符串。 @provstr参数为nvarchar(4000)数据类型,默认情况下此参数为NULL。 如果使用SQLNCLI创建了链接服务器,则可以在@provstr参数中指定用户将连接到SQL Server实例:


EXEC sp_addlinkedserver     @server=N'WSERVER2012\SQLEXPRESS',   @srvproduct=N'',  @provider=N'SQLNCLI',  @datasrc=N'WSERVER2012\SQLEXPRESS',@provstr = N'SERVER=WSERVER2012\SQLEXPRESS';

@catalog is the name of the catalog or the name of the database for the SQL Server, by default this parameter is NULL. When the linked server is defined against an instance of SQL Server, the catalog refers to the default database to which the linked server is mapped.

@catalog是SQL Server的目录名称或数据库名称,默认情况下,此参数为NULL。 当针对SQL Server实例定义链接服务器时,目录将引用链接服务器映射到的默认数据库。


EXEC sp_addlinkedserver     @server=N'WSERVER2012\SQLEXPRESS',   @srvproduct=N'',  @provider=N'SQLNCLI',  @datasrc=N'WSERVER2012\SQLEXPRESS',@provstr = N'SERVER=WSERVER2012\SQLEXPRESS',@catalog  = N'AdventureWorks2014';

More about sp_addlinkedserver parameters can be found on this link.

在此链接上可以找到有关sp_addlinkedserver参数的更多信息。

Create or update a mapping between a login on the local SQL Server instance and account on a remote SQL Server

创建或更新本地SQL Server实例上的登录名与远程SQL Server上的帐户之间的映射

After creating a linked server using sp_addlinkedserver procedure:

使用sp_addlinkedserver过程创建链接服务器后:


USE master;
GO
EXEC sp_addlinkedserver   @server = N'WSERVER2012\SQLEXPRESS',  @srvproduct = N'SQL Server';
GO

Use the sp_addlinkedsrvlogin to create or modified a mapping a local logins to the remote logins using the following syntax:

使用sp_addlinkedsrvlogin使用以下语法创建或修改将本地登录名映射到远程登录名的映射:


EXEC sp_addlinkedsrvlogin@rmtsrvname = N'remote server name',@useself = 'useseif',@locallogin = N'local login',@rmtuser = N'remote user',@rmtpassword = N'remote password';

@rmtsrvname is the name of the remote server (linked server) which login mapping applies to:

@rmtsrvname是登录映射适用于的远程服务器(链接服务器)的名称:


EXEC sp_addlinkedsrvlogin@rmtsrvname = N'WSERVER2012\SQLEXPRESS';

@useself is used to determine how connection with remote server will be established. There are three states ‘True’, ‘False’, ’Null’.

@useself用于确定如何建立与远程服务器的连接。 有三个状态'True''False''Null'


EXEC sp_addlinkedsrvlogin@rmtsrvname = N'WSERVER2012\SQLEXPRESS',@useself =N'True';

If the ‘True’ state is used, then the current security context of the local login will be passed to the remote login.

如果使用“ True”状态,则本地登录名的当前安全上下文将传递给远程登录名。

Window or the local login credentials, will be used if Windows or SQL Server authentication is selected, respectively

如果分别选择了Windows或SQL Server身份验证,则将使用Window或本地登录凭据。

When executing the above SQL code, the following results will appear in the Catalogs folder for SQL Server Authentication and local user ‘Jack’:

执行上述SQL代码时,以下结果将显示在SQL Server身份验证和本地用户“ Jack”Catalogs文件夹中:

Note: To successfully connect to a remote server the user with the exact same username and password must exist on the remote server.

注意 :要成功连接到远程服务器,远程服务器上必须存在具有完全相同的用户名和密码的用户。

In case when connecting to local SQL Server using SQL Server Authentication with local user login credentials that doesn’t exist on the remote server (e. g. user ‘Ben’), the results under the Catalog folder may look like this:

如果使用远程服务器上不存在的本地用户登录凭据(例如用户“ Ben”)使用SQL Server身份验证连接到本地SQL Server时,目录文件夹下的结果可能如下所示:

The same security context is used when creating a linked server with the sp_addlinkedserver procedure:

使用sp_addlinkedserver过程创建链接服务器时,将使用相同的安全上下文:


USE master;
GO
EXEC sp_addlinkedserver   N'WSERVER2012\SQLEXPRESS',  N'SQL Server';
GO

The @useself parameter is varchar(8) data type, by default this parameter is set to ‘True’.

@useself参数是varchar(8)数据类型,默认情况下此参数设置为'True'。

If the ‘False’ value is set to @useself parameter, then the local server login use the arguments specified in the @rmtuser and @rmtpassword parameters to log in to the linked server:

如果将'False'值设置为@useself参数,则本地服务器登录名将使用@rmtuser@rmtpassword参数中指定的参数登录到链接服务器:


EXEC sp_addlinkedsrvlogin@rmtsrvname = N'WSERVER2012\SQLEXPRESS',@useself ='false', @locallogin = N'Ben',@rmtuser = N'Jack',@rmtpassword = N'Jack';

Now, when local user ‘Ben’ login to local SQL Server, under the Catalogs folder, all databases that are available on a remote server for the ‘Jack’ remote login will be listed:

现在,当本地用户“ Ben”登录到本地SQL Server时,在Catalogs文件夹下,将列出远程服务器上可用于“ Jack”远程登录的所有数据库:

For a linked server that doesn’t require username and password (e.g. Microsoft Access), these arguments can be set to Null.

对于不需要用户名和密码的链接服务器(例如Microsoft Access),可以将这些参数设置为Null

@locallogin is a local login, by default this parameter is set to Null. You can designate either an individual login or all local logins. To specify that all local logins be affected pass a Null to this parameter. If not Null, then in the @locallogin parameter can be a SQL Server login or a Windows login.

@locallogin是本地登录名,默认情况下此参数设置为Null。 您可以指定单个登录名或所有本地登录名。 要指定所有本地登录均受影响,请将Null传递给此参数。 如果不是Null,则@locallogin参数中可以是SQL Server登录名或Windows登录名。

@rmtuser is the username of the remote login used to connect to a remote server if @useself is set to ‘False’.

@rmtuser是用于如果@useself被设置为“假”连接到一个远程服务器的远程登录的用户名。

@rmtpassword is the password of the remote user used to connect to a remote server if @useself is set to ‘False’.

@rmtpassword是用于如果@useself被设置为“假”连接到一个远程服务器的远程用户的密码。

设置SQL Server链接服务器选项 (Set a SQL Server linked server options)

Additionally, you can configure a linked server by using sp_serveroption procedure. Here you can set the vireos of options related to a linked server like: collation compatible, collation name, connect timeout, data access, lazy schema validation, rpc, rpc out, use remote collation etc., by executing the following code:

此外,可以使用sp_serveroption过程配置链接服务器。 在这里,您可以通过执行以下代码来设置与链接服务器相关的选项的选项:排序规则兼容,排序规则名称,连接超时,数据访问,惰性模式验证,rpc,rpc输出,使用远程排序规则等:


EXEC sp_serveroption@server = 'server',@optname = 'option_name',@optvalue = 'option_value';

@server is the name of the linked server for which to set the option.

@server是要为其设置选项的链接服务器的名称。

@optname the name of the option to be configured.

@optname要配置的选项的名称。

@optvalue the value of the option. Valid values are True or On to enable and False or Off to disable the option, a non-negative integer for the connection timeout and query time out options, or collation name for the collation name option.

@optvalue选项的值。 有效值为TrueOn启用,而FalseOff禁用该选项,连接超时和查询超时选项的非负整数,或collat​​ion name选项的归类名称。

The following example, enables remote procedure calls to the ‘WSERVER2012\SQLEXPRESS’ linked server:

下面的示例启用对“ WSERVER2012 \ SQLEXPRESS”链接服务器的远程过程调用:


EXEC sp_serveroption@server = ' WSERVER2012\SQLEXPRESS',@optname = 'rpc out',@optvalue = 'True';

To allow/deny a linked server for distributed query access, use the @optname ‘data access’. The following example, disable distributed query to ‘WSERVER2012\SQLEXPRESS’ linked server

要允许/拒绝链接服务器进行分布式查询访问,请使用@optname'data access' 。 下面的示例禁用对“ WSERVER2012 \ SQLEXPRESS”链接服务器的分布式查询


EXEC sp_serveroption@server = 'WSERVER2012\SQLEXPRESS',@optname = 'data access',@optvalue = 'False';

Now when execute a linked server query:

现在,当执行链接服务器查询时:


SELECT * FROM [WSERVER2012\SQLEXPRESS].AdventureWorks2014.HumanResources.Employee e

The following error message will appear:

将出现以下错误消息:

Msg 7411, Level 16, State 1, Line 1
Server ‘WSERVER2012\SQLEXPRESS’ is not configured for DATA ACCESS.

消息7411,第16级,状态1,第1行
未为数据访问配置服务器'WSERVER2012 \ SQLEXPRESS'。

To set the query timeout use the @optname ‘query timeout’. Here can be set how long, in seconds, a remote process can take before time is out. From the example below query timeout is set to 120 seconds:

要设置查询超时,请使用@optname'查询超时'。 可以在此处设置远程进程在超时之前需要花费的时间(以秒为单位)。 在下面的示例中,查询超时设置为120秒:


EXEC sp_serveroption@server = 'WSERVER2012\SQLEXPRESS',@optname = 'query timeout',@optvalue = 120;

More about options under the sp_serveroption procedure can be found on this link.

在此链接上可以找到有关sp_serveroption过程中的选项的更多信息。

获取有关SQL Server链接服务器的信息(设置) (Getting information(settings) about a SQL Server linked server)

To see information about linked servers and the referenced data sources, SQL Server Management Studio or the system stored procedures can be used.

若要查看有关链接服务器和引用的数据源的信息,可以使用SQL Server Management Studio系统存储过程

Below is the list of the most commonly used system stored procedures.

以下是最常用的系统存储过程的列表。

To see all about login mappings defined against a specific linked server, run the sp_helplinkedsrvlogin procedure.

若要查看有关针对特定链接服务器定义的登录映射的所有信息,请运行sp_helplinkedsrvlogin过程。


EXEC sp_helplinkedsrvlogin@rmtsrvname = N'remote server name',@locallogin = N'local login'

@rmtsrvname the name of the linked server for which want to see login setting displayed, by default this parameter is Null

@rmtsrvname要为其显示登录设置的链接服务器的名称,默认情况下此参数为Null

@locallogin the local login that has a mapping to the linked server, by default this parameter is set to Null

@locallogin具有到链接服务器的映射的本地登录名,默认情况下此参数设置为Null

In the example below are displayed all login mappings for all linked servers defined on the local machine:

在下面的示例中,显示了本地计算机上定义的所有链接服务器的所有登录映射:


EXEC sp_helplinkedsrvlogin;
GO

The result will be something like this:

结果将是这样的:

Linked Server Local Login Is Self Mapping Remote Login
WSERVER2012\CTP21 Null 1 Null
WSERVER2012\SQLEXPRESS Null 1 Null
WSERVER2012\SQLEXPRESS WSERVER2012\Zivko 1 Null
WSERVER2012\SQLEXPRESS Ben 0 Jack
链接服务器 本地登录 是自映射 远程登录
WSERVER2012 \ CTP21 空值 1个 空值
WSERVER2012 \ SQLEXPRESS 空值 1个 空值
WSERVER2012 \ SQLEXPRESS WSERVER2012 \ Zivko 1个 空值
WSERVER2012 \ SQLEXPRESS 0 插口

Linked Server is the name of the linked server.

链接服务器链接服务器的名称。

Local Login displays which local users are affected by this mapping. If Null is set, this mapping applies to all users who don’t have specific mappings.

本地登录显示该映射影响哪些本地用户。 如果设置为Null,则此映射适用于所有没有特定映射的用户。

Is Self Mapping, if it displays 1, the local login and password are used when connecting to a linked server. If 0 displayed, then the value from the Remote Login column are used for connecting to a linked server. Note, remote password is not displayed for security reason.

是Self Mapping ,如果显示1,则在连接到链接服务器时使用本地登录名和密码。 如果显示0,则使用“ 远程登录”列中的值连接到链接的服务器。 注意,出于安全原因,不会显示远程密码。

To display all login mappings for a specific linked server, execute the following:

要显示特定链接服务器的所有登录映射,请执行以下操作:


EXEC sp_helplinkedsrvlogin@rmtsrvname = N'WSERVER2012\SQLEXPRESS';
Linked Server Local Login Is Self Mapping Remote Login
WSERVER2012\SQLEXPRESS Null 1 Null
WSERVER2012\SQLEXPRESS WSERVER2012\Zivko 1 Null
WSERVER2012\SQLEXPRESS Ben 0 Jack
链接服务器 本地登录 是自映射 远程登录
WSERVER2012 \ SQLEXPRESS 空值 1个 空值
WSERVER2012 \ SQLEXPRESS WSERVER2012 \ Zivko 1个 空值
WSERVER2012 \ SQLEXPRESS 0 插口

To displays all login mappings for a local login, execute the following:

要显示本地登录的所有登录映射,请执行以下操作:


EXEC sp_helplinkedsrvlogin@rmtsrvname = NULL,@locallogin = N'Ben';
Linked Server Local Login Is Self Mapping Remote Login
WSERVER2012\SQLEXPRESS Ben 0 Jack
链接服务器 本地登录 是自映射 远程登录
WSERVER2012 \ SQLEXPRESS 0 插口

sp_linkedservers – this procedure returns a list of linked servers defined on the local server

sp_linkedservers –此过程返回在本地服务器上定义的链接服务器的列表


EXEC sp_linkedservers

sp_catalogs – this procedure displays a list of catalogs for the specified linked server

sp_catalogs –此过程显示指定链接服务器的目录列表


EXEC sp_catalogs@server_name = N'WSERVER2012\SQLEXPRESS'

Basically, this displays a list of available databases for the chosen linked server:

基本上,这将显示所选链接服务器的可用数据库列表:

sp_tables_ex – shows the table information about the tables for the specified linked server:

sp_tables_ex –显示有关指定链接服务器的表的表信息:


EXEC sp_tables_ex@table_server = N'WSERVER2012\SQLEXPRESS',@table_schema = N'HumanResources',@table_catalog = N'AdventureWorks2014',@table_type = N'Table';

sp_columns_ex – shows column information for all columns, or a specified column in a remote table

sp_columns_ex –显示所有列或远程表中指定列的列信息


EXEC sp_columns_ex@table_server = N'WSERVER2012\SQLEXPRESS',@table_name = N'Address',@table_schema = N'Person',@table_catalog = N'AdventureWorks2014',@column_name = N'City';

sp_table_privileges_ex – displays table permissions for a linked server table.

sp_table_privileges_ex –显示链接服务器表的表权限。


EXEC sp_table_privileges_ex@table_server = N'WSERVER2012\SQLEXPRESS',@table_name = N'Address',@table_schema = N'Person',@table_catalog = N'AdventureWorks2014',@fUsePattern = 0

sp_column_privileges_ex – displays a list privileges for columns on a specific table for a linked server

sp_column_privileges_ex –显示链接服务器的特定表上的列的列表特权


EXEC sp_column_privileges_ex@table_server = N'WSERVER2012\SQLEXPRESS',@table_name = N'Address',@table_schema = N'Person',@table_catalog = N'AdventureWorks2014',@column_name = N'City';

sp_testlinkedserver this procedure tests the connection to a linked server:

sp_testlinkedserver此过程测试到链接服务器的连接:


EXEC sp_testlinkedserver @servername =N'Server name'

If the test fails, it returns an error message with the reason of the failure.

如果测试失败,则返回一条错误消息,说明失败的原因。

Below is an example in which creates a linked server named ‘WSERVER2012’ and then tests the connection:

下面是一个示例,其中创建一个名为“ WSERVER2012”的链接服务器,然后测试连接:


USE master;
GO
EXEC sp_addlinkedserver   @server=N'WSERVER2012',  @srvproduct = N'SQL Server';
GO

Test a linked server connection:

测试链接服务器的连接:


EXEC sp_testlinkedserver@servername = N'WSERVER2012';
GO

Reason for the failure:

失败原因:

OLE DB provider “SQLNCLI11” for linked server “WSERVER2012” returned message “Login timeout expired”.
OLE DB provider “SQLNCLI11” for linked server “WSERVER2012” returned message “A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.”.
Msg 2, Level 16, State 1, Line 6
Named Pipes Provider: Could not open a connection to SQL Server [2].

链接服务器“ WSERVER2012”的OLE DB提供程序“ SQLNCLI11”返回消息“登录超时已过期”。
链接服务器“ WSERVER2012”的OLE DB提供程序“ SQLNCLI11”返回消息“建立与SQL Server的连接时发生了与网络相关或特定于实例的错误。 找不到服务器或无法访问服务器。 检查实例名称是否正确,以及是否将SQL Server配置为允许远程连接。 有关更多信息,请参见SQL Server联机丛书。”。
消息2,级别16,州1,第6行
命名管道提供程序:无法打开与SQL Server [2]的连接。

To see the settings for a linked server from SQL Server Management Studio, right click on linked server under the Linked Server folder and chose Properties command:

要从SQL Server Management Studio查看链接服务器的设置,请右键单击“ 链接服务器”文件夹下的链接服务器,然后选择“ 属性”命令:

This will open the Linked Server Properties dialog:

这将打开“ 链接服务器属性”对话框:

On the top right side of the dialog, there are three tabs (General, Security, Server Options) on which can be seen all settings for the chosen a linked server.

在对话框的右上方,有三个选项卡(“ 常规” ,“ 安全性” ,“ 服务器选项” ),可以在其中看到所选链接服务器的所有设置。

To show all setting s for a chosen linked server in a query editor, right click on the name of a linked server under the Linked Servers folder, from the context menu chose Script Linked Server as -> Create to -> New Query Editor Window command:

要在查询编辑器中显示所选链接服务器的所有设置,请右键单击“ 链接服务器”文件夹下的链接服务器的名称,从上下文菜单中选择“ 脚本链接服务器”为->创建至->新建查询编辑器窗口命令:

This will create a .sql script with all settings that contains the chosen linked server:

这将创建一个.sql脚本,其中包含包含所选链接服务器的所有设置:


USE [master]
GO/****** Object:  LinkedServer [WSERVER2012\SQLEXPRESS]    Script Date: 6/27/2017 1:56:57 PM ******/
EXEC master.dbo.sp_addlinkedserver @server = N'WSERVER2012\SQLEXPRESS', @srvproduct=N'SQL Server'/* For security reasons the linked server remote logins password is changed with ######## */
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'WSERVER2012\SQLEXPRESS',@useself=N'True',@locallogin=NULL,@rmtuser=NULL,@rmtpassword=NULL
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'WSERVER2012\SQLEXPRESS',@useself=N'False',@locallogin=N'Ben',@rmtuser=N'Jack',@rmtpassword='########'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'WSERVER2012\SQLEXPRESS',@useself=N'True',@locallogin=N'WSERVER2012\Zivko',@rmtuser=NULL,@rmtpassword=NULL
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'collation compatible', @optvalue=N'false'
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'data access', @optvalue=N'false'
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'dist', @optvalue=N'false'
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'pub', @optvalue=N'false'
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'rpc', @optvalue=N'true'
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'rpc out', @optvalue=N'true'
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'sub', @optvalue=N'false'
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'connect timeout', @optvalue=N'0'
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'collation name', @optvalue=null
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'lazy schema validation', @optvalue=N'false'
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'query timeout', @optvalue=N'120'
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'use remote collation', @optvalue=N'true'
GOEXEC master.dbo.sp_serveroption @server=N'WSERVER2012\SQLEXPRESS', @optname=N'remote proc transaction promotion', @optvalue=N'true'
GO

删除映射 (Delete a mapping)

To delete an existing mapping between a local login and login on a linked server use the sp_droplinkedsrvlogin system stored procedure.

若要删除本地登录名和链接服务器上的登录名之间的现有映射,请使用sp_droplinkedsrvlogin系统存储过程。


EXEC sp_droplinkedsrvlogin@rmtsrvname = N'Remote server',@locallogin = N'Local login’;

@rmtsrvname is the name of a linked server that the login mapping applies to. A linked server must exist under the Linked Server folder, otherwise the following error message my appears when execute the sp_droplinkedsrvlogin procedure:

@rmtsrvname是登录映射应用于的链接服务器的名称。 链接服务器文件夹下必须存在链接服务器,否则在执行sp_droplinkedsrvlogin过程时会出现以下错误消息:

Msg 15015, Level 16, State 1, Procedure sp_droplinkedsrvlogin, Line 32 [Batch Start Line 0]
The server ‘WSERVER2012’ does not exist. Use sp_helpserver to show available servers.

消息15015,级别16,状态1,过程sp_droplinkedsrvlogin,第32行[Batch Start Line 0]
服务器“ WSERVER2012”不存在。 使用sp_helpserver显示可用的服务器。

@locallogin is the local login that has mapping to a linked server. A mapping for local login to a remote server must exist, otherwise the following error message may appear:

@locallogin是已映射到链接服务器的本地登录名。 本地登录到远程服务器的映射必须存在,否则可能会出现以下错误消息:

Msg 15007, Level 16, State 1, Procedure sp_droplinkedsrvlogin, Line 51 [Batch Start Line 0]
‘Jana’ is not a valid login or you do not have permission.

消息15007,级别16,状态1,过程sp_droplinkedsrvlogin,第51行[批处理开始第0行]
“ Jana”不是有效的登录名,或者您没有权限。

If the @locallogin property is set to Null, the default mapping which is created when a linked server is created by sp_addlinkedserver procedure will be removed.

如果@locallogin属性设置为Null ,则将删除由sp_addlinkedserver过程创建链接服务器时创建的默认映射。

The following example will remove the login mapping for the ‘Ben’ local user to the ‘WSERVER2012\SQLEXPRESS’ linked server:

以下示例将删除“ Ben”本地用户到“ WSERVER2012 \ SQLEXPRESS”链接服务器的登录映射:


EXEC sp_droplinkedsrvlogin@rmtsrvname = N'WSERVER2012\SQLEXPRESS',@locallogin = N'Ben';

This code will remove the default login mapping for all users to the ‘WSERVER2012\SQLEXPRESS’ linked server:

此代码将删除所有 用户“ WSERVER2012 \ SQLEXPRESS”链接服务器的默认登录映射:


EXEC sp_droplinkedsrvlogin@rmtsrvname = N'WSERVER2012\SQLEXPRESS',@locallogin = Null;

删除SQL Server链接服务器 (Delete a SQL Server linked server)

To remove a linked server, form the Linked Server folder use the sp_dropserver procedure:

若要删除链接服务器,请使用sp_dropserver过程形成“链接服务器”文件夹:


EXEC sp_dropserver@server = N'Server',@droplogins = ‘droplogins’ | NULL

@server is the name of the server that will be removed.

@server是将被删除的服务器的名称。

@droplogins if the ‘droplogins’ value is set, then all logins for the specified remote server will be removed, by default the @droplogins property is set to Null.

@droplogins如果设置了'droplogins'值,则将删除指定远程服务器的所有登录名,默认情况下,@droplogins属性设置为Null

Now, when executing this code:

现在,执行此代码时:


EXEC sp_dropserver@server = N'WSERVER2012\SQLEXPRESS',@droplogins = NULL;

The following error message will appear:

将出现以下错误消息:

Msg 15190, Level 16, State 1, Procedure sys.sp_dropserver, Line 56 [Batch Start Line 3]
There are still remote logins or linked logins for the server ‘WSERVER2012\SQLEXPRESS’.

消息15190,级别16,状态1,过程sys.sp_dropserver,第56行[批处理开始第3行]
服务器“ WSERVER2012 \ SQLEXPRESS”仍存在远程登录名或链接登录名。

To resolve this problem, there are two solutions:

要解决此问题,有两种解决方案:

  1. sp_droplinkedsrvlogin procedure and then execute the above mentioned code sp_droplinkedsrvlogin过程删除与链接服务器相关的所有登录名,然后执行上述代码
  2. Null value for the @droplogins property in the 'droplogins'值放到sp_dropserver procedure, just put the sp_dropserver过程中,而不是为@droplogins属性的@droplogins属性设置为‘droplogins’ value and all logins related to a chosen linked server will be removed together with a linked server: Null值,与所选链接服务器有关的所有登录名都会与链接服务器一起删除:

EXEC sp_dropserver@server = N'WSERVER2012\SQLEXPRESS',@droplogins ='droplogins';

Other articles in this series:

本系列的其他文章:

  • How to create and configure a linked server in SQL Server Management Studio 如何在SQL Server Management Studio中创建和配置链接服务器
  • 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数据库的链接服务器

翻译自: https://www.sqlshack.com/create-configure-drop-sql-server-linked-server-using-transact-sql/

transact-sql

transact-sql_如何使用Transact-SQL创建,配置和删除SQL Server链接服务器相关推荐

  1. 【SQL】blog.sql :创建博客的SQL语句

    这位大神 的 blog.sql 创建博客的SQL语句 /* Navicat MySQL Data Transfer Source Database : blog Target Server Type ...

  2. 查询链接服务器信息,如何通过 SQL Server 链接服务器和分布式查询使用 Excel

    作为链接服务器查询 Excel 您可以使用企业管理器.系统存储过程或 SQL-DMO(分布式管理对象)将 Excel 数据源配置为 SQL Server 链接服务器.在所有的这些情况中,您总需要设置以 ...

  3. 如何使用SQL Server链接服务器查询Excel数据

    Linked servers allow SQL Server access to data from a remote data source. A remote data source can b ...

  4. 在线sql服务器,SQL Server链接服务器

    SQL Server链接服务器 SQL Server提供链接到另一个服务器的选项.这通常被用来连接到其他SQL Server数据库,但它也可以被用来连接到一个Microsoft Access数据库.这 ...

  5. SQL Server “链接服务器”的使用

    链接服务器是干什么的? 链接服务器 可以把远程数据库链接到本地,然后可以像使用本地数据库一样进行查询,可以交叉本地数据库进行查询 这样 每个数据库可以分布在不同服务器上,一方面减轻服务器压力,另一方面 ...

  6. html如何链接sql sever,SQL Server链接服务器

    链接服务器节点可以连接到另一个数据库,通常/通常在不同的机器上运行,也许在不同的城市/国家.如果您需要执行分布式查询(查询远程数据库),这可能是有用的. 设置链接服务器就是相当于使用简单的 SQL S ...

  7. 查询链接服务器信息,SQL Server链接服务器

    SQL Server提供链接到另一个服务器的选项.这通常被用来连接到其他SQL Server数据库,但它也可以被用来连接到一个Microsoft Access数据库.这是通过链接服务器节点成为可能. ...

  8. SQL Server链接服务器(一台机器SQL Server登陆,同时映射登陆到另外一台远程服务器的数据库)

    现在我有一个需求是需要在两个不同的SQL SERVER 2012的服务器之间进行数据访问和更新.我们需要把Server One的数据插入到Server Two的服务器上去,一天执行一次任务.我们的首选 ...

  9. SQL Server链接服务器

    SQL Server 中存在可以链接到其他服务器的选项,一般情况下是用来与别的 SQL Server 数据库相连接,但是有时候也可以与一个Microsoft Access数据库 相连接.这样的操作是通 ...

最新文章

  1. 解决pycharm创建github工程但push失败的问题
  2. byteman_Byteman –用于字节码操纵的瑞士军刀
  3. MVC5+EF6 入门完整教程四
  4. 简单的多线程实例下载(供初学者下载学习)
  5. 职高有计算机专业前途怎么样,职高有哪些专业前途好有发展前景
  6. 先滑窗后时空联合处理MATLAB,时空联合优化重建方法及系统与流程
  7. Linux Ubuntu 18.04安装JDK、Hadoop、Hbase以及图形界面
  8. go 变量大写_和我一起学Go系列:Go基本语法概览
  9. html5--select与HTML5新增的datalist元素
  10. ArcGIS 打开ArcCatalog 报错error code=-8
  11. Download Software Top 10
  12. CRM客户管理系统搭建
  13. matlab 半导体激光模拟工具箱,MATLAB中的激光器仿真
  14. 一个不常用的DOM原生API,closest
  15. 棕榈油跌停见顶,铁矿石认沽上涨,YP05惊天大反弹2022.3.14
  16. API管理神器:Apifox
  17. GIC/ITS代码分析(3)ITS驱动初始化
  18. eclipse安装与配置maven插件
  19. KS值和GINI系数
  20. jQuery 与for相关的遍历方法

热门文章

  1. Markdown入门指南-指间阁
  2. WWDC 2015大会到来了
  3. javascript的内置对象
  4. [转]win7 64位下android开发环境的搭建
  5. 无法打开登录所请求的数据库 sa。登录失败。 用户 sa 登录失败。
  6. Linux服务器部署常用命令
  7. 【博客项目】—登录验证功能实现( 五)
  8. 数据库原理—数据库基础(二)
  9. HTML的基本知识(一)——标题标签
  10. 深入理解CSS中的line-height的使用