views:

27

answers:

2

I'm using a Database Project in Visual Studio 2010 to manage all of my database code.

For my application, we separate the data for each client into their own copy of the database, with a common database to map users to the appropriate database.

I'd like to keep several different sample data sets in TFS along with the code and be able to selectively deploy one of them as necessary.

I've created scripts for two different client databases and have them both saved in the post-deployment directory in tfs. I'd like to maintain another that we use for demos and unit testing.

Is there a way to make :r conditional in SQLCMD? Are there other suggestions or best practices for how to do this? I've been reading through the Visual Studio 2010 Database Projects Guidance Document, but haven't found anything yet.

Thanks in advance for any help you can provide.

A: 

May be it is not directly you want, but for easy database creation & population from .Net review usage of wide migrate tool. My preference (Migrator.NET) but full review can be found there: http://flux88.com/blog/net-database-migration-tool-roundup/

Dewfy
+1  A: 

I was on the right track before adding a variable to the project and checking it in the script.

There are two little catches:

  1. In the .sqlcmdvars file the value of the variable value needs to include the single quotes around it
  2. This method produces a single script with all of the sample data included, but only the one specified by the variable gets executed. As the sample data gets more complete, this will become a pretty large file, but this should be ok for my purposes. I could see how this would be a deal-breaker for really large data sets. Also, the individual scripts can't have the GO separator in them since it's contained in a BEGIN END block.

Here's my file:

/* All deployments get the reference data */
:r .\ReferenceData.PostDeployment.sql

IF ('$(SampleSet)' = 'ClientA')
BEGIN
    :r .\SampleData.ClientA.PostDeployment.sql
END
ELSE IF ('$(SampleSet)' = 'ClientB')
BEGIN
    :r .\SampleData.ClientB.PostDeployment.sql
END

Credit to Jamie Thomson and this article for the quoted value bit.

Mike Forman