azure云服务器搭建连接_如何创建到Azure SQL数据库的链接服务器

本文介绍了如何通过SQL Server Management Studio(SSMS)和Transact-SQL创建链接服务器以访问Azure SQL数据库。文章详细阐述了创建过程中可能遇到的问题及其解决方法,并提供了使用OPENQUERY功能查询数据的示例。
摘要由CSDN通过智能技术生成

azure云服务器搭建连接

Linked servers allow to access data from another SQL Server or another data source (e.g. Excel) by using SQL Server Management Studio (SSMS) or Transact-SQL.

链接服务器允许使用SQL Server Management Studio(SSMS)Transact-SQL访问来自另一个SQL Server或另一个数据源(例如Excel )的数据。

This article will explain how to create and configure a linked server to retrieve data from an Azure SQL database. Also, we will explain how to solve some common problems/issues during the process of creating a linked server to an Azure SQL database.

本文将介绍如何创建和配置链接服务器以从Azure SQL数据库检索数据。 此外,我们还将说明在创建到Azure SQL数据库的链接服务器的过程中如何解决一些常见问题/问题。

通过SSMS创建到Azure SQL数据库的链接服务器 (Create a linked server to an Azure SQL database via SSMS)

To create a linked server via SSMS, go to Object Explorer under the Server Objects folder, right click on the Linked Servers folder and from the context menu choose the New Linked Server command:

要通过SSMS创建链接服务器,请转到“ 服务器对象”文件夹下的“ 对象资源管理器 ”,右键单击“ 链接服务器”文件夹,然后从上下文菜单中选择“ 新建链接服务器”命令:

The New Linked Server window will be opened:

将打开“ 新链接服务器”窗口:

In this window, the first thing that needs to be done is to enter a name for a linked server in the Linked server box and to choose the server type by clicking the SQL Server or Other data source radio button under the General tab.

在此窗口中,需要做的第一件事是在“ 链接的服务器”框中输入链接服务器的名称,并通过单击“ 常规”选项卡下的“ SQL Server”或“ 其他数据源”单选按钮来选择服务器类型。

Let’s first choose the SQL Server radio button under the Server type section and see what will happen. Also, on the General tab in the Linked server box, enter the name of the Azure SQL server for which a linked server is created:

首先让我们选择“ 服务器类型”部分下的“ SQL Server”单选按钮,然后看看会发生什么。 另外,在“ 链接服务器”框中的“ 常规”选项卡上,输入为其创建链接服务器的Azure SQL服务器的名称:

Under the Security tab, select the Be made using this security context radio button and enter user credentials that exist on Azure server:

在“ 安全性”选项卡下,选择“ 使用此安全性上下文进行创建”单选按钮,然后输入Azure服务器上存在的用户凭据:

More about the Security and Server Options tab can be found on the How to create and configure a linked server in SQL Server Management Studio article.

有关“ 安全服务器选项”选项卡的更多信息,请参见如何在SQL Server Management Studio中创建和配置链接服务器

After entering the user credentials, press the OK button to create a linked server to an Azure SQL database. Now, under the Linked Servers folder, Azure linked server that we created will appear and in the Catalogs folder, all available databases will be listed:

输入用户凭据后,按“ 确定”按钮以创建到Azure SQL数据库的链接服务器。 现在,在“ 链接服务器”文件夹下,将显示我们创建的Azure链接服务器,并且在“ 目录”文件夹中,将列出所有可用的数据库:

But, when expanding a particular database (e.g.TestDatabase) is needed in order to see tables of the database, the following error message will appear:

但是,当需要扩展特定数据库(例如TestDatabase)以查看数据库表时,将出现以下错误消息:

Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)

无法检索此请求的数据。 (Microsoft.SqlServer.Management.Sdk.Sfc)

ADDITIONAL INFORMATION:

附加信息:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

执行Transact-SQL语句或批处理时发生异常。 (Microsoft.SqlServer.ConnectionInfo)

Reference to database and/or server name in ‘TestDatabase.sys.sp_tables_rowset2’ is not supported in this version of SQL Server. (Microsoft SQL Server, Error: 40515)

在此版本SQL Server中,不支持在“ TestDatabase.sys.sp_tables_rowset2”中引用数据库和/或服务器名称。 (Microsoft SQL Server,错误:40515)

This error occurs because Azure does not allow to alter the master database. To resolve this, you need to connect directly to the Azure database you are going to use.

发生此错误的原因是Azure不允许更改主数据库。 若要解决此问题,您需要直接连接到将要使用的Azure数据库。

Delete the SQL Azure linked server that we created and create a new one:

删除我们创建SQL Azure链接服务器,然后创建一个新的:

