如何在另一个 Azure SQL 中制作一个 Azure SQL Exec 或 select 另一个 table/procedure

How to make a Azure SQL Exec or select another table/procedure in another Azure SQL

我有两台Azure SQL服务器,每台包含一个数据库,我需要在它们之间执行选择或存储过程。

例如:

Server1.database.windows.net 数据库:local1 Server2.database.windows.net 数据库:remote1

在服务器 1 中:Select * from Server2.remote1.dbo.Orders 并且此查询 return 订单列表。

在 Server2 中:Exec Server1.local1.dbo.ProcedureOrders 并且这会在 Server1

中执行一个存储过程

首先,这可能吗?如果是,如何?

您可以使用 elastic queries to query tables across different databases in different logical servers and even when they belong to different subscriptions. You can find and example here.

您还可以 运行 跨不同逻辑服务器上的数据库进行事务,如 this 文章中所述。

是的,这是可能的。 Azure sql 数据库不支持链接服务器,但就像@Alberto Morillo 提到的 Elastic Query 可以帮助您在其他 SQL 服务器中查询和执行存储过程。

对于远程 table 查询,步骤如下:

  1. 创建数据库范围内的主密钥和凭据:

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

  2. 创建外部数据源:

    <External_Data_Source> ::= CREATE EXTERNAL DATA SOURCE <data_source_name> WITH (TYPE = RDBMS, LOCATION = ’<fully_qualified_server_name>’, DATABASE_NAME = ‘<remote_database_name>’, CREDENTIAL = <credential_name> ) [;]

  3. 外部表:

    CREATE EXTERNAL TABLE [ database_name . [ schema_name ] . | schema_name . ] table_name ( { <column_definition> } [ ,...n ]) { WITH ( <rdbms_external_table_options> ) } )[;]

    <rdbms_external_table_options> ::= DATA_SOURCE = <External_Data_Source>, [ SCHEMA_NAME = N'nonescaped_schema_name',] [ OBJECT_NAME = N'nonescaped_object_name',]

执行远程存储过程:

Elastic query 还引入了一个存储过程,提供对远程数据库的直接访问。存储过程称为sp_execute _remote,可用于在远程数据库上执行远程存储过程或T-SQL代码。

示例:

EXEC sp_execute_remote
        N'MyExtSrc',
        N'select count(w_id) as foo from warehouse'

更多详情请参考:

  1. Reporting across scaled-out cloud databases (preview)
  2. Query across cloud databases with different schemas (preview)

希望这对您有所帮助。