在Azure SQL数据库中创建关联服务器

4

想在Azure SQL中创建链接服务器。我想使用链接服务器在SQL Azure中使用本地DB视图。是否可能或是否有其他替代方法。欢迎所有建议。


https://learn.microsoft.com/zh-cn/azure/sql-database/sql-database-features ; 简而言之,没有链接服务器。 - user6144226
链接还是复制? - 4c74356b41
3个回答

7

是的,您可以在SQLAZURE中创建链接服务器...假设您有本地预置服务器A和Azure中的数据库AZ_b..您可以为本地预置实例创建一个Azure链接服务器...

由于您想要这样做我想使用链接服务器在sql azure中使用本地DB视图。在创建链接服务器之后您需要从服务器A运行查询,即本地预置服务器,这是链接服务器名称解析的唯一方式,您不能反过来做。

以下是步骤

-- Supporse your database on Azure is named 'Azure_Test_DB' 
EXEC sp_addlinkedserver   
@server='myLinkedAzureServer', -- specify the name of the linked server   
@srvproduct='',        
@provider='sqlncli', 
@datasrc='azure-test-db.database.windows.net',   -- add here your server name   
@location='',   
@provstr='',   
--------Change it by your need ------------------------------------------------------------------ 
@catalog='Azure_Test_DB'  -- specify the name of database on your Azure DB you want to link 
------------------------------------------------------------------------------------------------- 

-- Configure credentials for Azure linked server 
EXEC sp_addlinkedsrvlogin   
@rmtsrvname = 'myLinkedAzureServer',   
@useself = 'false',   
--------Change it by your need ------------------------------------------------------------------ 
@rmtuser = 'yourLoginName',   -- add here your login on Azure DB   
@rmtpassword = 'yourPassword' -- add here your password on Azure DB   
------------------------------------------------------------------------------------------------- 

-- Configure options for Azure linked server 
EXEC sp_serveroption 'myLinkedAzureServer', 'rpc out', true;   


-- Now you can query the data using 4-part names   
select * from myLinkedAzureServer.[Azure_Test_DB].[dbo].[Students]; 

一旦您创建了联接服务器,就可以连接到A服务器并运行以下查询:

select * from 
myLinkedAzureServer.[Azure_Test_DB].[dbo].[Students] az
join
localdb.dbo.table1 tbl
on tbl.somecol=az.somecol

参考文献:
https://gallery.technet.microsoft.com/scriptcenter/How-to-create-linked-cb98fa7d
https://www.mssqltips.com/sqlservertip/3630/connect-an-azure-sql-database-to-an-onpremises-sql-server/

以上步骤适用于大多数计算机... 如果不起作用,您需要按照此处的步骤设置ODBC DSN。

https://blogs.msdn.microsoft.com/sqlcat/2011/03/07/linked-servers-to-sql-azure/


1
你无法在Azure SQL数据库中创建链接服务器。
微软提供了一个新的Instance as a Service预览版,可以执行跨数据库查询。它也可能允许使用链接服务器,但目前还没有广泛推出。
现在,你可以使用弹性查询来设置类似的功能。这是我写的一篇博客文章。然而,它并不等同于链接服务器。

0

由于SQL Azure数据库上不支持链接服务器的限制,您可以通过将对象从本地SQL Server数据库复制到SQL Azure或使用SQL数据同步在本地SQL Server和SQL Azure服务器之间同步数据来解决此问题。

希望这可以帮助您。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接