Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

Schedule Generation of All Object Creation Scripts for All Specified SQL Server Databases

0.00/5 (No votes)
22 Apr 2022CPOL 3.3K  
How to schedule generation of all object creation scripts
Create a batch file which uses sqlpubwiz to create script files which look like the file created in SQL Server Management Studio accessed via Tasks > Generate Scripts > Choose Objects (Entire database and all objects). You can choose specific SQL instances and databases.

Introduction

You can use the Microsoft provided utility sqlpubwiz to create a script file which looks like the file created in SQL Server Management Studio when you choose:

Tasks > Generate Scripts > Choose Objects (Entire database and all objects)

Batch File Code

The ScriptDB Batch File to script a SQL Server database

You can then create a calling batch file which calls the above ScriptDB multiple times for different databases/instances. For example...

Scripting Multiple Databases

This batch can then be scheduled using Windows Scheduler.

Because each script file is named based on today's date, you won't overwrite any earlier scripts if you schedule once per day. If you want to create a script file including the time of day (so the schedule can run more than once a day), include "t1" in the "set filea" line as needed.

History

  • 22nd April, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)