views:

68

answers:

4

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 :-(

+2  A: 

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.

Paulo Santos
I thought there were only in VS Team System Database Edition, but I also found a "Database Project" in Visual Studio 2008 Professional. I'll look at that, thanks for the heads up!
Michael Stum
There is no "Generate Script" command in T-SQL, nor, AFAIK in SQLCMD. It's only a menu/button task in Management Studio (which calls SMO to do it). It can, however, be scripted from Powershell, (SQL 2008) which may be what you were thinking of.
RBarryYoung
+3  A: 

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.

Ben M
FYI: having written several of these myself, I can tell that you are glossing over the hard part, which is setting the options correctly. This is especially so since many of the options are specific to the particular SQL-type (tables, triggers, etc.) and will frequently throw exceptions if they are left on for types that they do not apply to.
RBarryYoung
Not sure what you mean, RBY. The options I refer to are all simple booleans: `CopyAllDatabaseTriggers`, `CopyAllTables`, `IncludeDatabaseContext`, `WithDependencies`, etc. There are a number of them, true, but it's a simple matter of setting most to `true`, the usual exceptions being security objects like logins and users, which depending on the source database may contain entries unwanted in the final script.
Ben M
+1  A: 

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

RBarryYoung
A: 

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

Jonas Lincoln