How to copy Azure SQL Database between tenants or subscriptions

How to copy Azure SQL Database between tenants or subscriptions

You have a massive database in one subscription or tenant, and you need to copy the database to another subscription/tenant.  As here also explains The Azure portal, PowerShell, and the Azure CLI do not support database copy to a different subscription. You can use T-SQL if only using a SQL authentication login to log in to the target server.  Copy a database - Azure SQL Database | Microsoft Learn

Use SqlPackage to copy Azure SQL Database:

Use SqlPackage is a great way to copy database, it’s a command line interface and it works in Windows, Linux and Mac. Here I want to explain how I used SqlPackage to export and import the database between tenants.

1- Download and install SQLPackage

First download the latest version from here Download and install SqlPackage - SQL Server | Microsoft Learn

After installing SQLPackage go to the installed folder C:\Program Files\Microsoft SQL Server\160\DAC\bin, You need to run CMD from this location.

Install SQL Package

You can see all command line syntax from this referral link SqlPackage.exe - SQL Server | Microsoft Learn, before you starting I suggest to check “Set server firewall” and make sure your IP is in the firewall rules.

So in the first step I started with Version and as you see I am using the latest version

 
sqlpackage.exe /Version
Export and Import database with SQL Package

Export Database

Export with the following command, You only need to replace your path, server and database name as well as username and password to connect to the database.

 
SqlPackage /Action:Export /TargetFile:"C:\SQLPackageFiles\ExportHere.bacpac" /df:"C:\SQLPackageFiles\log.txt" /SourceConnectionString:"Server=tcp:xxx.database.windows.net,1433;Initial Catalog=YourDatabaseNameHere;Persist Security Info=False;User ID=YourUserName;Password=YourPassword;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
Export Azure SQL Database with SQLPackage

Using /df:"C:\SQLPackageFiles\log.txt" is also helpful as you can have extra details.

3- Import the database to new subscription/ tenants

For import new database in the target you do not need to create or prepare anything, the only thing you need to run the following command and put your parameter there!

 
SqlPackage /Action:Import /SourceFile:"C:\SQLPackageFiles\ExportHere.bacpac" /df:"C:\SQLPackageFiles\log_import.txt" /TargetConnectionString:"Server=tcp:YourTargetServerName.windows.net,1433;Initial Catalog=MyNewDatabase;Persist Security Info=False;User ID=YourUserName;Password=YourPassword;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"

After only 3 minutes I restored the database in the target, If I go to the portal I can see my new database has been created.

How to copy Azure SQL Database between tenants or subscriptions 

Experience slowness for import and export?

If you are experiencing slowness for importing and exporting database you can check the log files which I mentioned earlier.

Import/Export service performs a logical backup of the database the time to complete is more dependent on the number of objects in the database than a traditional physical database backup. As also mentions here Import and export of a database takes a long time - Azure SQL Database | Microsoft Learn. In some cases you might need to change the service tier to higher tier then If the database has reached its resource limits, you can change it to what you want, see the referral link to compare Azure Service tier DTU resource limits single databases - Azure SQL Database | Microsoft Learn

Ideally, you should run client applications (like the sqlpackage utility or your custom DAC application) from a VM in the same region as your database. Otherwise, you might experience performance issues related to network latency.