3

For an automated setup build that generates the setup for an application which uses Microsoft SQL Server, I am currently evaluating whether the following is possible:

I want to programmatically (CMD script or C# code) execute the function "Generate Scripts" on a database that is accessible from Microsoft SQL Server Management Studio 2008.

I.e. call some code and have all the objects (tables, SPs, constraints, etc.) of a specified database as an SQL script.

Currently, I only need the structure, not the data.

Question: Is it possible to achieve this goal?

5 Answers 5

4

I wrote SMOscript, a command line tool to script all objects into a single file, or one file for all objects.

The Generate Scripts function of MSSQL typically does not consider references and dependencies to determine the sequence of objects (at least that was the case with 2000 and 2005)

Sign up to request clarification or add additional context in comments.

6 Comments

Does your code work fine for SQL Server 2008? I see that it requires libraries from SQL Server 2005 installed in order to work. Otherwise, it is a wonderful tool.
I use version 0.19 regularly on 2008 (10.0.2531.0 SP1)
Current version does not work for a SQL Azure database. Not too surprising seeing as it doesn't work from Generate Scripts inside SSMS 2008 R2 either, both ending with the same error message about Index was outside the bounds of the array. (see my question here: stackoverflow.com/questions/9622688/…)
In the newer SQL Server Management Studio versions, the "Generate Scripts" wizard also has the ability to generate SQL code for both Schema and Data. Is this also possible with SMOscript?
Link in answer is broken.
|
3

I used http://www.codeplex.com/ScriptDB with great success.

Comments

2

You should be able to do it in managed code using SMO

Comments

2

Microsoft has also released a tool called 'mssql-scripter'. It can be found here: mssql-scripter

2 Comments

That looks awesome! Thanks a lot 😊.
This library is no longer maintained... Thanks Microsoft!....
1

You could invoke a standard select clause (using ADO.NET for example) over the sysobjects view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.