2017-02-16 3 views
0

Как выполнить SELECT в таблице для всех баз данных, доступных в пуле ElasticDB. Все они имеют одну и ту же схему БД, и они создаются динамически. Я изучил Elastic Database Query, но потерялся посередине.Как запросить все базы данных в Azure ElasticDB Pool?

Reporting across scaled-out cloud databases

Он просит, чтобы загрузить пример приложения консоли первым, создать осколок, а затем выполнить запрос, который немного сбивает с толку. В любом случае я могу запускать запросы T-SQL из SQL Server Management Studio для запроса всех баз данных.

PS: БД не оштукатурены. Они находятся на БД для каждого клиента.

Спасибо заранее!

ответ

0

Я думаю, вам нужно добавить базы данных в качестве внешних источников, чтобы вы могли выполнить кросс-запрос базы данных, вы сможете запросить таблицы так, как если бы они были локальными. я нашел руководство, которое может помочь вам установить его: Ссылка на руководство:

https://www.mssqltips.com/sqlservertip/4550/sql-azure-cross-database-querying/

руководство:

DB1 has Db1Table table:

CREATE TABLE DB1.dbo.Db1Table (
ID int IDENTITY(1, 1) NOT NULL PRIMARY KEY, 
CustomerId INT, 
CustomerName NVARCHAR(50)); 
INSERT INTO DB1.dbo.Db1Table(CustomerId, CustomerName) VALUES 
(1, 'aaaaaaa'), 
(2, 'bbbbbbb'), 
(3, 'ccccccc'), 
(4, 'ddddddd'), 
(5, 'eeeeeee'); 
CREATE TABLE DB1.dbo.Db1Table (
ID int IDENTITY(1, 1) NOT NULL PRIMARY KEY, 
CustomerId INT, 
CustomerName NVARCHAR(50)); 
INSERT INTO DB1.dbo.Db1Table(CustomerId, CustomerName) VALUES 
(1, 'aaaaaaa'), 
(2, 'bbbbbbb'), 
(3, 'ccccccc'), 
(4, 'ddddddd'), 
(5, 'eeeeeee'); 

DB2 has Db2Table table:

CREATE TABLE DB2.dbo.Db2Table (
ID int IDENTITY(1, 1) NOT NULL PRIMARY KEY, 
CustomerId INT, 
Country NVARCHAR(50)); 
INSERT INTO DB2.dbo.Db2Table(CustomerId, Country) VALUES 
(1, 'United States'), 
(3, 'Greece'), 
(4, 'France'), 
(5, 'Germany'), 
(6, 'Ireland'); 
CREATE TABLE DB2.dbo.Db2Table (
ID int IDENTITY(1, 1) NOT NULL PRIMARY KEY, 
CustomerId INT, 
Country NVARCHAR(50)); 
INSERT INTO DB2.dbo.Db2Table(CustomerId, Country) VALUES 
(1, 'United States'), 
(3, 'Greece'), 
(4, 'France'), 
(5, 'Germany'), 
(6, 'Ireland'); 

If we want to fetch customers whose country is Greece then we could do the following query:

SELECT 
db1.CustomerId, 
db1.CustomerName 
FROM DB1.dbo.Db1Table db1 
INNER JOIN DB2.dbo.Db2Table db2 ON db1.CustomerId = db2.CustomerId 
WHERE db2.Country = 'Greece'; 
SELECT 
db1.CustomerId, 
db1.CustomerName 
FROM DB1.dbo.Db1Table db1 
INNER JOIN DB2.dbo.Db2Table db2 ON db1.CustomerId = db2.CustomerId 
WHERE db2.Country = 'Greece'; 

but instead of returning customerId 3 we get the following error:

Reference to database and/or server name in 'DB2.dbo.Db2Table' is not supported in this version of SQL Server.

Reference to database and/or server name in 'DB2.dbo.Db2Table' is not supported in this version of SQL Server.

In order to be able to perform a cross database query we need to perform the following steps: Step1: Create Master Key

The database master key is a symmetric key used to protect the private keys of certificates and asymmetric keys that are present in the database. More info here.

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<password>'; 

-- Example -- 
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '[email protected]' 
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<password>'; 


-- Example -- 
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '[email protected]' 

Step 2: Create Database Scoped Credential “my_credential”

A database credential is not mapped to a server login or database user. The credential is used by the database to access the external location anytime the database is performing an operation that requires access.

CREATE DATABASE SCOPED CREDENTIAL <credential_name> 
WITH IDENTITY = '<user>', 
SECRET = '<secret>'; 

