3

I have used Scoped Database Credentials, External Data Sources and External Tables in the past to build links in one database to tables in a separate database. However, up to this point, I have only found this to work if the table names are different (either by name or by schema) in both databases. This is because the linkage expects that the name of the external table as defined in the 'source' database is going to exactly match the name of the table in the 'target' database.

What I have is two databases which are 'mirror images' of each other. Each database has a table named [dbo].[Customers]. In DatabaseA, which already has a table named [dbo].[Customers], how can I link to [dbo].[Customers] in DatabaseB? SQL won't allow me to build an EXTERNAL table named [dbo].[Customers] in DatabaseA because it already exists. I can build an EXTERNAL table named [ext].[Customers] but then that doesn't map to the table in DatabaseB because there is no [ext].[Customers] in DatabaseB.

So this is what my question boils down to - is there a way to create an EXTERNAL table named [ext].[Customers] in DatabaseA and map it to [dbo].[Customers] in DatabaseB?

3
  • If you're doing this a lot you should consider putting all tables in the same database in a different schema Commented Aug 17, 2020 at 23:06
  • I guess in some applications that would be appropriate. What we have here are mirrored staging and production environments so separate databases seems more appropriate. Sometimes though we need to push staged data through to production and up until now it's been very cumbersome. I intend to script this; create the link, publish the data and then drop the linkage so that the two databases are once again separate once the operation is complete. Commented Aug 17, 2020 at 23:22
  • Have you considered building this process in data factory? Then all of your ETL is outside the database and you don't have to run any scripts against your prod database at all. ADF is great for metadata driven operations, i.e. tell it a table and it does it all without requiring column mappings etc. Commented Aug 18, 2020 at 1:01

1 Answer 1

6
Answer recommended by Microsoft Azure Collective

You can specify the remote schema and object name in CREATE EXTERNAL TABLE, eg

CREATE EXTERNAL TABLE ext.Customers
(   
  [CustomerID] int NOT NULL,
  [CustomerName] varchar(50) NOT NULL,
  [Company] varchar(50) NOT NULL
)
WITH
( 
  DATA_SOURCE = MyElasticDBQueryDataSrc,
  SCHEMA_NAME = N'dbo',  
  OBJECT_NAME = N'Customers'  
)
Sign up to request clarification or add additional context in comments.

3 Comments

Quick follow-up - will a stored procedure transaction successfully span both databases?
We were struggling to setup Azure Sync due to FW issues and dealing with so many DB's via the UI. Azure Elastic Query was way simpler to setup and more flexible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.