How To: Automate "Generate Script" in SQL Server
If you are here below are the things that you are probably trying to achieve:
Trying to automate the "Generate Script" functionality in SQL Server to take a backup of all databases and the creation scripts - tables, functions, stored procedures, views etc
Trying to take a backup of your schema every night
Trying to do the above and put it in Source Control for versioning
Versioning of SQL Server databases schema
What you need before you start?
SQL Server (Express) Management Studio on the machine you are trying to create and backup the script on: http://tinyurl.com/423fqvc . Once installed please make sure you open it and can connect to your Database Server.
SQL Server Database Publishing Wizard on the machine you are trying to create and backup the script on
Check if this is under:
C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Publishing\1.2\SqlPubWiz.exe
or
C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\1.2\SqlPubWiz.exe
if not download from
SQL Server 2005:
http://www.microsoft.com/download/en/details.aspx?id=5498
SQL Server 2008:
http://stackoverflow.com/questions/1391013/sql-database-publishing-wizard-with-sql-server-2008
Tortoise SVN on the machine you are trying to do the automatic commit of the database scripts on
Download from http://tortoisesvn.net/downloads.html
SVN on the machine you are trying to do the automatic commit of the database scripts on
Download from http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=11151&expandFolder=11151&folderID=91
Visual Studio Express C# I just find programming using C# easier to create batch programs - you can use anything you like (Java etc.)
Download from http://www.microsoft.com/express
Creating scripts for all databases on the server
1. Using your favorite coding language create a windows application (exe, bat) which does the below
Connects to your database server
Lists all the databases on server
Select name From sys.sysdatabases
Loops through the database names
Calls SqlPubWiz.exe for each database name (See Point 2 under "What you need before you start")
And passes the below parameters to it:
script -f -d DatabaseName -S DatabaseServer -U UserName -P Password C:\BACKUPFOLDER\DatabaseName.sql -schemaonly
Example Code in C#
Subversioning the scripts
2. Run the application created in Step 1, to create first version schemas/scripts of all your databases in your backup folder.
3. Add the first version of your databases schemas to your source control.
4. Right click the BACKUPFOLDER. Click "Tortoise SVN" then "Import". Connect your SVN server, create a folder for your schema backups and press OK.
5. Right click your BACKUPFOLDER again click "SVN Checkout" -- make sure your current directory is right. Once you click OK all your files will be "Versioned".
Automating Add , Delete And Commit
6. Create a bat file and paste the below code in it (call it autocommit.bat)
svn cleanup %1
for /f "tokens=2*" %%i in ('svn status %1 ^| find "?"') do svn add "%%i"
for /f "tokens=2*" %%i in ('svn status %1 ^| find "!"') do svn delete "%%i"
svn commit -m "Automatic commit" %1
7. Pass the folder name to this bat file
eg.
autocommit.bat C:BackupFolder
Schedule Backup
8. Goto Windows Task Scheduler and get it to run your first application that generates scripts for all databases first.
9. And then run the autocommit.bat batch file