-- Example -- 
CREATE DATABASE SCOPED CREDENTIAL my_credential 
WITH IDENTITY = 'dbuser', 
SECRET = '[email protected]'; 


CREATE DATABASE SCOPED CREDENTIAL <credential_name> 
WITH IDENTITY = '<user>', 
SECRET = '<secret>'; 


-- Example -- 
CREATE DATABASE SCOPED CREDENTIAL my_credential 
WITH IDENTITY = 'dbuser', 
SECRET = '[email protected]'; 

credential_name Specifies the name of the database scoped credential being created. credential_name cannot start with the number (#) sign. System credentials start with ##. IDENTITY =’identity_name‘ Specifies the name of the account to be used when connecting outside the server. SECRET =’secret‘ Specifies the secret required for outgoing authentication.

Step 3: Create External Data Source “my_datasource” of type RDBMS

This instruction creates an external data source for use in Elastic Database queries. For RDBMS, it specifies the logical server name of the remote database in Azure SQL Database. -- (only on Azure SQL Database v12 or later)

CREATE EXTERNAL DATA SOURCE <data_source_name> 
WITH (
TYPE=RDBMS, 
LOCATION='<server_name>.database.secure.windows.net', 
DATABASE_NAME='<remote_database_name>', 
CREDENTIAL = <sql_credential>); 

-- Example -- 
CREATE EXTERNAL DATA SOURCE my_datasource 
WITH (
TYPE=RDBMS, 
LOCATION='ppolsql.database.secure.windows.net', 
DATABASE_NAME='DB2', 
CREDENTIAL = my_credential); 

-- (only on Azure SQL Database v12 or later) 

CREATE EXTERNAL DATA SOURCE <data_source_name> 
WITH (
TYPE=RDBMS, 
LOCATION='<server_name>.database.secure.windows.net', 
DATABASE_NAME='<remote_database_name>', 
CREDENTIAL = <sql_credential>); 

-- Example -- 
CREATE EXTERNAL DATA SOURCE my_datasource 
WITH (
TYPE=RDBMS, 
LOCATION='ppolsql.database.secure.windows.net', 
DATABASE_NAME='DB2', 
CREDENTIAL = my_credential); 
CREATE EXTERNAL TABLE [ database_name . [ schema_name ] . | schema_name. ] table_name 
(<column_definition> [ ,...n ]) 
WITH ( 
    DATA_SOURCE = <data_source_name>); 

-- Example -- 
CREATE EXTERNAL TABLE [dbo].[Db2Table] (
[ID] int NOT NULL, 
[CustomerId] INT, 
[Country] NVARCHAR(50) 
) WITH (DATA_SOURCE = my_datasource) 


CREATE EXTERNAL TABLE [ database_name . [ schema_name ] . | schema_name. ] table_name 
(<column_definition> [ ,...n ]) 
WITH ( 
    DATA_SOURCE = <data_source_name> 
); 

-- Example -- 
CREATE EXTERNAL TABLE [dbo].[Db2Table] (
[ID] int NOT NULL, 
[CustomerId] INT, 
[Country] NVARCHAR(50) 
) WITH (DATA_SOURCE = my_datasource) 

database_name . [ schema_name ] . | schema_name. ] table_name The one to three-part name of the table to create. For an external table, only the table metadata is stored in SQL along with basic statistics about the file and/or folder referenced in Hadoop or Azure blob storage. No actual data is moved or stored in SQL Server. [ ,…n ] The column definitions, including the data types and number of columns, must match the data in the external files. DATA_SOURCE = external_data_source_name Specifies the name of the external data source that contains the location of the external data.

After running the DDL statements, you can access the remote table Db2Table as though it were a local table.

So, now if we want to fetch customers whose country is Greece the query would be executed successfully:

SELECT 
db1.CustomerId, 
db1.CustomerName 
FROM DB1.dbo.Db1Table db1 
INNER JOIN DB1.dbo.Db2Table db2 ON db1.CustomerId = db2.CustomerId 
WHERE db2.Country = 'Greece'; 

-- Result -- 
CustomerId | CustomerName 
------------------------- 
3  ccccccc 


SELECT 
db1.CustomerId, 
db1.CustomerName 
FROM DB1.dbo.Db1Table db1 
INNER JOIN DB1.dbo.Db2Table db2 ON db1.CustomerId = db2.CustomerId 
WHERE db2.Country = 'Greece'; 


-- Result -- 
CustomerId | CustomerName 
------------------------- 
3  ccccccc