SQL Server Schema and data comparison using SSDT

zzoitvuj  于 2023-08-02  发布在  SQL Server
关注(0)|答案(1)|浏览(135)

I'm trying to do data comparison and automate database upgrade script generation by using SQL Server Database tools > SchemaComparison/DataComparison .

Using VS command window, I am able to run

> Tools.NewDataComparison [args...]
> SQL.DataCompareExportToFile [file]

which produces .sql file containing required inserts/updates/deletes.

Now I would like to go a step further and automate this.

I have been looking at Microsoft.VisualStudio.Data.Tools.DataCompare, Microsoft.SqlServer.Dac and similar assemblies but haven't found any of the methods above. Which dll exposes these?

I have also tried to run these methods with devenv.exe (which only starts VS and executes the arguments in command window) but was only successful at running Tools.NewDataComparison . Is there a way to chain or reuse the context, so I can run the SQL.DataCompareExportToFile afterwards?

I know I can use SqlPackage.exe to export dacpac and bacpac , but comparison is possible only for schema. Data comparison is not. Is there a way to use bacpac for comparison?

I would accept any solution which would allow me to compare the databases and generate the upgrade .sql script. Either in C# or any other script.

xxe27gdn

xxe27gdn1#

Unfortunately I cannot comment to ask for clarification. If this is a DB project in Visual Studio you can setup Pre and Post execution scripts to manage data updates.

We break ours down into

Reference Data - Static lookup data, we kill and fill so Ref data is always clean and controlled.

Migration Data - Copies data from another source and is based on what release is being run and only executes once.

Sandbox Data - Aligns data for repeatable test by the front-end. Think of unit testing. Only runs on non-Prod environments.

So once you have this in place, then we use SqlPackage to upgrade the DAC which includes the Pre and Post scripts.

If you are talking about actually comparing 2 databases, then I only know client tools like VS Data Compare and Red Gate that could do that. But you could try using Merge between tables or maybe explore an SSIS solution.

相关问题