Using SQLPackage to import or export SQL Server and Azure SQL DB
Published Mar 14 2019 01:38 AM 57.6K Views

First published on MSDN on Jan 31, 2017, Updated Feb 7th, 2023

Purpose:

Explain how to easily and quickly use SQLPackage to import or export your SQL Server and Azure SQL Database.

 

Locating the SQLPackage.exe:

The SQLPackage.exe is part of the DacFramework which installed with SSMS or with SQL Server Data Tools
Or you can download only the DacFramework if you do not have already the management tools

 

Export from Azure SQL DB to bacpac file:

to export database from your Azure SQL DB to bacpac file use this command:
sqlpackage.exe /Action:Export /ssn:tcp:<ServerName>.database.windows.net,1433 /sdn:<DatabaseName> /su:<UserName> /sp:<Password> /tf:<TargetFile>

 

Example:

Export Azure SQL Database to bacpac file. 

 

sqlpackage.exe /Action:Export /ssn:tcp:MyOwnServer.database.windows.net,1433 /sdn:AdventureWorks /su:AdminUser /sp:AdminPassword1 /tf:C:\Temp\AW.bacpac

 

Export Azure SQL Database to bacpac file and collect basic log to a file

 

sqlpackage.exe /Action:Export /ssn:tcp:MyOwnServer.database.windows.net,1433 /sdn:AdventureWorks /su:AdminUser /sp:AdminPassword1 /tf:C:\Temp\AW.bacpac /df:C:\Temp\AWExport.log

 

Export Azure SQL Database to bacpac file and collect diagnostics log to a file

 

sqlpackage.exe /Action:Export /ssn:tcp:MyOwnServer.database.windows.net,1433 /sdn:AdventureWorks /su:AdminUser /sp:AdminPassword1 /tf:C:\Temp\AW.bacpac /df:C:\Temp\AWExport.log /d

 

 

 

Import from bacpac file to Azure SQL DB:

to import database from bacpac file to your Azure SQL DB use this command:
sqlpackage.exe /Action:Import /tsn:tcp:<ServerName>.database.windows.net,1433 /tdn:<TargetDatabaseName> /tu:<UserName> /tp:<Password> /sf:<Path to bacpac file> /p:DatabaseEdition=Premium /p:DatabaseServiceObjective=P4

example:

import database to default target service tier (Gen5 General Purpose with 2 vCore):

 

sqlpackage.exe /Action:Import /tsn:tcp:MyServer.database.windows.net,1433 /tdn:AdventureWorks /tu:AdminUser /tp:AdminPassword1 /sf:C:\temp\AW.bacpac

 

import database and set target service tier to P2

 

sqlpackage.exe /Action:Import /tsn:tcp:MyServer.database.windows.net,1433 /tdn:AdventureWorks /tu:AdminUser /tp:AdminPassword1 /sf:C:\temp\AW.bacpac /p:DatabaseEdition=Premium /p:DatabaseServiceObjective=P2

 

 

Remarks:

    • /p:Storage=File : is used to redirect the backing storage for the schema model used during extraction, this helpful with large databases that may cause out-of-memory exception if the default memory location is used. 
    • Note that for large databases import its recommended setting the service tier during the import, this help import process to run faster.
    • Note that the database can be created prior to import, the only condition is that the database will be empty. this allows you to set database properties before import starts.
    • For large database export you might want to set the temp folder location, as by default it will use C: drive. 

      you may run the following commands on your command line window before executing SQLPackage.

      SET TEMP={path to your temp folder}
      SET TMP={path to your temp folder}​
    • if operation logs is needed please add /df:<filepath> to get the logs for the operation, by default the screen output will be captured, to add diagnostics information (more detailed logging) use the /d swicth 

 

 

    • When exporting from the active database, the database may not be transactional consistent as this process go object by object. If transactional consistent is needed it's recommended to export from the copied database. ( learn how to copy Azure SQL DB ) (thanks to ErikEJ for his comment)


Full documentation for SQLPackage.exe

14 Comments
Co-Authors
Version history
Last update:
‎Feb 07 2023 02:27 AM
Updated by: