2016-05-31 93 views
1

我有兩個不同的數據庫,我需要來自兩個數據庫的信息。我設法得到這樣的數據:Azure SQL - 通過多個數據庫查詢創建視圖

create master key encryption by password = 'test' 

create database scoped crendetial testcredentials with identity = '[email protected]', secret = '[email protected]' 

-- CREATE DATA SOURCE -- 
create external data source RemoteDataSource with 
(
    type = RDBMS, 
    location = 'secret.database.windows.net', 
    database_name = 'testDB_Dev', 
    credential = testcredentials 
) 

-- CREATE TABLES -- 
create external table [RemoteDepartment] 
(
    [ID] [int], 
    [Name] [nvarchar](50) 
) 
with 
(
    data_source = RemoteDataSource, 
    schema_name = 'dbo', 
    object_name = 'Department' 
); 

在這裏,我現在可以selectjoin,等等。 (Employee表來自其他數據庫(一個我查詢)

select e.Name, e.Role, d.Name from Employee as e inner join 
    RemoteDepartment as d on d.ID = e.DepartmentId 

利用隨後的輸出

|---Name---|---Role---|---Name--| 
| Louise | Manager | HR | 
| Tim | Finances | Admin | 

這裏是我的問題。儘管這一切都非常好,而且速度很快,但我似乎無法將自己的頭腦包圍起來,或者在線查找任何資源,也無法瞭解如何使用這種查詢兩個獨立數據庫的方法創建視圖。

我曾嘗試以下:

create master key encryption by password = 'test' 

create database scoped crendetial testcredentials with identity = '[email protected]', secret = '[email protected]' 

-- CREATE DATA SOURCE -- 
create external data source RemoteDataSource with 
(
    type = RDBMS, 
    location = 'secret.database.windows.net', 
    database_name = 'testDB_Dev', 
    credential = testcredentials 
) 

-- CREATE TABLES -- 
create external table [RemoteDepartment] 
(
    [ID] [int], 
    [Name] [nvarchar](50) 
) 
with 
(
    data_source = RemoteDataSource, 
    schema_name = 'dbo', 
    object_name = 'Department' 
); 

CREATE VIEW test_view AS 
    select e.Name, e.Role, d.Name from RemoteEmployee as e inner join 
     RemoteDepartment as d on d.ID = e.DepartmentId 

但得到一個編譯錯誤說

語法錯誤: 'CREATE VIEW' 必須在該批次中唯一的語句

我試圖刪除除了select聲明以外的所有內容,但這不起作用(編譯器不知道什麼RemoteDepartment實際上是

+0

對不起,你說你有兩個不同的數據庫,但我只在一個數據庫'testDB_Dev'中看到2個表。 –

+0

當然可以。我對該帖子進行了修改。表'Employee'來自被查詢的數據庫。我的道歉 – Detilium

+0

我建議你在一個數據庫中創建2個表,否則使用2個數據庫浪費太多的資源 –

回答

-1

這聽起來像是一個簡單的情況,不能用Azure SQL數據庫實例進行跨數據庫查詢。

+0

但我可以嗎?你讀過我寫的嗎?我可以獲取數據,但我無法創建視圖。 – Detilium