在SQLPackage实用工具中探索操作和任务

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实用程序中总共提供了七个主要操作:

  1. Extract – Used to create an extract from the database of a live SQL Server or Azure SQL Database 提取 –用于从实时SQL Server或Azure SQL数据库的数据库中创建提取
  2. Publish – Used to publish a DACPAC file from a local machine to the server 发布 –用于将DACPAC文件从本地计算机发布到服务器
  3. Export – Exports a live database from the server with data to a BACPAC file 导出 –将服务器上的实时数据库与数据一起导出到BACPAC文件
  4. Import – Used to import a BACPAC file to the SQL Server or Azure SQL Database 导入 –用于将BACPAC文件导入到SQL Server或Azure SQL数据库
  5. DeployReport – Provides a list of changes in XML that are going to be applied once the database is published DeployReport –提供数据库发布后将要应用的XML更改列表。
  6. DriftReport – Provides a list of changes in XML that has been applied to a database after being registered DriftReport –提供在注册后已应用于数据库的XML更改列表。
  7. Script – Generates a T-SQL script to incrementally update a database in SQL Server or Azure SQL Database 脚本 –生成T-SQL脚本以增量更新SQL Server或Azure SQL数据库中的数据库

Let us now go ahead and learn about each of these SQLPackage actions and how to execute these from the command line.

现在让我们继续学习每个SQLPackage操作,以及如何从命令行执行这些操作。

使用提取动作 (Using Extract action)

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文件,该文件将包含数据库中的架构和其他对象,但不包含数据。 此操作的参数如下:

  • Action – Extract 动作 –摘录
  • SourceDatabaseName – Name of the source database from which the DACPAC needs to be created SourceDatabaseName –需要从其创建DACPAC的源数据库的名称
  • SourceServerName – The name of the server from which the DACPAC is to be created SourceServerName –要从其创建DACPAC的服务器的名称
  • TargetFileName – Local path in which the DACPAC file is to be saved TargetFileName – DACPAC文件将保存到的本地路径

sqlpackage
/action:Extract
/TargetFile:”C:\temp\SQLShackSnapshotExtract.dacpac”
/SourceDatabaseName:SQLShackSnapshot
/SourceServerName:”localhost”

sqlpackage
/ action:提取
/TargetFile:“C:\temp\SQLShackSnapshotExtract.dacpac”
/ SourceDatabaseName:SQLShackSnapshot
/ SourceServerName:“本地主机”

在SQLPackage实用工具中探索操作和任务_第1张图片

Figure 1 – Extract Action

图1 –提取动作

使用发布动作 (Using Publish action)

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文件:

  • Action – Publish, since we are going to publish the DACPAC file to the database server 行动 –发布,因为我们要将DACPAC文件发布到数据库服务器
  • SourceFile – The path of the source (DAPAC) file in the local machine SourceFile –本地计算机中源(DAPAC)文件的路径
  • TargetDatabaseName – The name of the database in the target server TargetDatabaseName –目标服务器中数据库的名称
  • TargetServerName – The name of the target database server TargetServerName –目标数据库服务器的名称

sqlpackage
/action:Publish
/SourceFile:”C:\temp\SQLShackSnapshot.dacpac”
/TargetDatabaseName:SQLShackSnapshot
/TargetServerName:”localhost”

sqlpackage
/ action:发布
/SourceFile:“C:\temp\SQLShackSnapshot.dacpac”
/ TargetDatabaseName:SQLShackSnapshot
/ TargetServerName:“本地主机”

在SQLPackage实用工具中探索操作和任务_第2张图片

Figure 2 – Publish Action

图2 –发布动作

使用导出动作 (Using Export action)

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。 该文件通常较大,因为数据也会导出到该文件中。 可以按以下方式指定此操作的参数:

  • Action – Export. This is used to export the BACPAC file 行动 –出口。 这用于导出BACPAC文件
  • SourceDatabaseName – Name of the source database from which the BACPAC needs to be created SourceDatabaseName –需要从其创建BACPAC的源数据库的名称
  • SourceServerName – The name of the server from which the BACPAC is to be created SourceServerName –要从其创建BACPAC的服务器的名称
  • TargetFileName – Local path in which the BACPAC file is to be saved TargetFileName –将BACPAC文件保存到的本地路径

sqlpackage
/action:Export
/TargetFile:”C:\temp\SQLShackSnapshotExtract.bacpac”
/SourceDatabaseName:SQLShackSnapshot
/SourceServerName:”localhost”

sqlpackage
/ action:导出
/TargetFile:“C:\temp\SQLShackSnapshotExtract.bacpac”
/ SourceDatabaseName:SQLShackSnapshot
/ SourceServerName:“本地主机”

