Hi,@Anonymous
Welcome to Microsoft T-SQL Q&A Forum!
Azure SQL Database does not support cross-database and cross-instance queries using three-part or four-part names, so depending on your needs, Make sure the source database will be an Azure SQL Managed Instance And The target database will be an Azure SQL Database.
Steps required for the target database (Azure SQL Database):
- Create the required login, user and credentials on Azure SQL Database: --master database
create login LgTest with password = 'STRONGP@123'
-- user database
create user User1 for login LgTest
alter role [db_owner] add Member [User1]
-- user database
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'STRONGP@123';
GO
-- user database
CREATE DATABASE SCOPED CREDENTIAL User1
WITH IDENTITY = 'LgTest', SECRET = 'STRONGP@123' -- this user needs to have sufficient rights on your Azure SQL MI.
GO
2) Create an external data source that contains a connection to Azure SQL Managed Instance, you can create it with the following script after adding your location (FQDN), database name, and CREDENTIAL created earlier:
CREATE EXTERNAL DATA SOURCE RemoteReferenceData -- create the external data
WITH
( TYPE=RDBMS,
LOCATION='tcp:******.public.******.database.windows.net,3342',
DATABASE_NAME='*****',
CREDENTIAL= User1, -- the comma is important to work
);
3) Create the external table as follows, note that the external table structure should match the original table in Azure SQL Managed Instance:
-- the external table should be same as the table in your Azure SQL managed database.
CREATE EXTERNAL TABLE [dbo].[DBtable](
[col1] [nvarchar](max) NULL,
[col2] [nvarchar](max) NULL,
[col3] [nvarchar](max) NULL)
WITH
(
DATA_SOURCE= RemoteReferenceData
);
After completing these steps, you should be able to see the created external table and external data source in Azure SQL Database as shown below:
![184326-image.png](https://learn-attachment.microsoft.com/api/attachments/184326-image.png?platform=QnA)
Reminder
: When trying the above steps, you must first create and allow required logins and users to access your Azure SQL Managed Instance in the Create and allow required logins and users to access your Azure SQL Managed Instance.
Best regards,
Bert Zhou
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.