Cross-database queries in Azure SQL Database
A limitation with Azure SQL database has been its inability to do cross-database SQL queries. This has changed with the introduction of elastic database queries, now in preview. However, it’s not as easy as on-prem SQL Server, where you can just use the three-part name syntax DatabaseName.SchemaName.TableName. Instead, you have to define remote tables (tables outside your current database), which is similar to how PolyBase works for those of you familiar with PolyBase.
Here is sample code that, from within database AdventureWorksDB, selects data from table Customers in database Northwind:
--Within database AdventureWorksDB, will select data from table Customers in database Northwind
--Create database scoped master key and credentials
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<password>';
--Needs to be username and password to access SQL database
CREATE DATABASE SCOPED CREDENTIAL jscredential WITH IDENTITY = '<username>', SECRET = '<password>';
--Define external data source
CREATE EXTERNAL DATA SOURCE RemoteNorthwindDB WITH
(TYPE = RDBMS,
LOCATION = '<servername>.database.windows.net',
DATABASE_NAME = 'Northwind',
CREDENTIAL = jscredential
);
--Show created external data sources
select * from sys.external_data_sources;
--Create external (remote) table. The schema provided in your external table definition needs to match the schema of the tables in the remote database where the actual data is stored.
CREATE EXTERNAL TABLE [NorthwindCustomers]( --what we want to call this table locally
[CustomerID] [nchar](5) NOT NULL,
[CompanyName] [nvarchar](40) NOT NULL,
[ContactName] [nvarchar](30) NULL,
[ContactTitle] [nvarchar](30) NULL,
[Address] [nvarchar](60) NULL,
[City] [nvarchar](15) NULL,
[Region] [nvarchar](15) NULL,
[PostalCode] [nvarchar](10) NULL,
[Country] [nvarchar](15) NULL,
[Phone] [nvarchar](24) NULL,
[Fax] [nvarchar](24) NULL
)
WITH
(
DATA_SOURCE = RemoteNorthwindDB,
SCHEMA_NAME = 'dbo', --schema name of remote table
OBJECT_NAME = 'Customers' --table name of remote table
);
--Show created external tables
select * from sys.external_tables;
--You can now select data from this external/remote table, including joining it to local tables
select * from NorthwindCustomers
--Cleanup
DROP EXTERNAL TABLE NorthwindCustomers;
DROP EXTERNAL DATA SOURCE RemoteNorthwindDB;
DROP DATABASE SCOPED CREDENTIAL jscredential;
DROP MASTER KEY;
So this allows you to do cross-database queries, but keep in mind the reasons this solution does not work as well as SQL Server:
- Additional coding to create the remote tables
- This is strictly a read of the data. You can’t use remote tables to write to the tables
- The databases you are trying to join could be located on different servers, introducing latency
- You can use Elastic Database Tools instead of using Elastic Database Queries, but this requires even more coding as you have to use C# libraries
- A work around could be to just put all the tables in one database, but that could require a lot of rewrites if moving an on-prem solution to the cloud, plus there is a 1TB database size limit in SQL Database
More info:
Elastic database query for cross-database queries (vertical partitioning)
