Udostępnij za pośrednictwem


How to: Define Variables for Database Projects

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

If you define a SQLCMD 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 SQLCMD variable in terms of another SQLCMD variable. For more information about MSBuild variables, see MSBuild Properties.

Note

You can use SQLCMD variables and MSBuild variables in the same script. However, you should not give a SQLCMD 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 Database.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 defines three variables for you that you can use in your object definitions and scripts. The values of these variables are 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. The third variable is $(DefaultLogPath), and it contains the path in which the log file for the database is stored on the target server.

To display SQLCMD variables for a database project

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

    A list of SQLCMD variables and values appears.

To add a SQLCMD variable to a .sqlcmdvars file

  1. Display the list to which you want to add a SQLCMD 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 SQLCMD variable in a .sqlcmdvars file

  1. Display the list that contains the SQLCMD 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 SQLCMD variable from a .sqlcmdvars file

  1. Display the list that contains the SQLCMD 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 Delete.

  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