[tip] Use the Database Publishing Wizard to script your table data - Jon Galloway

[tip] Use the Database Publishing Wizard to script your table data

Jeff Atwood recently asked "Is your database under version control?"  Well, is it? It's not as easy as it sounds. Until now, there wasn't a good, scriptable solution to the problem. I'll run through a few of the options I've looked at in the past, but let's jump to to the punchline:

The Database Publishing Wizard can script data as schema, and it's got a command line mode.

The Problem

Most of the applications I work on run against SQL Server databases. Jeff hit the nail on the head when he said that your database is an integral part of your application. I'll take it a step further and say that your data is often an integral part of your application. Many databases include signifcant amounts of reference data which are as much a part of your software release as your config files - for instance, a DotNetNuke site without the correct version of the database is just plain shot.

I'm not talking about database backups, which are an important part of your production environment. Database backups are a more appropriate solution to safeguarding your transactional data, but they don't integrate at all with the rest of your application's version control.

I previously posted a script to check a database (MDF) file into Subversion. It works, but it's far from ideal. It's just a binary file, so while you've got history, you can't track differences. No good.

I've used SQL-Scripter, GenDbScripts (had to fix a few bugs), and Enisey Data Scripter. They all work, but none of them was scriptable. 

I set out to see how easy it would be to write something in PowerSMO that would script out the table schema and data. It should have been easy, since SMO has a special "execution mode" that records all commands and outputs the resulting script. Heck, David Hayden has one post showing C# sample code to script a database schema, and another using SMO's Transfer.TransferData() to copy all data from one database to another. No problem, I'll just mash them together to enable CaptureSql and TransferData, and I'm set...

Yeah, not really. I converted it to a PowerShell script. It worked great... but it only scripted the schema, not the data. It looks like TransferData doesn't go through script, so it's not picked up by that cool CaptureSql thing. (In case you care, that PowerShell script is in the footnotes.)

The Solution (well, pretty much...)

Then Scott Guthrie mentioned the new Database Publishing Wizard on an extensive post on deploying a SQL database to a remote hosting environment. Scott's post was pretty beefy and covered the SQL Server Hosting Toolkit, so you might have missed the beautiful screenshots below the fold. The Database Publishing Wizard makes it easy to script your schema and data from within Visual Studio: 

 

The more exciting thing to me is that the Database Publishing Wizard can be run from the commandline:

sqlpubwiz script ( -C connection_string | -d local_database_name ) (output_file) [switches]

switches:

 Output options:

  [-f] :                       Overwrite existing files
  [-noschemaqualify] :         Output script does not qualify object names with
                             schema name.
  [-schemaonly] :              Only script schema
  [-dataonly] :                Only script data
  [-targetserver] :            Specifies what version of SQL Server the script
                             should target.  Valid versions are "2000", "2005".
                             Default Value: 2005
  [-q] :                       Suppress output
  [-nodropexisting] :           Default Value: False

 Local connection related:

  [-C connection_string] :     Connection string
  [-d local_database_name] :   Database name to script
  [-U local_user_name] :       Username
  [-P local_password] :        Password
  [-S local_server_name] :     Server name

Perfect!!!

