且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Azure (18) 使用External Table实现垮库查询

更新时间:2022-09-15 21:38:57

 《Windows Azure Platform 系列文章目录

 

  问题

  1.我们在进行SQL Server开发的时候,经常会使用垮库查询。但是在默认情况下,使用Azure SQL Database不支持垮库查询。如下图:

  SQL Azure (18) 使用External Table实现垮库查询

  2.我们执行垮库查询语句,如下:

SELECT A.CustomerID,A.FirstName, A.LastName,B.OrderID FROM   CRMDB.dbo.CustomerInfo AS A LEFT JOIN OrderDB.dbo.OrderInfo AS B ON   A.CustomerID=B.CustomerID

 

  3.会出现以下的报错信息:

  SQL Azure (18) 使用External Table实现垮库查询

  4.可以看到,在默认情况下,使用Azure SQL Database不支持垮库查询。

 

  使用外部表:

  准备工作

  1.我们这里增加一些难度。我们在Azure SQL Database分别创建2个Server。如下图:

  SQL Azure (18) 使用External Table实现垮库查询

  注意,为了避免出现跨Azure数据中心之间的延时,请尽量在同一个数据中心创建Server。笔者在中国东部数据中心,创建2个Server

 

  2.我们在这2个Server下创建2个不同的Database,如下图:

SQL Azure (18) 使用External Table实现垮库查询

 

  3.使用SSMS,连接到ew79sank1x.database.chinacloudapi.cn,1433,数据库CRMDB,创建表CustomerInfo

SQL Azure (18) 使用External Table实现垮库查询
create table dbo.CustomerInfo
(
    CustomerID nvarchar(100) not null primary key,
    FirstName nvarchar(100) not null,
    LastName nvarchar(100) not null
)
Go

insert into CustomerInfo(CustomerID,FirstName,LastName) values 
('001','Jason','Zhang'),
('002','Peter','Huang'),
('003','Jason','Hu'),
('004','Mike','Lee')
SQL Azure (18) 使用External Table实现垮库查询

 

  4.使用SSMS,连接到lcqyvtqri1.database.chinacloudapi.cn,1433,数据库OrderDB,创建表OrderInfo

SQL Azure (18) 使用External Table实现垮库查询
create table dbo.OrderInfo
(
    OrderID nvarchar(100) not null,
    CustomerID nvarchar(100) not null,
)
Go

insert into OrderInfo(OrderID,CustomerID) values 
('2013010100001','001'),
('2013010100002','001'),
('2013010100003','002'),
('2013010100004','002'),
('2013010100005','002'),
('2013010100006','003'),
('2013010100007','004')
SQL Azure (18) 使用External Table实现垮库查询

 

  创建外部表

  1.使用SSMS,连接到ew79sank1x.database.chinacloudapi.cn,1433,数据库CRMDB。执行以下脚本

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<password>'; 
CREATE DATABASE SCOPED CREDENTIAL ElasticDBQueryCred 
WITH IDENTITY = '<username>', 
SECRET = '<password>';  

  请注意:上面的<username>和<password>,是连接到lcqyvtqri1.database.chinacloudapi.cn,1433,数据库OrderDB的用户名和密码。注意是连接到另外的Server和Databse,不是自己。

 

  2.执行以下命令

CREATE EXTERNAL DATA SOURCE MyElasticDBQueryDataSrc WITH 
    (TYPE = RDBMS, 
    LOCATION = 'lcqyvtqri1.database.chinacloudapi.cn', 
    DATABASE_NAME = 'OrderDB', 
    CREDENTIAL = ElasticDBQueryCred, 
) ;

  连接到lcqyvtqri1.database.chinacloudapi.cn,1433

 

  3.创建外部表

SQL Azure (18) 使用External Table实现垮库查询
CREATE EXTERNAL TABLE dbo.OrderInfo
(
    OrderID nvarchar(100) not null,
    CustomerID nvarchar(100) not null
)
WITH 
( DATA_SOURCE = MyElasticDBQueryDataSrc)
SQL Azure (18) 使用External Table实现垮库查询

  注意:上面创建的External Table的Table Name和Table Schema,必须与Azure SQL Database: lcqyvtqri1.database.chinacloudapi.cn,1433,数据库OrderDB中的表OrderInfo的Table Name和Table Schema一样。

 

  4.执行完毕后,我们可以在ew79sank1x.database.chinacloudapi.cn,1433,查看到原本保存在lcqyvtqri1.database.chinacloudapi.cn,1433的数据库OrderDB中的表dbo.OrderInfo。如下图:

SQL Azure (18) 使用External Table实现垮库查询

 

  5.我们可以执行以下语句,查询dbo.OrdrInfo表的内容:

SELECT [OrderID],[CustomerID] FROM [dbo].[OrderInfo]

 

  

  执行结果,就是我们在lcqyvtqri1.database.chinacloudapi.cn,1433数据库OrderDB中的表dbo.OrderInfo的值

  

  6.执行查询语句:

SELECT A.CustomerID,A.FirstName, A.LastName,B.OrderID FROM dbo.CustomerInfo AS A LEFT JOIN dbo.OrderInfo AS B ON A.CustomerID=B.CustomerID

  如下图:

  SQL Azure (18) 使用External Table实现垮库查询

 

 

  参考资料:

https://azure.microsoft.com/en-us/documentation/articles/sql-database-elastic-query-getting-started-vertical/



本文转自Lei Zhang博客园博客,原文链接:http://www.cnblogs.com/threestone/p/5552502.html,如需转载请自行联系原作者