在SQLPackage实用工具中探索操作和任务_第3张图片

Figure 3 – Export Action

图3 –导出动作

使用导入动作 (Using Import action)

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文件时导出的数据。 用于使用“导入”操作的参数如下:

  • Action – Import, as the function is going to import a BACPAC file into the SQL Server 行动 –导入,因为该函数将BACPAC文件导入到SQL Server中
  • SourceFile – The path of the source (BAPAC) file in the local machine SourceFile –本地计算机中源(BAPAC)文件的路径
  • TargetDatabaseName – The name of the database in the target server TargetDatabaseName –目标服务器中数据库的名称
  • TargetServerName – The name of the target database server TargetServerName –目标数据库服务器的名称

sqlpackage
/action:Import
/SourceFile:”C:\temp\SQLShackSnapshotExtract.bacpac”
/TargetDatabaseName:SQLShackSnapshotImport
/TargetServerName:”localhost”

sqlpackage
/ action:导入
/SourceFile:“C:\temp\SQLShackSnapshotExtract.bacpac”
/ TargetDatabaseName:SQLShackSnapshotImport
/ TargetServerName:“本地主机”

在SQLPackage实用工具中探索操作和任务_第4张图片

Figure 4 – Import Action

图4 –导入动作

使用DeployReport操作 (Using DeployReport action)

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报告,该报告将列出发布后将要应用于数据库的更改。 此操作接受的参数如下:

  • Action – DeployReport. As we are going to generate an XML report 行动 – DeployReport。 当我们要生成XML报告时
  • SourceFile – Name of the source DACPAC file SourceFile –源DACPAC文件的名称
  • TargetFile – Name of the target DACPAC file TargetFile –目标DACPAC文件的名称
  • TargetDatabaseName – The name of the database in the target server TargetDatabaseName –目标服务器中数据库的名称
  • OutputPath – The path where the XML report will be stored after it is generated OutputPath –生成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”

在SQLPackage实用工具中探索操作和任务_第5张图片

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报告后,您可以使用任何代码编辑器轻松打开它,并按以下方式查看报告:

在SQLPackage实用工具中探索操作和任务_第6张图片

Figure 6 – Deployment Report

图6 –部署报告

使用DriftReport操作 (Using DriftReport action)

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报告,该报告将向我们提供在将数据库注册为数据层应用程序后已应用于数据库的更改列表。 生成漂移报告所需的参数如下:

  • Action – DriftReport 行动 – DriftReport
  • TargetDatabaseName – The name of the database in the target server TargetDatabaseName –目标服务器中数据库的名称
  • TargetServerName – The name of the target database server TargetServerName –目标数据库服务器的名称
  • OutputPath – The path where the XML report will be stored after it is generated OutputPath –生成XML报告后将在其中存储的路径

sqlpackage
/action:DriftReport
/TargetServerName:”localhost”
/TargetDatabaseName:SQLShackDemoDB
/OutputPath:”C:\temp\DriftReport.xml”

sqlpackage
/ action:漂移报告
/ TargetServerName:“本地主机”
/ TargetDatabaseName:SQLShackDemoDB
/OutputPath:“C:\temp\DriftReport.xml”

在SQLPackage实用工具中探索操作和任务_第7张图片

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:

生成漂移报告后,您可以通过在任何代码编辑器中打开文件来查看文件的详细信息。 在下图中,由于没有对数据库进行任何更改,因此漂移报告为空:

在SQLPackage实用工具中探索操作和任务_第8张图片

Figure 8 – Drift Report

图8 –漂移报告

使用脚本动作 (Using Script action)

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:

使用此操作的参数如下:

  • Action – Script. As we are going to generate an SQL script that will store the difference of the changes 动作 –脚本。 我们将生成一个SQL脚本来存储更改的差异
  • SourceFile – Name of the source DACPAC file SourceFile –源DACPAC文件的名称
  • TargetFile – Name of the target DACPAC file TargetFile –目标DACPAC文件的名称
  • TargetDatabaseName – The name of the database in the target server TargetDatabaseName –目标服务器中数据库的名称
  • OutputPath – The path where the T-SQL script will be stored after the comparison OutputPath –比较后将存储T-SQL脚本的路径

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”

在SQLPackage实用工具中探索操作和任务_第9张图片

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脚本存储在指定的路径中。 如下图所示,该脚本包含更改列表,需要将更改应用于数据库才能将其修改为第二个版本:

在SQLPackage实用工具中探索操作和任务_第10张图片

Figure 10 – Difference Script

图10 –差异脚本

结论 (Conclusion)

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/

你可能感兴趣的:(数据库,python,java,大数据,linux)