databasevisual-studio-2010deployment

Pre loading data in database projects


I have a visual studio database project in my solution for managing changes to database and source control.

I have a script for each table under Scripts/Post-Deployment folder in the project which I run in Script.PostDeployment.sql as follows:

:r .\Data.Script1.sql
:r .\Data.Script2.sql
....

These script populate required data like lookup tables using "Insert Into tablename..." statements.

The problems I am facing are:

  1. If in above example Script1 fails because the database already exists and I am redeploying because of minor change in structure of correcting a spell mistake in data etc., the rest of batch is aborted too.
  2. Changes in insert into statements are not propagated because when depolying to existing database first statement in Data.*.sql file will fail since that row already exists.

Is there a way in for visual studio database projects to compare data in existing database and use insert or update based on if a row already exists or not?


Solution

  • You should use MERGE INTO instead of INSERT INTO statements in you Data.*.sql files.