SQL Server Automating database schema comparison using Microsoft technologies (SSDT)

brqmpdu1  于 2023-08-02  发布在  其他
关注(0)|答案(1)|浏览(104)

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.

tvokkenx

tvokkenx1#

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`""

相关问题