Step 1

Create two different databases using one of the ways given below in Windows Azure portal (https://portal.azure.com)

Way 1

New - SQL databases - Add.

Azure

Way 2

New - Databases - SQL database.

Azure

Please fill in the details on the screen given below.

Azure

If you do not have resource group, please select Create new.

If you do not have Server, create new Server, using the screen given below.

Please note the Server admin login & Password. This will be required for the further process.

Azure

Once you have filled all the required information, please click Create.

Repeat the process given above to create another database.

In this case, we have created two different databases, as shown below.

  1. myDemoDB1
  2. myDemoDB2

    Azure

Once the databases are created, you need to set the Firewall rules (either Server level or database level) depending on your requirement.

For setting rules, you can refer to

https://docs.microsoft.com/en-us/azure/sql-database/sql-database-firewall-configure

Step 2

Writing cross database query

Open SQL Server Management Studio and connect to the Server, which we have created, using the Server admin login & Password, which we have used while creating the Server.

You can get the Server name by going to Yourdatabasename - Overview - Server name.

Azure

Now, create two different tables in two databases.

  1. Employee in myDemoDB1
    1. USE [myDemoDB1]
    2. GO
    3. /****** Object: Table [dbo].[Employee] Script Date: 04/05/2017 15:14:33 ******/
    4. SET ANSI_NULLS ON
    5. GO
    6. SET QUOTED_IDENTIFIER ON
    7. GO
    8. CREATE TABLE [dbo].[Employee](
    9. [Id] [int] NOT NULL,
    10. [FirstName] [varchar](50) NULL,
    11. [LastName] [varchar](50) NULL,
    12. [DeptId] [int] NULL
    13. )
    14. GO
  1. Department in myDemoDB2
    1. USE [myDemoDB2]
    2. GO
    3. /****** Object: Table [dbo].[Department] Script Date: 04/05/2017 15:17:02 ******/
    4. SET ANSI_NULLS ON
    5. GO
    6. SET QUOTED_IDENTIFIER ON
    7. GO
    8. CREATE TABLE [dbo].[Department](
    9. [DeptId] [int] NOT NULL,
    10. [Name] [varchar](50) NULL
    11. )
    12. GO

To access the table from the remote database, we need to create elastic database query.

For this, we need to define an external data source, which will point to the target database.

  1. CREATE EXTERNAL DATA SOURCE RefmyDemoDB2
  2. WITH
  3. (
  4. TYPE=RDBMS,
  5. LOCATION='your server name',
  6. DATABASE_NAME='myDemoDB2',
  7. CREDENTIAL= your “Server admin login”
  8. );

The query given above has created a reference to the target database (In this case, it is myDemoDB2).

Now, using the reference given above, we can create an external table reference.

  1. CREATE EXTERNAL TABLE [dbo].[Department](
  2. [DeptId] [int] NOT NULL,
  3. [Name] [varchar] (50) NULL
  4. )
  5. WITH
  6. (
  7. DATA_SOURCE = RefmyDemoDB2
  8. );

If you have noticed that we have used CREDENTIAL but never specified the password for it, so it’ll give you an error.

Azure

Thus, let’s modify our query to include the user credentials

The query given below will create user credentials for you.

  1. CREATE DATABASE SCOPED CREDENTIAL yourServeradminlogin
  2. WITH IDENTITY = 'yourServeradminlogin',
  3. SECRET = 'yourPassword';

Let’s encrypt these credentials, using Master Key Encryption.

  1. CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'yourPassword';

Finally, our full query will look, as shown below.

  1. DROP EXTERNAL TABLE [Department]
  2. DROP EXTERNAL DATA SOURCE RefmyDemoDB2
  3. DROP DATABASE SCOPED CREDENTIAL yourServeradminlogin
  4. DROP MASTER KEY
  5. CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'yourPassword';
  6. CREATE DATABASE SCOPED CREDENTIAL yourServeradminlogin
  7. WITH IDENTITY = 'yourServeradminlogin',
  8. SECRET = 'yourPassword';
  9. CREATE EXTERNAL DATA SOURCE RefmyDemoDB2
  10. WITH
  11. (
  12. TYPE=RDBMS,
  13. LOCATION='testdbdemoserver.database.windows.net',
  14. DATABASE_NAME='myDemoDB2',
  15. CREDENTIAL= yourServeradminlogin
  16. );
  17. CREATE EXTERNAL TABLE [dbo].[Department](
  18. [DeptId] [int] NOT NULL,
  19. [Name] [varchar](50) NULL
  20. )
  21. WITH
  22. (
  23. DATA_SOURCE = RefmyDemoDB2
  24. );
  25. /****** Script for SelectTopNRows command from SSMS ******/
  26. SELECT *
  27. FROM [dbo].[Employee] E
  28. INNER JOIN [dbo].[Department] D
  29. ON E.DeptId = D.DeptId

The output of the query will look, as shown below.

Please note the Department table under External Tables. This acts as a link between the two different databases.

Azure

Now, you can write cross database queries in Azure SQL, using the steps mentioned above.