Azure SQL - 通过多个数据库创建查询

时间:2016-05-31 13:57:29

标签: sql-server azure views

我有两个不同的数据库,我需要来自两个数据库的信息。我设法获得了这样的数据:

create master key encryption by password = 'test'

create database scoped crendetial testcredentials with identity = 'secret@login', secret = 'secret@password'

-- 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等等。 (员工表来自其他数据库(我正在查询的那个)

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 = 'secret@login', secret = 'secret@password'

-- 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实际上是什么

1 个答案:

答案 0 :(得分:-1)

这听起来只是一个无法使用Azure SQL数据库实例进行跨数据库查询的简单案例。