Udostępnij za pośrednictwem


How to: Define Variables for Database Projects

You can deploy your database project into multiple environments by defining SETVAR variables and including them in your pre-deployment and post-deployment scripts. For example, you can define SETVAR variables for a service broker or a service certificate.

If you define a SETVAR variable in terms of an MSBuild variable, you can specify a value for it that is specific to a build configuration. As an alternative, you can override the MSBuild variable at a command prompt when you build the database project. You cannot define a SETVAR variable in terms of another SETVAR variable. For more information about MSBuild variables, see MSBuild Properties.

Note

You can use SETVAR variables and MSBuild variables in the same script. However, you should not give a SETVAR variable the same name as an MSBuild variable because the MSBuild variable will take precedence.

The list of variables and their values is stored in the Properties folder of the database project. When you create a project, you also automatically create the file SqlCommandVariables.sqlcmdvars. You can define multiple files and associate them with different build configurations if you want to provide different values for each configuration. For more information, see How to: Configure Deployment Settings for Database and Server Projects.

Note

Visual Studio Team System Database Edition defines two variables for you that you can use in your object definitions and scripts. The values of these two variables is set when you deploy the project. The first variable is $(DatabaseName), it and contains the name of the target database to which you are deploying. The second variable is $(DefaultDataPath), and it contains the path in which the files for the database are stored on the target server.

To display SETVAR variables for a database project

  • In Solution Explorer, expand the database project for which you want to display SETVAR variables, expand the Properties folder, and then double-click the .sqlcmdvars file that you want to modify.

    A list of SETVAR variables and values appears.

To add a SETVAR variable to a .sqlcmdvars file

  1. Display the list to which you want to add a SETVAR variable.

    For more information, see the first procedure in this topic.

  2. In the Variable Name column, click in the last row, and type a name for the variable that you want to add.

  3. In the Variable Value column, click in the row in which you just added the variable name, and type a value for the variable.

  4. On the File menu, click Save All.

To modify a SETVAR variable in a .sqlcmdvars file

  1. Display the list that contains the SETVAR variable that you want to modify.

    For more information, see the first procedure in this topic.

  2. In the Variable Value column, click the row of the variable whose value you want to change, and type a new value.

  3. On the File menu, click Save All.

To delete a SETVAR variable from a .sqlcmdvars file

  1. Display the list that contains the SETVAR variables that you want to delete.

    For more information, see the first procedure in this topic.

  2. In the Variable Name column, right-click in the row of the variable that you want to delete, and click Remove Variable.

  3. On the File menu, click Save All.

See Also

Tasks

How to: Add Files and Filegroups

Concepts

An Overview of Database Build and Deployment

Overview of Files and Filegroups

Terminology Overview of Database Edition