Oops, we're missing two things before my quest is ended:

  1. We need an option to specify which tables to script. I know that complicates things a bit, but we gotta have it. Maybe a parameter that points to a text file containing a list of tables to be scripted. That's important because we want to script and version our reference data, but we don't want to script our transactional data for a lot of reasons (most importantly we don't want to overwrite transactional data, but also the transactional data will usually be the most subject to large growth).
  2. Why do the drop scripts need to check sys.objects for SQL 2005 databases and sysobjects for SQL 2000 databases? Why not use the INFORMATION_SCHEMA views so the same script works on both? That's the subject of another post, I guess...

Oh, and that PowerSMO I mentioned. It'll script the schema, but not the data:

$databaseToScript = "AdventureWorks" $serverName = "(local)\SQL2005" #USES POWERSMO - http://www.pluralsight.com/dan/samples/PowerSMO.zip #SETUP VOODOO $executionPolicy = get-ExecutionPolicy set-ExecutionPolicy "Unrestricted" .\InitPowerSMO.ps1 cls #CONNECT TO DATABASE $server = SMO_Server $serverName $server.ConnectionContext.SqlExecutionModes = [Microsoft.SqlServer.Management.Common.SqlExecutionModes]"CaptureSql" #ExecuteAndCaptureSql $database = $server.Databases[$databaseToScript] $t = SMO_Transfer($database) $t.CopyAllObjects = $true $t.DropDestinationObjectsFirst = $true $t.CopySchema = $true $t.CopyData = $true #Doesn't get scripted $t.DestinationServer = $serverName $t.DestinationDatabase = "just_a_dummy_value" $t.Options.IncludeIfNotExists = $true $t.CreateTargetDatabase = $true #Doesn't help #$t.TransferData() "Generating script" $commands = $t.ScriptTransfer() "Writing to file" $scriptFile = "{0}_Script.sql" -f $databaseToScript $commands | out-File $scriptFile "Done" #CLEANUP $server = '' set-ExecutionPolicy -executionPolicy $executionPolicy
Published Friday, December 29, 2006 11:53 PM by Jon Galloway

Comments

# re: [tip] Use the Database Publishing Wizard to script your table data

Saturday, December 30, 2006 5:06 PM by Jon Galloway

# re: [tip] Use the Database Publishing Wizard to script your table data

The EasySQL Deploy is a similar data scripter. It uses an  innovative way to pass the list with tables to command line interface. First you generate a project with all the options you need. Then you pass the project name as command line option.

Thursday, February 22, 2007 9:15 AM by Chris Page

# re: [tip] Use the Database Publishing Wizard to script your table data

Bruce Chapman has a script which scripts out data insert statements on a specified db object for running against another table of same structure.

Available from this link:

www.ifinity.com.au/.../Default.aspx

I have downloaded it and find it very useful. What will really make the DB publishing wizard successful will be the option to select objects to script.

Thursday, May 31, 2007 4:41 PM by Mike Teye

# re: [tip] Use the Database Publishing Wizard to script your table data

Hi John,

Thanks for the post. This may be just what I needed.

I have successfully used this tool as well, but it is limited:

www.codeproject.com/.../ScriptDB4Svn.asp

Wednesday, January 23, 2008 2:56 PM by Tod Birdsall

# re: [tip] Use the Database Publishing Wizard to script your table data

Can anyone suggest me how to script the remaining objects of the database excluding hte encrypted one's....as this wizard is not having the capacity to script encrypted objects

Thursday, January 24, 2008 8:21 AM by Vinay

# How-To: Auto-Generate SQL 2000 Database Creation and Alteration Scripts

I recently changed jobs and I am working with the rest of the software development team to streamline

Friday, January 25, 2008 10:01 AM by Tod1d's Thought Process

# re: [tip] Use the Database Publishing Wizard to script your table data

Please anyone advice ...

The scriptor is running fine but it is not scripting the database even if a single object is encrypted. It should actually exclude that encrypted object instead of excluding whole objects in that database.

Can any one advice me how to exclude the object which in encrypted..instead of excluding the entire database.....

Monday, February 18, 2008 11:54 PM by Vinay

# database publishing wizard

Pingback from  database publishing wizard

Sunday, June 01, 2008 7:08 AM by database publishing wizard

# database publishing wizard

Pingback from  database publishing wizard

Sunday, June 01, 2008 10:32 AM by database publishing wizard

# database publishing wizard

Pingback from  database publishing wizard

Sunday, June 01, 2008 11:08 AM by database publishing wizard

# re: [tip] Use the Database Publishing Wizard to script your table data

Please anyone suugest...

The scriptor is running fine in GUI environment but it is not scripting the database when i am writing a batch file for scripting database from a remote server. I have tried this several times but it always gives an error that "wrong/missing arguments".

Below is the code of the batch file which i am trying to run.

"C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\SqlPubWiz.exe" script -d remoteDatabase -S RemoteServerName -U UserName -P Password E:\BackupDBScript.sql

Can any one tell me what am i doing wrong?? Or i have to try some other method to schedule the backup of my database from remote server on my local system in any format (sql, excel, text, mdf)

I am not using GUI environment as i want the backup to be scheduled and run automatically for which i have to use the batch file.

Please suggest me, its really an urgent issue!

Thursday, February 12, 2009 4:02 AM by Talha Ashfaque Khan

# re: [tip] Use the Database Publishing Wizard to script your table data

how do i execute the script after publishing..its is bringing out errors.like it doesnt contain a create database statement

Sunday, March 29, 2009 4:18 AM by uthman

# The Option Wiz | 7Wins.eu

Pingback from  The Option Wiz | 7Wins.eu

Wednesday, August 18, 2010 12:39 PM by The Option Wiz | 7Wins.eu

# re: [tip] Use the Database Publishing Wizard to script your table data

I'm having a problem with the SqlPubWiz scripting the create views before the create tables which those views reference. Obviously this doesn't work since those tables are not found until later. I've seen other people have problems with this but have yet to find a solution other than manually rearranging the output file.

Does anybody have a solution to this problem? Does anybody know WHY on earth it would create the views before the tables?  I can see NO reason to create the views first and every reason to create them last.

Friday, September 23, 2011 12:36 PM by jim

# re: [tip] Use the Database Publishing Wizard to script your table data

Here would be the possibilities: Great movie calling by having an eight megapixel camera and great viewing through the Retina Display while discussing great moments together with your friend basically sitting on the couch. The apple ipad really looks attractive as one's sole camera.This can help you decide which usually among ipad or mini netbook, would function as the appropriate choice for you personally. To accomplish this with ipad tablet, you should do it through iTunes that is a bit clunky as well as nowhere near as sleek since the Android system.

Knowledge and also experience enables iPad Developer to create unique along with rich ipad Apps for clients as well as company. Innovation received embodied with this gadget and it has opened an excellent path regarding iPad application development.What about university students? The show, though, is excellent and the Touch will allow on-screen multitasking as well as<a href=www.purevolume.com/.../Wonderful+Quality+Low+Cost+Tents>check it out wiki </a> group the related tasks together.

In addition there are scenarios where you obtain ipad repairs or substitute to package these brand new features. You could convert Youtube video to apple ipad or convert  Veoh movie to apple ipad on Macintosh OS X, and the output movie looks very nice in your iPad's silver screen. Since, iPad is invented from the manufacturer regarding iPhone, so a lot of the gadgets + functions would be the same, but tiny bit elaborated with additional power permitting the consumers work more efficiently. There are numerous disadvantages if you will not unlock apple ipad.

Saturday, September 08, 2012 1:09 PM by Dexqak39

# Command(s) completed successfully but&#8230; the tables are not created | DIGG LINK

Pingback from  Command(s) completed successfully but&#8230; the tables are not created | DIGG LINK

# re: [tip] Use the Database Publishing Wizard to script your table data

Hi,Please open the file /includes/settings.aspIf you want to change the dlfauet settings.asp to point to mysql you have to follow these steps:1) comment the line pointing to the mdb file:changepDatabaseConnectionString =  Driver={Microsoft Access Driver (*.mdb)};DBQ=   server.MapPath( ../database/comersus.mdb )  ;  to' pDatabaseConnectionString =  Driver={Microsoft Access Driver (*.mdb)};DBQ=   server.MapPath( ../database/comersus.mdb )  ;  2) uncomment Mysql connection string:change' mySQL  Server 3.51 local' pDatabaseConnectionString =  Driver={MySQL ODBC 3.51 Driver};Server=localhost;database=comersus;user=root;password=;OPTION=3                         to' mySQL  Server 3.51 local pDatabaseConnectionString =  Driver={MySQL ODBC 3.51 Driver};Server=localhost;database=comersus;user=root;password=;OPTION=3                         3) Configure mysql server connection string paramaters like server, database, user and password.Make sure that the user configured has write permissions in your database.[]

Saturday, March 09, 2013 12:18 AM by Neil

# re: [tip] Use the Database Publishing Wizard to script your table data

Session is one thing and dasabate is other thing. You can mix Comersus dasabate with your own MDB as long as it does not contain the same table names. If you want to let customers log in in your app and maintain session in Comersus, make sure that the same session variable is used and that both authenticate by email.[]

Monday, March 11, 2013 7:07 AM by Chilliz

# re: [tip] Use the Database Publishing Wizard to script your table data

hello wonderful Blog

Friday, May 10, 2013 7:47 PM by oognurro@gmail.com

Leave a Comment

(required) 
(required) 
(optional)
(required)