简体   繁体   English

SQL Server - 如何自动“生成和发布脚本”

[英]SQL Server - how to “Generate and Publish Scripts” automatically

I want to automatically (ideally from the command prompt in a batch file) automate the generation of the schema of my SQL Server 2008 R2 database. 我想自动(理想情况下从批处理文件中的命令提示符)自动生成我的SQL Server 2008 R2数据库的模式。

In SSMS, I can right-click the DB, choose "Tasks", "Generate scripts", and then follow the wizard to gen a Schema script. 在SSMS中,我可以右键单击数据库,选择“任务”,“生成脚本”,然后按照向导生成模式脚本。 Is there a command-line version of this process that I can use? 我可以使用这个过程的命令行版本吗?

Microsoft released a new tool a few weeks ago called mssql-scripter that's the command line version of the "Generate Scripts" wizard in SSMS. 几周前微软发布了一个名为mssql-scripter的新工具,它是SSMS中“Generate Scripts”向导的命令行版本。 It's a Python-based, open source command line tool and you can find the official announcement here . 这是一个基于Python的开源命令行工具,你可以在这里找到官方公告。 Essentially, the scripter allows you to generate a T-SQL script for your database/database object as a .sql file. 本质上,脚本编写器允许您为数据库/数据库对象生成一个.sql文件作为.sql文件。 You can generate the file and then execute it. 您可以生成该文件然后执行它。 This might be a nice solution for you to generate the schema of your db (schema is the default option). 这可能是一个很好的解决方案,您可以生成数据库的模式(模式是默认选项)。 Here's a quick usage example to get you started: 这是一个快速使用示例,可​​帮助您入门:

$ pip install mssql-scripter
# script the database schema and data piped to a file.
$ mssql-scripter -S localhost -d AdventureWorks -U sa  > ./adventureworks.sql

More usage examples are on our GitHub page here: https://github.com/Microsoft/sql-xplat-cli/blob/dev/doc/usage_guide.md 更多用法示例在我们的GitHub页面上: https//github.com/Microsoft/sql-xplat-cli/blob/dev/doc/usage_guide.md

From this answer , there appear to be tools called SMOScript and ScriptDB that can do that. 这个答案来看,似乎有一些名为SMOScriptScriptDB的工具可以做到这一点。

If you find a way without third party tools please share :) 如果您找到没有第三方工具的方法请分享:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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