Programmatically generate script for all objects in a database

17,063

Solution 1

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)

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

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

Share:
17,063
Uwe Keim
Author by

Uwe Keim

German developer. Some of my apps: SharePoint Systemhaus Göppingen (zwischen Stuttgart und Ulm) Eigene Homepage erstellen Test Management Software Windows 10 Ereignisanzeige Very proud father of Felix (2012) and Ina (2014). Loves running, climbing and Indian food. Code Project member #234.

Updated on June 04, 2022

Comments

  • Uwe Keim
    Uwe Keim almost 2 years

    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?