[英]Can Sql Package Exe Run Post Deployment Script In Command Line?
The following will deploy Sql database with sqlpackage.exe. 下面将使用sqlpackage.exe部署Sql数据库。 This does not require use of Publish Profile Xml.
这不需要使用发布配置文件Xml。 Is there a way to add simple post-deployment script inline, without having external post-deployment file?
有没有一种方法可以内联添加简单的部署后脚本,而无需使用外部部署后文件?
Eg: will publish database inline, without a publish profile xml. 例如:将内联发布数据库,而没有发布配置文件xml。
SqlPackage.exe
/Action:Publish
/SourceFile:TestDatabase.dacpac
/TargetDatabaseName:TestDb
/TargetServerName:localhost
Now, goal to add post deployment script in command line, eg insert value in sample table. 现在,目标是在命令行中添加部署后脚本,例如在示例表中插入值。
Intended Goal: 预期目标:
SqlPackage.exe
/Action:Publish
/SourceFile:TestDatabase.dacpac
/TargetDatabaseName:TestDb
/TargetServerName:localhost
/PostDeploymentScript:"insert into dbo.SampleTable (SampleColumn) values ('1')"
Cannot find Post Deployment script inline option in Microsoft website. 在Microsoft网站中找不到内联部署后脚本选项。 Maybe it doesn't exist.
也许它不存在。
https://docs.microsoft.com/en-us/sql/tools/sqlpackage?view=sql-server-2017 https://docs.microsoft.com/zh-cn/sql/tools/sqlpackage?view=sql-server-2017
SQLPackage Post Deployment Script not running SQLPackage Post Deployment脚本未运行
Update (Steve Ford answer below may not work): 更新(下面的史蒂夫·福特答案可能不起作用):
Want to conduct this from command prompt without any External files. 希望在没有任何外部文件的情况下从命令提示符执行此操作。 Steve answer may not work for my situation/question.
史蒂夫的答案可能不适用于我的情况/问题。
The way to add a post deployment script requires you to add it to the project. 添加部署后脚本的方法要求您将其添加到项目中。
See Microsoft documentation here: MSDN Pre & post deployment scripts 请参阅此处的Microsoft文档: MSDN部署前和发布后脚本
To add and modify a pre- or post-deployment script use Solution Explorer, expand your database project to display the Scripts folder. 要使用“解决方案资源管理器”添加和修改部署前或部署后脚本,请展开数据库项目以显示“脚本”文件夹。
Right click on the Scripts folder and select Add. 右键单击“脚本”文件夹,然后选择“添加”。
Select Scripts in the context menu. 在上下文菜单中选择“脚本”。
Select Pre-Deployment Script or Post-Deployment Script. 选择部署前脚本或部署后脚本。 Optionally, specify a non-default name.
(可选)指定一个非默认名称。 Click Add to finish.
单击添加完成。
Double click the file in the Scripts folder. 双击“脚本”文件夹中的文件。
The Transact-SQL editor opens, displaying the contents of the file. Transact-SQL编辑器打开,显示文件内容。
You can use SQLCMD syntax and variables in your scripts and set these in the database project properties. 您可以在脚本中使用SQLCMD语法和变量,并在数据库项目属性中进行设置。 For example:
例如:
You can use SQLCMD syntax to include the content of a file in a pre- or post-deployment script. 您可以使用SQLCMD语法在部署前或部署后脚本中包含文件的内容。 Files are included and run in the order you define them: :r .\\myfile.sql
包含文件并按照您定义它们的顺序运行::r。\\ myfile.sql
You can use SQLCMD syntax to reference a variable in the post-deployment script. 您可以使用SQLCMD语法在部署后脚本中引用变量。 You set the SQLCMD variable in the project properties or in a publish profile:
您可以在项目属性或发布配置文件中设置SQLCMD变量:
:setvar TableName MyTable
insert into [$(TableName)] (SampleColumn) values ('1')
When you are asking for specific answer it is usually good idea to explain what exactly do you want to achieve and why. 当您要求特定的答案时,通常最好解释一下您到底想要实现什么以及为什么实现。 You might stick for specific solution to solve 1 problem that can be solved in many different ways.
您可能会坚持使用特定的解决方案来解决可以通过许多不同方式解决的1个问题。
What's the real problem you are trying to solve. 您要解决的真正问题是什么? If the problem is with the fact that post/pre scripts are always executed then you might want to write idempotent script or use some functionality that would execute that script just once.
如果问题在于后脚本/前脚本始终执行,那么您可能要编写幂等脚本或使用某些功能来执行该脚本一次。 You can check my answer here for that.
您可以在这里查看我的答案。
If you have other reasons of doing that then you can also achieve that by calling sqlcmd utility or any other one that can execute the statement from the file and run this utility straight away after the sqlpackage . 如果您还有其他原因,也可以通过调用sqlcmd实用程序或可以从文件执行该语句并在sqlpackage之后立即运行此实用程序的任何其他方法来实现 。
Now answering your initial question. 现在回答您的第一个问题。 This is what you need to do if you want to do that with just creating new variable, let's say ReplaceVariable and add single line to the post script:
如果您只想创建新变量就可以做到这一点,比如说ReplaceVariable,并在发布脚本中添加一行:
$(ReplaceVariable)
and then run 然后运行
sqlpackage.exe
/Action:Publish
/SourceFile:"Database1.dacpac"
/TargetDatabaseName:TestDb
/TargetServerName:localhost
/Variables:ReplaceVariable="insert into a values (1);"
and if nothing is supposed to be executed, just pass something like "--" as an argument. 如果不执行任何操作,则只需传递“-”之类的参数即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.