开发者

What is the proper way to insert known values into a VS 2010 Database project post-deployment?

开发者 https://www.devze.com 2023-03-28 21:13 出处:网络
I\'ve tried a few different google searches but can\'t find any best practices or tutorials that address this.

I've tried a few different google searches but can't find any best practices or tutorials that address this.

This is the first time I've used a VS database project. I've imported an existing database (everything looks fine) and now I want to populate some of the tables post-deployment.

There is a Script.PostDeployment.sql file that includes the following header:

/*
Post-Deployment Script Template                         
--------------------------------------------------------------------------------------
 This file contains SQL statements that will be appended to the build script.       
 Use SQLCMD syntax to include a file in the post-deployment script.         
 Example:      :r .\myfile.sql                              
 Use SQLCMD syntax to reference a variable in the post-deployment script.       
  Example:      :setvar TableName MyTable                           
           SELECT * FROM [$(TableName)]                 
 --------------------------------------------------------------------------------------
*/

I'm wondering if from the last three lines there is some expected way to write these scripts using variables instead of just pure T-SQL syntax?

Should I be writing

INSERT INTO [dbo].[BlackAdder] VALUES ('edmund')
INSERT INTO [dbo].[BlackAdder] VALUES ('baldrick')

or

setva开发者_如何学Gor [dbo].[BlackAdder] BlackAdder
INSERT INTO [$(BlackAdder)] VALUES ('edmund')
INSERT INTO [$(BlackAdder)] VALUES ('baldrick')

Does the latter allow some sort of compile-time check so that if setvar cannot resolve [dbo].[BlackAdder] that the project will give me some error?


If you open the project Properties folder, and click on Database.sqlcmvars you will see 3 vars already defined $(DefaultDataPath), $(DtabaseName) and $(DefaultLogPath) and can define your own here.

The setvar would be the other way around:

:setvar BlackAdder [dbo].[BlackAdder] 

but ideally you define these in Database.sqlcmvars.

The intended use is for deploying your database project into multiple environments by defining SQLCMD variables and including them in your pre-deployment and post-deployment scripts.

  • How to: Define Variables for Database Projects

  • Property Files in Database and Server Projects

0

精彩评论

暂无评论...
验证码 换一张
取 消