简体   繁体   中英

Automating database schema comparison using Microsoft technologies (SSDT)

SQL Server Data Tools (SSDT) includes a Schema Compare utility that you can use to compare two database definitions. The source and target of the comparison can be any combination of connected database, SQL Server database project, a snapshot, or .dacpac file. The results of the comparison appear as a set of actions that must be taken with the target to make it the same as the source. Once the comparison is complete you can update the target directly (if the target is a project or a database) or generate an update sql script .

Is there a way to automate the process of generating the update script using current (2017) Microsoft technologies?

Note: Automation can either be supported via a .net library or CLI.

There are few ways to do that, but the simplest one is to use MSBuild as suggested by @Crowcoder. These are the lines of code that I use in powershell script.

Extract dacpac from database (You might not need that, but if you do then you need sqlpackage for that. Possible location is C:\\Program Files (x86)\\Microsoft SQL Server\\140\\DAC\\bin\\SqlPackage.exe)

$cmd = "`"$sqlPackagePath`" /Action:Extract /tf:`"$dacpacPath`" /SourceConnectionString:`"$connString;Initial Catalog=$dbName;`""

Generate publish script (Possible location is C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\MSBuild.exe)

$cmd = "`"$msBuildPath`" `"$sqlProjPath`" /p:Configuration=Release /t:Build /p:SqlPublishProfilePath=`"$publishConfigPath`""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM