Den här sidan finns ännu inte på svenska och visas därför på engelska.
SQL Schema Comparison
How to compare SQL Server schemas with SSDT and sqlpackage, what to ignore, how to read and review the generated script safely, and which changes can lose data.
Schema comparison means taking two database schemas, two live databases or a database and a project in source control, and listing every difference between them: missing tables, extra columns, changed procedures, different indexes. It is how you check that environments match, how you work out what a deployment will change, and how you generate the script to bring one schema in line with another.
This guide covers the main tools, how to set up a comparison so the results are useful, and, most importantly, how to review the generated changes safely before running them anywhere that matters.
The main tools
For SQL Server, the most widely used options are:
- **SQL Server Data Tools (SSDT)** in Visual Studio, whose Schema Compare window compares databases, projects and DACPAC files visually and can generate an update script.
- **sqlpackage**, Microsoft's command-line tool, which can extract a schema to a DACPAC file, compare it with a database and produce a deployment script or a report of the changes. It runs on Windows, Linux and macOS and is easy to automate.
- **Third-party comparison tools**, which offer similar features with their own interfaces, filters and reporting.
All of them work in a similar way: build a model of each schema, compare the models and work out the changes needed to make the target match the source.
Compare with sqlpackage
`sqlpackage` is a good fit for regular, automated comparisons. A typical pattern is to extract the schema of a reference database, or build it from source control, as a DACPAC, then generate a report and a script of what would change on the target:
```powershell
sqlpackage /Action:Extract /SourceServerName:test-sql01 /SourceDatabaseName:SalesDb /TargetFile:SalesDb.dacpac
sqlpackage /Action:DeployReport /SourceFile:SalesDb.dacpac `
/TargetServerName:prod-sql01 /TargetDatabaseName:SalesDb /OutputPath:changes.xml
sqlpackage /Action:Script /SourceFile:SalesDb.dacpac `
/TargetServerName:prod-sql01 /TargetDatabaseName:SalesDb /OutputPath:changes.sql
```
The DeployReport lists every operation the deployment would perform, and flags operations that could cause data loss. The Script action produces the actual T-SQL, which you can review before running anything.
Decide what to ignore
Out of the box, a comparison reports every difference, including many you don't care about. Configure it to ignore the noise, so real differences stand out:
- **Whitespace, comments and keyword casing** in code objects.
- **Users, logins, roles and permissions**, which usually differ between environments on purpose.
- **File and filegroup sizes**, and other physical storage details.
- **Column order**, if your team doesn't care about it. Some tools will rebuild a table just to reorder columns, which is expensive and rarely necessary.
- **Collation** differences, if they are expected, although these deserve a deliberate decision.
Save these options in a profile or publish settings file, so every comparison uses the same rules.
Review the script before running it
A generated schema script is a starting point, not something to run blindly. Read it with these questions in mind:
- **Will any data be lost?** Dropping a column or table, or shrinking a column's size, can discard data. Most tools can be set to stop if a change would lose data; keep that setting on.
- **Will any table be rebuilt?** Some changes, such as inserting a column in the middle or changing certain column types, are done by creating a new table, copying the data and swapping it in. On a large table that can take a long time and needs a lot of space and log.
- **Will anything be locked for a long time?** Adding an index or a constraint on a large table can block other work. Consider doing those online, or in a maintenance window.
- **Is anything being dropped that shouldn't be?** An object that exists only in the target, such as an emergency index, will be dropped if the comparison is set to remove extra objects. Decide whether that is what you want.
- **Is the order sensible?** Check that dependent objects are created after the things they depend on.
On anything important, run the script against a restored copy of the target first, and time it.
Compare regularly, not just at deployment
Comparison is most useful as a routine check rather than a last-minute step. Comparing production with source control nightly, and reporting differences, catches drift early, while it is still small and easy to explain. See our guide to [schema drift detection](/guides/schema-drift-detection) for more on finding and preventing drift.
Troubleshooting Scenarios
The comparison shows hundreds of differences
Most are probably permissions, whitespace or storage settings. Add ignore options for those and run it again.
The script wants to rebuild a large table
Check why. Column order or a type change is often the cause; reconsider the change or ignore column order.
The deployment would drop an index that only production has
It is probably an emergency or tuning index. Add it to source control if it should stay, or exclude it from the comparison.
The script ran in test but failed in production
Production had drifted. Generate the script against production itself, not against a test copy.
Where SQL Cockpit fits
Schema comparison is easier when you can explore the objects involved, and what depends on them, before deciding on a change. SQL Cockpit includes object search and schema exploration across connected SQL Server databases, which helps when reviewing the differences a comparison tool reports. The comparison and deployment themselves are best done with tools such as SSDT and sqlpackage as described here.
FAQ
What is SQL schema comparison?
It is listing the differences between two database schemas, such as two environments or a database and source control, and optionally generating a script to align them.
Which tools can compare SQL Server schemas?
SQL Server Data Tools, the sqlpackage command-line tool and several third-party schema comparison products.
Can a schema comparison script lose data?
Yes. Dropping or shrinking columns and dropping tables can lose data, so review the script and keep data loss protection turned on.
Why does a schema change rebuild my table?
Some changes, such as reordering columns or certain type changes, are done by copying the table. On large tables that can be slow and costly.
What should I ignore in a schema comparison?
Usually whitespace, comments, casing, permissions, users and storage settings, and possibly column order.
Semantic Keywords
- SQL schema comparison
- schema drift
- table synchronisation
- environment comparison
- migration orchestration
- SQL Server monitoring
- SQL Agent monitoring
- runtime analysis
- runtime baselines
- runtime variance
- SQL Server operations
- database telemetry
- historical analysis
- operational dashboards
- cross-server monitoring