How can I automate the "generate scripts" task in SQL Server Management Studio 2008?

Viewed 96042

I'd like to automate the script generation in SQL Server Management Studio 2008.

Right now what I do is :

  • Right click on my database, Tasks, "Generate Scripts..."
  • manually select all the export options I need, and hit select all on the "select object" tab
  • Select the export folder
  • Eventually hit the "Finish" button

Is there a way to automate this task?

Edit : I want to generate creation scripts, not change scripts.

14 Answers

If you're a developer, definitely go with SMO. Here's a link to the Scripter class, which is your starting point:

Scripter Class

In Tools > Options > Designers > Table and Database Designers there's an option for 'Auto generate change scripts' that will generate one for every change you make at the time you save it.

You can do it with T-SQL code using the INFORMATION_SCHEMA tables.

There are also third-party tools - I like Apex SQL Script for precisely the use you are talking about. I run it completely from the command-line.

Try new SQL Server command line tools to generate T-SQL scripts and monitor Dynamic Management Views.

Worked for me like charm. It is a new python based tool from Microsoft that runs from command line. Everything works like described on the Microsoft page (see link below) Worked for me with SQL 2012 server.

You install it with pip:

$pip install mssql-scripter

Command parameter overview as usual with h for help:

mssql-scripter -h

Hint: If you log in to SQL-Server via Windows authentication, just leave away Username and password.

https://cloudblogs.microsoft.com/sqlserver/2017/05/17/try-new-sql-server-command-line-tools-to-generate-t-sql-scripts-and-monitor-dynamic-management-views/

From Visual Studio 2008 SP1 TeamSuite :

In the Server Explorer / Data Connections tab, there's a publish to provider tool which does the same as "Microsoft SQL Server Database Publishing Wizard", but which is compatible with MS Sql Server 2008.

Related