2

有没有办法访问Visual Studio 环境变量,例如 $(SolutionDir)?基本上,我有一套播种脚本,但我尽量避免引用硬编码路径,如 C:\projects**\seeding.sql,相反,我想使用 $(SolutionDir)Seeding.sql

4

1 回答 1

2

Here is the general process for using project variables in SQL in your database project from MSDN:

How to: Define Variables for Database Projects

This depends on what version of Visual Studio you're using (and whether you're using a database project -- highly recommended), but the general idea is you can assign some of your project variables as SQLCMD variables, build a SQLCMD script using those variables and when you run a build, the system will use SQLCMD to invoke your script, applying the variables as necessary to run your script.

Even more to the point, the database project's recommended practice for "seeding" scripts and other post-build artifacts is to designate one SQL script as the post-build script and have it use SQLCMD syntax to invoke other .sql script files.

So in my project (for example) I have this SQL script in a Scripts folder:

:r .\PopulateNumbersTable.sql
:r .\Insert.Dimension.Date.sql
:r .\FillInMetaData.sql
:r .\Permissions.sql

Which then calls each of those scripts (also in the folder using relative paths) which are run in SQLCMD mode. So in PopulateNumbersTable.sql for example, I set the max value of the numbers table as a SQLCMD variable so when the script runs in the post-build it uses that value.

Anyway, check out SQLCMD and VS Database Projects, it basically covers what you're looking for, it's plenty flexible and (for me) pretty easy to understand.

于 2013-07-25T14:44:56.243 回答