In this article, I am going to explain in detail about actions and tasks in the SQLPackage Utility. In my previous article, I have explained the overview of the SQLPackage utility. The SQLPackage utility is a command-line utility tool provided by Microsoft to automate SQL Server database deployments. Since this utility is cross-platform, you can easily install it on any operating system of your choice. SQLPackage actions are the types of functions that we can achieve with this utility. You can install this utility directly on any production server and it can be used as is. You can find more information about this utility and SQLPackage actions from the official documentation from Microsoft.
在本文中,我将详细解释有关SQLPackage实用程序中的操作和任务的信息。 在上一篇文章中,我解释了SQLPackage实用工具的概述。 SQLPackage实用工具是Microsoft提供的用于自动执行SQL Server数据库部署的命令行实用工具。 由于该实用程序是跨平台的,因此您可以轻松地将其安装在您选择的任何操作系统上。 SQLPackage操作是我们可以使用该实用程序实现的功能类型。 您可以将此实用程序直接安装在任何生产服务器上,并且可以按原样使用。 您可以从Microsoft的官方文档中找到有关此实用程序和SQLPackage操作的更多信息。
In total, seven primary actions are provided in the SQLPackage utility:
SQLPackage实用程序中总共提供了七个主要操作:
Let us now go ahead and learn about each of these SQLPackage actions and how to execute these from the command line.
现在让我们继续学习每个SQLPackage操作,以及如何从命令行执行这些操作。
This action is used to create a DACPAC file from an existing database in the SQL Server or Azure SQL Database. Using this action, you can easily create a DACPAC file that will contain the schema and other objects in the database, but no data. The parameters for this action are as follows:
此操作用于从SQL Server或Azure SQL数据库中的现有数据库创建DACPAC文件。 使用此操作,您可以轻松创建一个DACPAC文件,该文件将包含数据库中的架构和其他对象,但不包含数据。 此操作的参数如下:
sqlpackage
/action:Extract
/TargetFile:”C:\temp\SQLShackSnapshotExtract.dacpac”
/SourceDatabaseName:SQLShackSnapshot
/SourceServerName:”localhost”
sqlpackage
/ action:提取
/TargetFile:“C:\temp\SQLShackSnapshotExtract.dacpac”
/ SourceDatabaseName:SQLShackSnapshot
/ SourceServerName:“本地主机”
Figure 1 – Extract Action
图1 –提取动作
As discussed above, the Publish action is used to deploy a DACPAC file incrementally to the database server. If the database does not exist, then a new database will be created. However, if the database already exists, then the utility will compare the differences between the source (DACPAC) and the destination (SQL Server Database) and generate the necessary scripts to update the database.
如上所述,发布操作用于将DACPAC文件增量部署到数据库服务器。 如果数据库不存在,那么将创建一个新数据库。 但是,如果数据库已经存在,则该实用程序将比较源(DACPAC)和目标(SQL Server数据库)之间的差异,并生成必要的脚本来更新数据库。
We can use the following parameters for this action to publish a DACPAC file:
我们可以使用以下参数来执行此操作以发布DACPAC文件:
sqlpackage
/action:Publish
/SourceFile:”C:\temp\SQLShackSnapshot.dacpac”
/TargetDatabaseName:SQLShackSnapshot
/TargetServerName:”localhost”
sqlpackage
/ action:发布
/SourceFile:“C:\temp\SQLShackSnapshot.dacpac”
/ TargetDatabaseName:SQLShackSnapshot
/ TargetServerName:“本地主机”
Figure 2 – Publish Action
图2 –发布动作
The Export action is somewhat like the Extract function, except the fact that this action exports the data along with the schema of the database. The output file in case of the Export action is a BACPAC and not DACPAC. This file is usually bigger in size as the data is also exported in the file. The parameters for this action can be specified as follows:
“导出”操作与“提取”功能有些类似,不同之处在于该操作将数据与数据库的模式一起导出。 如果执行“导出”操作,则输出文件是BACPAC,而不是DACPAC。 该文件通常较大,因为数据也会导出到该文件中。 可以按以下方式指定此操作的参数:
sqlpackage
/action:Export
/TargetFile:”C:\temp\SQLShackSnapshotExtract.bacpac”
/SourceDatabaseName:SQLShackSnapshot
/SourceServerName:”localhost”
sqlpackage
/ action:导出
/TargetFile:“C:\temp\SQLShackSnapshotExtract.bacpac”
/ SourceDatabaseName:SQLShackSnapshot
/ SourceServerName:“本地主机”
Figure 3 – Export Action
图3 –导出动作
The Import action is just the opposite of the Export action. Using this action, you can easily restore a BACPAC file back to the database server in SQL Server or Azure SQL Database. This will restore the schema as well as the data which was exported while creating the BACPAC file. The parameters used to use the Import action are as follows:
导入操作与导出操作相反。 使用此操作,您可以轻松地将BACPAC文件还原回SQL Server或Azure SQL数据库中的数据库服务器。 这将还原架构以及在创建BACPAC文件时导出的数据。 用于使用“导入”操作的参数如下:
sqlpackage
/action:Import
/SourceFile:”C:\temp\SQLShackSnapshotExtract.bacpac”
/TargetDatabaseName:SQLShackSnapshotImport
/TargetServerName:”localhost”
sqlpackage
/ action:导入
/SourceFile:“C:\temp\SQLShackSnapshotExtract.bacpac”
/ TargetDatabaseName:SQLShackSnapshotImport
/ TargetServerName:“本地主机”
Figure 4 – Import Action
图4 –导入动作
The DeployReport action can be used to prepare an XML report that will list the changes that are going to be applied to a database once it is published. The parameters accepted by this action are as follows:
DeployReport操作可用于准备XML报告,该报告将列出发布后将要应用于数据库的更改。 此操作接受的参数如下:
sqlpackage
/action:DeployReport
/SourceFile:”C:\temp\SnapshotV1.dacpac”
/TargetFile:”C:\temp\SnapshotV2.dacpac”
/TargetDatabaseName:SQLShackDemoDB
/OutputPath:”C:\temp\DeployReport.xml”
sqlpackage
/ action:DeployReport
/SourceFile:“C:\temp\SnapshotV1.dacpac”
/TargetFile:“C:\temp\SnapshotV2.dacpac”
/ TargetDatabaseName:SQLShackDemoDB
/OutputPath:“C:\temp\DeployReport.xml”
Figure 5 – DeployReport Action
图5 – DeployReport操作
Once the XML report is generated, you can easily open it with any code editor and view the report as follows:
生成XML报告后,您可以使用任何代码编辑器轻松打开它,并按以下方式查看报告:
Figure 6 – Deployment Report
图6 –部署报告
The DriftReport action is used to generate an XML report that will give us the list of changes that has been applied to a database after it was registered as a data-tier application. The parameters required to generate the drift report are as follows:
DriftReport操作用于生成XML报告,该报告将向我们提供在将数据库注册为数据层应用程序后已应用于数据库的更改列表。 生成漂移报告所需的参数如下:
sqlpackage
/action:DriftReport
/TargetServerName:”localhost”
/TargetDatabaseName:SQLShackDemoDB
/OutputPath:”C:\temp\DriftReport.xml”
sqlpackage
/ action:漂移报告
/ TargetServerName:“本地主机”
/ TargetDatabaseName:SQLShackDemoDB
/OutputPath:“C:\temp\DriftReport.xml”
Figure 7 – DriftReport Action
图7 – DriftReport操作
Once the drift report has been generated, you can view the details of the file by opening it in any code editor. In the following figure, since there are no changes applied to the database, the drift report is empty:
生成漂移报告后,您可以通过在任何代码编辑器中打开文件来查看文件的详细信息。 在下图中,由于没有对数据库进行任何更改,因此漂移报告为空:
Figure 8 – Drift Report
图8 –漂移报告
This option compares the source DACPAC file with the one in the server and then creates a difference of the T-SQL changes that needs to apply to modify the database. This action can also be used to compare two different DACPAC files and generate a script with the difference between them. These changes are written to a script file on the local machine which can then be provided to the DBA for execution in the production environment.
此选项将源DACPAC文件与服务器中的文件进行比较,然后创建需要修改数据库的T-SQL更改的差异。 此操作还可以用于比较两个不同的DACPAC文件,并生成一个脚本,它们之间存在差异。 将这些更改写入本地计算机上的脚本文件,然后可以将其提供给DBA以在生产环境中执行。
The parameters to use this action are as follows:
使用此操作的参数如下:
sqlpackage
/action:Script
/SourceFile:”C:\temp\SnapshotV1.dacpac”
/TargetFile:”C:\temp\SnapshotV2.dacpac”
/TargetDatabaseName:SQLShackDemoDB
/OutputPath:”C:\temp\SnapshotDiff.sql”
sqlpackage
/ action:脚本
/SourceFile:“C:\temp\SnapshotV1.dacpac”
/TargetFile:“C:\temp\SnapshotV2.dacpac”
/ TargetDatabaseName:SQLShackDemoDB
/OutputPath:“C:\temp\SnapshotDiff.sql”
Figure 9 – Script Action
图9 –脚本动作
The SQL script generated by this action is stored in the path specified. As you can see in the figure below, the script contains the list of changes that needs to be applied to the database in order to modify it to the second version:
此操作生成SQL脚本存储在指定的路径中。 如下图所示,该脚本包含更改列表,需要将更改应用于数据库才能将其修改为第二个版本:
Figure 10 – Difference Script
图10 –差异脚本
In this article, we have explored all the SQLPackage actions that are provided with the SQLPackage utility. These actions are useful to automate database deployment in the SQL Server environment. The SQLPackage actions can be used against any database starting from SQL Server 2008 and Azure SQL Database as well.
在本文中,我们研究了SQLPackage实用程序提供的所有SQLPackage操作。 这些操作对于在SQL Server环境中自动化数据库部署很有用。 SQLPackage操作可用于从SQL Server 2008和Azure SQL Database开始的任何数据库。
翻译自: https://www.sqlshack.com/exploring-the-sqlpackage-actions/