This time, under the Server type section of the General tab, choose the Other data source radio button. The name for the linked server in the Linked server text box can be, this time, whatever you like (e.g. AZURE SQL DATABASE). Under the Provider drop down box, choose the Microsoft OLE DB Provider SQL Server item. In the Data source text box, enter the name of the SQL Azure (e.g. server.database.windows.net). The most important setting in order to correctly create a linked server to an Azure SQL database is to enter the name in the Catalog text box (e.g. TestDatabase) of an Azure SQL database for which you want to create a linked server to an Azure SQL database. Otherwise, if this field is left empty, we will encounter the same 40515 error when trying to get a list of the tables under the Catalogs folder.

这次,在“ 常规”选项卡的“ 服务器类型”部分下,选择“ 其他数据源”单选按钮。 这次,“ 链接服务器”文本框中的链接服务器名称可以是任意名称(例如AZURE SQL DATABASE)。 在“ 提供程序”下拉框中,选择“ Microsoft OLE DB提供程序SQL Server”项。 在“ 数据源”文本框中,输入SQL Azure的名称(例如server.database.windows.net)。 为了正确创建到Azure SQL数据库的链接服务器,最重要的设置是在要为其创建到Azure SQL数据库的链接服务器的Azure SQL数据库的目录文本框中输入名称(例如TestDatabase)。 。 否则,如果将此字段保留为空,则尝试获取Catalogs文件夹下的表列表时,我们将遇到相同的40515错误。

Under the Security tab, use the same setting that we used in the previous example and press the OK button. This will create a linked server to an Azure SQL database (TestDatabase database), and when the plus (+) sign next to the Tables folder is pressed, the Tables folder will expand and show all tables for the TestDatabase database:

在“ 安全性”选项卡下,使用与上一个示例相同的设置,然后按“ 确定”按钮。 这将创建一个链接服务器到Azure SQL数据库(TestDatabase数据库),并且当按下Tables文件夹旁边的加号(+)时,Tables文件夹将展开并显示TestDatabase数据库的所有表:

To retrieve data from the SQL Azure table (e.g. CustomerAddress), type the following code:

要从SQL Azure表(例如CustomerAddress)中检索数据,请键入以下代码:

 
SELECT * FROM [AZURE SQL DATABASE].[TestDatabase].[SalesLT].[CustomerAddress]
 

If everything goes well, the data from the CustomerAddress table will appear in the Results grid:

如果一切顺利,CustomerAddress表中的数据将显示在“ 结果”网格中:

Now, if you try to execute a stored procedure from the AZURE SQL DATABASE linked server, the following message may appear:

现在,如果您尝试从AZURE SQL DATABASE链接服务器执行存储过程,则可能会出现以下消息:

Msg 7411, Level 16, State 1, Line 48
Server ‘AZURE SQL DATABASE’ is not configured for RPC.

消息7411,第16层,状态1,第48行
未为RPC配置服务器“ AZURE SQL DATABASE”。

This is because the RPC and RCP Out (Remote Procedure Call) options that allow remote procedures to be called from the linked server or to be called to the linked server by default set to false.

这是因为默认情况下将RPC和RCP Out( 远程过程调用 )选项设置为false,该选项允许从链接服务器调用远程过程或调用链接过程到远程服务器。

To fix that, right click on the AZURE SQL DATABASE linked server, choose the Properties options:

要解决此问题,请右键单击AZURE SQL DATABASE链接服务器,选择“ 属性”选项:

Under the Server Options tab, set the RPC and RPC Out options to True:

在“ 服务器选项”选项卡下,将“ RPC”和“ RPC Out”选项设置为“ True”:

Or in a query editor, paste and execute the following code:

或在查询编辑器中,粘贴并执行以下代码:

 
EXEC master.dbo.sp_serveroption @server=N'AZURE SQL DATABASE', @optname=N'rpc', @optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE SQL DATABASE', @optname=N'rpc out', @optvalue=N'true'
GO
 

OPENQUERY功能 (OPENQUERY function)

The OPENQUERY function can be used to get data from the linked server by executing code like this:

OPENQUERY函数可用于通过执行以下代码从链接服务器获取数据:

 
SELECT * FROM OPENQUERY([AZURE SQL DATABASE],'SELECT * FROM SalesLT.CustomerAddress')
 

The results will be the same as from the example above.

结果将与上面的示例相同。

This is the ad hoc method for connection to a remote server using the linked server and querying data from a remote server. If the connection to remote server is frequently used, then using the linked server is better solution instead of using the OPENQUERY function.

这是使用链接的服务器连接到远程服务器并从远程服务器查询数据的临时方法。 如果经常使用到远程服务器的连接,则使用链接服务器是更好的解决方案,而不是使用OPENQUERY函数。

This function can be used in the FROM clause of the INSERT, SELECT, DELETE or UPDATE statement.

