English 中文(简体)
Visual Studio 2010 SQL Server 2008 Database Project - Custom Updates
原标题:

In Visual Studio, say you ve got a SQL 2008 Database Project and you add a non-nullable column to a table. When you go to deploy the database now, if that table has data in it it should fail. (Right?) How would you write custom logic so that during the update you can set this column to "x" or use a more advanced query or cursor to update the entire table and fill in the new column? This is something you would only want to happen once - at the same time the column was added to the database. Is there any support for this?

最佳回答

Here s what I ended up doing.

  1. Add version info to the database (e.g. in a SystemSettings table).
  2. Create a script for the next version (e.g. 1.1.0.0.sql).
  3. In the ScriptsPost-DeploymentScript.PostDeployment.sql file, add the following:

DECLARE @versionMajor INT;
DECLARE @versionMinor INT;
DECLARE @versionBuild INT;
DECLARE @versionRevision INT;

SELECT TOP 1
    @versionMajor = VersionMajor,
    @versionMinor = VersionMinor,
    @versionBuild = VersionBuild,
    @versionRevision = VersionRevision
FROM SystemSettings;

IF (@versionMajor <= 1 AND @versionMinor < 1)
BEGIN
:r "..Upgrade1.1.0.0.sql"
END

This will run the 1.1.0.0 script if the database version is lower. The 1.1.0.0 script updates the database version numbers as its final step. Future updates simply require you to add another IF block.

问题回答

I would add a nullable column, run an UPDATE command to populate it, and alter the column to NOT NULL. Although I do use Visual Studio 2010, I do not use it to deploy. Instead, I deploy with SQL Compare - it generates a SQL script, which is easy to tweak.

Here is how I do it.

In the Schema Viewer open the table creation script and add the new columns like so:

[first_name]        varchar( 32 )   NOT NULL default    ,
[last_name]         varchar( 32 )   NOT NULL default    ,

Because they have a *default value * they can deploy successfully.

Then in my post install script I check for values in the table and run an update to populate them if so. Alternately the code could fix it.

update mytable
set newcolumn =  x 
where newcolumn is null;

I have done a lot of research on this type of topic, and sadly it seems to be an area where VSDB projects are lacking. A good place to start looking is the Visual Studio ALM Rangers guide to VSDB projects; they have a hands-on-lab detailing their recommended best-practice for doing exactly what you ask about (it involves using pre and post-deployment scripts to copy data to and from transient tables). Additionally, Barclay Hill s blog has an article pertaining to your question (he s the Sr. Program manager for Data Tools), which is the same as the guide.

This recommended method seems to me as having a very high maintenance cost if you have to be able to update multiple different versions of a target database, but if you only have one database (or one version of the schema) to update, it is pretty workable.





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签