忙了一大段時間,突然發現好長時間沒更新部落格了,這段時間專案實在太忙了,慢慢的再把這些時間學到的東西慢慢筆記下來,今天就先從Azure開始吧!!
以前如果有跨DB存取或Join Table,通常都會請DBA開Link DB的方式來處理,但在Azure SQL上面就沒有這個選項了
[![](https://2.bp.blogspot.com/-LvGu3FglCEI/WgOxsaEfz7I/AAAAAAAAIP8/3xQmoM8lFCs4oIawixv-HHidz_NHEjRpQCEwYBhgL/s1600/1.png)](https://2.bp.blogspot.com/-LvGu3FglCEI/WgOxsaEfz7I/AAAAAAAAIP8/3xQmoM8lFCs4oIawixv-HHidz_NHEjRpQCEwYBhgL/s1600/1.png) |
跨DB讀取資料 |
但它推出了一個語法來達成這樣子的需求 External Data Source,假設我在**A資料庫**,想要Join **B資料庫**的**PromoEmail Table**取得行銷案的Email資料來做交叉分析,這時候我們可以這樣寫
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'This Account Password';
CREATE DATABASE SCOPED CREDENTIAL AzureStorageCredential WITH IDENTITY = 'This DataBase Login Account', SECRET = 'This Account Password';
CREATE EXTERNAL DATA SOURCE RefPromoTableDataBase WITH ( TYPE=RDBMS, LOCATION ='Your Azure SQL Database Location', DATABASE_NAME = 'B', CREDENTIAL = AzureStorageCredential );
CREATE EXTERNAL TABLE [dbo].[PromoEmail]( [Email] [varchar] (300) Not NULL ) WITH ( DATA_SOURCE = RefPromoTableDataBase );
SELECT email FROM [Order] o Join PromoEmail pe on o.UserEmail = pe.Email
|
當然最後做完後,要記得把這些資源都釋放掉
1 2 3 4 5
| DROP EXTERNAL TABLE PromoEmail DROP EXTERNAL DATA SOURCE RefPromoTableDataBase DROP DATABASE SCOPED CREDENTIAL AzureStorageCredential DROP MASTER KEY
|
參考資料
Cross Database Queries In Azure SQL
SETTING UP CROSS DATABASE QUERIES IN AZURE SQL DATABASE