Deploying Changes with SqlPackage
This lesson explains the SqlPackage actions, publish properties and profiles, and safe deployment practices.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish between the SqlPackage actions.
- Explain the effect of BlockOnPossibleDataLoss and DropObjectsNotInSource.
- Describe practices that make database deployments safe.
SqlPackage actions
SqlPackage is a cross-platform command-line tool (installable as a .NET tool) whose actions are summarized in the following table.
| Action | Purpose |
|---|---|
| Publish | Update a database to match a dacpac |
| Script | Generate the T-SQL that Publish would run, for review |
| DeployReport | Generate an XML report of the changes Publish would make |
| DriftReport | Report changes made to a registered database since it was registered |
| Extract | Create a dacpac from an existing database's schema |
| Export / Import | Create or restore a bacpac containing schema and data |
TOKEN=$(az account get-access-token --resource https://database.windows.net/ --query accessToken -o tsv)
sqlpackage /Action:Script /SourceFile:bin/Debug/Inventory.dacpac \
/TargetServerName:sql-contoso.database.windows.net /TargetDatabaseName:Inventory \
/AccessToken:$TOKEN /OutputPath:deploy.sql
sqlpackage /Action:Publish /SourceFile:bin/Debug/Inventory.dacpac \
/TargetServerName:sql-contoso.database.windows.net /TargetDatabaseName:Inventory \
/AccessToken:$TOKEN /Profile:Inventory.Production.publish.xmlThe following diagram shows the SqlPackage actions around a dacpac.

SqlPackage actions
Publish properties and profiles
Publish behaviour is controlled by properties, set with /p:Name=Value or stored in a publish profile (.publish.xml) per environment. Important ones are listed in the following table.
| Property | Default | Effect |
|---|---|---|
| BlockOnPossibleDataLoss | True | Stops the deployment if a change could lose data, such as dropping or narrowing a column with data |
| DropObjectsNotInSource | False | Drops objects in the target that are not in the dacpac |
| GenerateSmartDefaults | False | Supplies defaults when adding NOT NULL columns to tables with data |
| BackupDatabaseBeforeChanges | False | Backs up the database before deploying (SQL Server) |
| BlockWhenDriftDetected | True | Blocks deployment to a registered database whose schema drifted |
| RegisterDataTierApplication | False | Registers the schema so DriftReport can detect later changes |
Publish properties
Safe deployment practices
A pipeline builds the dacpac once and deploys the same artifact to each environment in turn, so production receives exactly what was tested. Before production, a Script or DeployReport is generated and reviewed for unexpected drops or table rebuilds. Changes that the declarative model cannot express safely, such as splitting a column and moving its data, are handled in pre- and post-deployment scripts, or in several releases (add new column, migrate data, remove old column). Renames are recorded in the project's refactor log so SqlPackage renames objects instead of dropping and re-creating them. Vector indexes are not deployed with a dacpac when the table has fewer than 100 rows with vectors, so they may need a post-deployment step.
Safe deployments
A deployment that would drop data
A developer removes an obsolete column from a table in the project. The production deployment fails with a message that the column contains data. The data has been archived and the column must be removed. What should the team do?
Review the DeployReport, confirm the archive, and deploy that release with /p:BlockOnPossibleDataLoss=False (in the production profile for that deployment only). The default True value is a safety net that stops accidental data loss, so it should be overridden deliberately, not removed permanently. DropObjectsNotInSource controls dropping whole objects that are not in the project and does not affect columns removed from a table definition.
What is the default value of BlockOnPossibleDataLoss?
Show answer
True, so deployments that could lose data stop unless it is deliberately overridden.
Exam Tips
Remember for the exam
- Deploy a dacpac: SqlPackage Publish; preview: Script or DeployReport.
- BlockOnPossibleDataLoss defaults to True; override deliberately for intended drops.
- DropObjectsNotInSource defaults to False.
- Build once, deploy the same dacpac to every environment.