开发者

Free Schema Export for SQL Server 2008?

Not sure if this belongs here or on ServerFault, but I wonder if someone knows a free tool to export a SQL Server 2008 Schema? It's only for Tables and their Indexes, Foreign-Keys etc. and it needs to be a command line tool to run as part of a build process. If it can read a .net connection string, that would be awesome (hence the .net tag)

Data is not needed and any sort of versioning/diff is also "Nice, but not needed". And yes, I am aware of Red-Gate开发者_高级运维's awesome SQL Server tools, sadly this is a hobby project with 0 budget :-(


Not sure about a readymade tool, but it's easy enough to do with the SMO library (Microsoft.SqlServer.Smo, .SmoEnum, .SqlEnum):

using Microsoft.SqlServer.Management.Smo;

var server = new Server("localhost");
var database = server.Databases["databaseName"];
var transfer = new Transfer(database);
var options = new ScriptingOptions();
// set transfer and options object properties to reflect what you want to script:
// i.e. all tables, indexes, triggers, etc.
options.FileName = "c:\\temp\\databaseName_schema.sql";
transfer.Options = options;
transfer.ScriptTransfer();

I built a simple tool using this method to regenerate my product's database creation script as part of the pre-build steps in the setup builder.


Just out of curiosity have you tried using SQL Server Projects within Visual Studio?

One other way to do this is through SQL scripts as I'm sure you're aware of. The generate script command can be made to run in command line I think.


You can always use SQL Server Management Studio to do it, right click the database, select 'Tasks' and then 'Generate scripts'.


There are several free command-line tools like this at CodePlex. One of them is Scriptio, here.


Microsoft released a new tool a few weeks ago called mssql-scripter. It's a free Python-based, open source command line tool and you can find the official announcement here. Essentially, the scripter allows you to generate a T-SQL script for your database/database object as a .sql file. You can generate the file and then execute it. This might be a nice solution for you to generate the schema (the default option) your db and its objects such tables and indexes to also see the diff since it's a .sql file that is generated. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