Automatically Create Install Scripts with Sql Server

Problem: How can I QUICKLY create an install script automatically in sql server every time I change my database? Creating an install script in sql server is pretty easy thanks to the "Script Database As..." command. However, this is generally a long winded process that doesn't give you too much control.

In this post, I propose a solution to create build scripts with the following requirements

  1. Don't need to write any code
  2. Easily maintainable
  3. Can script schema AND data
  4. Can specify which objects to script
  5. Can run automatically before an MSI setup/deployment project is built
  6. It has to be FREE

The first thing you need to do is download Microsoft's Database Publishing Wizard (http://www.microsoft.com/downloads/details.aspx?FamilyId=56E5B1C5-BF17-42E0-A410-371A838E570A&displaylang=en)

Next, in your development sql server, add two new accounts named: ScriptSchema and ScriptData. Make sure they are created as a sql login. At this point, don't give them any other access than public access to the database you want to script.

Open the database you want to script. For each table/view/stored proc etc that you want to include in the install, grant view definition access to the ScriptSchema user as follows:

grant view definition on <schema>.<object name> to scriptschema

Next, for each table that you want to script the data for, grant select permission on that table to the ScriptData user, ie:

grant select on <schema>.<table name> to scriptdata

You can double up, so for instance if you want to script the creation and population of data of a certain table called [dbo].[customers] you'd run the two commands:

grant view definition on [dbo].[customers] to scriptschema

grant select on [dbo].[customers] to scriptdata

Once you've set up the permissions, you can check out what will be scripted by connecting to the database as the scriptschema and scriptdata users. As you've now defined what will be scripted, it's time to do the scripting itself.

Jump out into a cmd.exe, and run the two commands;

sqlpubwiz script -C "data source=[server]; initial catalog=[database]; uid=scriptschema; pwd=password;" schema.sql -schemaonly -f

sqlpubwiz script -C "data source=[server]; initial catalog=[database]; uid=scriptdata; pwd=password;" data.sql -dataonly -f

The first command connects to the database using the scriptschema account, and generates the definition of all the objects it can see, ie: those which you gave grant view definition access to.

Likewise the second command connects to teh database using the scriptdata account and generates the scripts to insert the data, based on all the tables it can select from.

You now have two scripts: schema.sql, data.sql. You can add these to your msi/setup projects in visual studio (outside the scope of this post). Furthermore you can then add the two commands mentioned to the pre-build events of your setup's resource project to build the install scripts prior to the MSI etc being built.

I really like this solution to automatically generate sql install scripts. I feel it's elegant, there's no code to be maintained, and no manual updating of sql scripts every time you change the database.

posted @ Friday, April 18, 2008 12:11 PM

Print

Comments on this entry:

# re: Automatically Create Install Scripts with Sql Server

Left by Jesse at 4/20/2008 3:01 AM
Gravatar
also might want to check this out for generating scripts:

http://www.codeplex.com/scriptdb

# re: Automatically Create Install Scripts with Sql Server

Left by TechUser at 6/4/2008 9:17 AM
Gravatar
Neither solution (wizard or scriptdb) works in Sql Server 2008. (I assume there will be an update.)

# re: Automatically Create Install Scripts with Sql Server

Left by TechUser at 6/4/2008 9:38 AM
Gravatar
actually the wizard is there, already bundled in to 2008; however, scriptdb doesn't work.

# re: Automatically Create Install Scripts with Sql Server

Left by Andrew at 6/5/2008 1:26 AM
Gravatar
Hi TechUser,

I admit I was only using 2005 when I was writing this post. I'm not suprised the wizard was added to 2008; finer levels of db scripting control are something that should have been there from way back in 2000 IMHO.

# re: Automatically Create Install Scripts with Sql Server

Left by jay at 7/23/2008 5:49 PM
Gravatar
hey how do i install scrips on my sql database usin godaddy

# re: Automatically Create Install Scripts with Sql Server

Left by Andrew at 7/23/2008 6:26 PM
Gravatar
after you generate the scripts, log into your hosting centre. Under "Databases" go to "SQL Server" & log in.

Under "Tools" click "Query Analyzer" and paste in the contents of your script files then hit "Execute"

Your comment:



 (will not be displayed)


 
 
 
Please add 5 and 7 and type the answer here:
 

Live Comment Preview:

 
«September»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011