可以在INSERTSELECTDELETEUPDATE语句的FROM子句中使用此函数。

使用Transact-SQL创建到Azure SQL数据库的链接服务器 (Create a linked server to an Azure SQL database using Transact-SQL)

In order to create a linked server to an Azure SQL database, type the following code in a query editor:

为了创建到Azure SQL数据库的链接服务器,请在查询编辑器中键入以下代码:

 
EXEC master.dbo.sp_addlinkedserver
 @server = N'AZURE SQL DATABASE', 
 @srvproduct=N'',
  @provider=N'SQLNCLI',
   @datasrc=N'server.database.windows.net',
    @catalog=N'TestDatabase'
 /* For security reasons, the linked server remote logins password is changed with ######## */
EXEC master.dbo.sp_addlinkedsrvlogin
 @rmtsrvname=N'AZURE SQL DATABASE',
 @useself=N'False',
 @locallogin=NULL,
 @rmtuser=N'zivko',@rmtpassword='#########'
GO
 

More about how to set linked server by using T-SQL can be found on the How to create, configure and drop a SQL Server linked server using Transact-SQL page

有关如何使用T-SQL设置链接服务器的更多信息,请参见如何使用Transact-SQL创建,配置和删除SQL Server链接服务器页面。

连接到Azure SQL数据库并使用分布式查询来查询数据 (Connecting to Azure SQL database and querying data using the distributed queries)

To connect to Azure SQL database and access data without creating a linked server first, use the T-SQL OPENROWSET or OPENDATASOURCE functions.

若要连接到Azure SQL数据库并访问数据而无需先创建链接服务器,请使用T-SQL OPENROWSETOPENDATASOURCE函数。

To open a connection and querying data from the Azure SQL database using the OPENROWSET function, type the following code in a query editor:

若要打开连接并使用OPENROWSET函数从Azure SQL数据库中查询数据,请在查询编辑器中键入以下代码:

 
SELECT * FROM OPENROWSET('SQLNCLI', 'Server=server.database.windows.net;Database=TestDatabase;UID=zivko;PWD==######;', 'SELECT * FROM SalesLT.CustomerAddress')
 

If, for some reasons, the above code does not work, use the code below to connect and query data from Azure SQL database:

如果由于某些原因上述代码无法正常工作,请使用以下代码从Azure SQL数据库连接和查询数据:

 
SELECT * FROM  OPENROWSET('MSDASQL', 'Driver={SQL SERVER}; Server=server.database.windows.net;Database=TestDatabase;UID=zivko; PWD=######;', 'SELECT * FROM SalesLT.CustomerAddress')
 

Another way of connecting and querying data from the Azure SQL database is by using the OPENDATASOURCE function.

从Azure SQL数据库连接和查询数据的另一种方法是使用OPENDATASOURCE函数。

In a query editor, paste and execute one of the following codes:

在查询编辑器中,粘贴并执行以下代码之一:

 
SELECT *  
FROM OPENDATASOURCE('MSDASQL', 'Driver={SQL SERVER}; Server=server.database.windows.net;Database=TestDatabase;UID=zivko;PWD==######;').TestDatabase.SalesLT.CustomerAddress
 

Or

要么

 
 SELECT * FROM OPENDATASOURCE('SQLNCLI', 'Server=server.database.windows.net;Database=TestDatabase;UID=zivko;PWD==######;').TestDatabase.SalesLT.CustomerAddress
 

Common error that may occur when using the T-SQL OPENROWSET and OPENDATASOURCE functions:

使用T-SQL OPENROWSETOPENDATASOURCE函数时可能会发生的常见错误:

Msg 15281, Level 16, State 1, Line 1

消息15281,第16级,状态1,第1行

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’, search for ‘Ad Hoc Distributed Queries’ in SQL Server Books On

SQL Server阻止访问组件“临时分布式查询”的STATEMENT“ OpenRowset / OpenDatasource”,因为此服务器的安全配置已将此组件关闭。 系统管理员可以使用sp_configure启用“临时分布式查询”的使用。 有关启用“临时分布式查询”的更多信息,请在SQL Server的“ Books On”上搜索“ Ad hoc Distributed Queries”。

To resolve this the Ad Hoc Distributed Queries option should be enabled. To enable the Ad Hoc Distributed Queries option, use the sp_configure procedure and in a query editor, paste and execute the following code:

要解决此问题,应启用“ 临时分布式查询”选项。 若要启用“临时分布式查询”选项,请使用sp_configure过程,并在查询编辑器中粘贴并执行以下代码:

 
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE
GO
 

Previous articles in this series:

本系列以前的文章:

翻译自: https://www.sqlshack.com/create-linked-server-azure-sql-database/

azure云服务器搭建连接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值