Share via


How to: Add References to Database Projects

You can add several types of references to database projects to handle different situations. For example, you can refer to other projects in the same solution, or you can refer to the output of other solutions. You follow slightly different procedures to add different types of references. For example, to use a cross-database reference, you must not only add the reference but also define a variable and associated value for the database, the server, or both. For more information about the types of references and when you might use each of them, see Using References in Database Projects.

To refer to objects that are defined in a database project in the same solution

  1. Open the solution that contains the database project to which you want to add a reference.

  2. On the Project menu, click Add Database Reference.

    The Add Database Reference dialog box appears.

  3. To add a reference to a database project, perform the following steps:

    1. Click Database projects in the current solution.

    2. In the list, click the database project to which you want to add a reference.

  4. To define a SETVAR variable for a server, perform the following steps:

    1. Click Define server variable.

    2. In Name, type a name for the variable that represents the server.

    3. In Value, type the name of the server that the variable represents.

    Note

    You do not need to define a server variable if you are referring to an object in a database that resides on the same server as the database to which you are adding the reference.

  5. To specify a SETVAR variable for the database to which you want to refer, follow these steps:

    1. Select the Define database variable check box.

    2. Clear the Literal check box if it was selected.

    3. In Name, type a name for the variable that represents the database.

    4. In Value, type the name of the database that the variable represents.

    Important

    If your database project contains objects that explicitly refer to objects in another database, you should update those references to use the variables that you just defined. If you imported objects from a database that contains cross-database references, you must update the references to resolve the warnings that those explicit cross-database references cause.

  6. To specify a literal value for the database to which you want to refer, follow these steps:

    1. Select the Define database variable check box.

    2. Select the Literal check box.

    3. In Value, type the name of the database to which you want to refer.

    Note

    You might specify a literal instead of a variable and value pair if the database name is always the same in your target deployment environment. For example, you could use a literal with the value of "master" for objects defined in the "master" database.

  7. If the referenced project might reference another project, you can select the Suppress errors caused by unresolved references in the referenced project check box. Errors in the referenced project are unlikely to cause errors in your current project. Therefore, you might decide to ignore them.

  8. If you do not want to update the objects and scripts, clear the Update the existing schema object definitions and scripts to use the database reference variables check box, and click OK.

    The reference is added to the database project, and you do not need to perform any other steps in this procedure.

  9. If you want to update the objects and scripts, select the Update the existing schema object definitions and scripts to use the database reference variables check box, and click OK.

    The Preview Changes - Rename Server or Database dialog box appears.

  10. (Optional) Clear the check box next to one or more changes if you do not want that change to be applied.

  11. (Optional) Click any change to display its details in the Preview changes pane.

  12. Click Apply to update the objects and scripts that you specified.

    The reference is added to the database project, and those objects and scripts that you specified are modified to use the SETVAR variables instead of the names of servers and databases.

To refer to objects that are defined in the compiled output (.dbschema file) of another database project

  1. Open the solution that contains the database project to which you want to add a reference.

  2. On the Project menu, click Add Database Reference.

    The Add Database Reference dialog box appears.

  3. To add a reference for a metafile, perform the following steps:

    1. Click Database project metafile (.dbmeta) or schema (.dbschema).

    2. In the box, type the path and the file name of the schema file, or click Browse to specify the schema file for which you want to add a reference.

  4. To define a SETVAR variable for a server, perform the following steps:

    1. Click Define server variable.

    2. In Name, type a name for the variable that represents the server.

    3. In Value, type the name of the server that the variable represents.

    Note

    You do not need to define a server variable if you are referring to an object in a database that resides on the same server as the database to which you are adding the reference.

  5. To specify a SETVAR variable for the database to which you want to refer, follow these steps:

    1. Select the Define database variable check box.

    2. Clear the Literal check box if it was selected.

    3. In Name, type a name for the variable that represents the database.

    4. In Value, type the name of the database that the variable represents.

    Important

    If your database project contains objects that explicitly refer to objects in another database, you should update those references to use the variables that you just defined. If you imported objects from a database that contains cross-database references, you must update the references to resolve the warnings that those explicit cross-database references cause.

  6. To specify a literal value for the database to which you want to refer, follow these steps:

    1. Select the Define database variable check box.

    2. Select the Literal check box.

    3. In Value, type the name of the database to which you want to refer.

    Note

    You might specify a literal instead of a variable and value pair if the database name is always the same in your target deployment environment. For example, you could use a literal with the value of "master" for objects defined in the "master" database.

  7. If the referenced project might reference another project, you can select the Suppress errors caused by unresolved references in the referenced project check box. Errors in the referenced project are unlikely to cause errors in your current project. Therefore, you might decide to ignore them.

  8. If you do not want to update the objects and scripts, clear the Update the existing schema object definitions and scripts to use the database reference variables check box, and click OK.

    The reference is added to the database project, and you do not need to perform any other steps in this procedure.

  9. If you want to update the objects and scripts, select the Update the existing schema object definitions and scripts to use the database reference variables check box, and click OK.

    The Preview Changes - Rename Server or Database dialog box appears.

  10. (Optional) Clear the check box next to one or more changes if you do not want that change to be applied.

  11. (Optional) You can click any change to display its details in the Preview changes pane.

  12. Click Apply to update the schema objects and scripts that you specified.

    The reference is added to the database project, and those objects and scripts that you specified are modified to use the SETVAR variables instead of the names of servers and databases.

To include the contents of a database project in the current solution as part of the referencing project

  1. Open the solution that contains the database project to which you want to add a reference.

  2. On the Project menu, click Add Database Reference.

    The Add Database Reference dialog box appears.

  3. Click Database projects in the current solution, click the database project to which you want to add a reference, and click OK.

    The reference is added to the database project. When you deploy the project to which you added the reference, the project that you specified in the reference is treated as part of the project that you are deploying.

To add a reference to a SQLCLR project in the current solution

  1. Open the solution that contains the database project to which you want to add a reference.

  2. On the Project menu, click Add Reference.

    The Add Reference dialog box appears.

  3. Click the Projects tab if it is not already active.

  4. Click the name of the SQL common language run-time (CLR) project to which you want to refer, and then click OK.

    The reference is added to the database project.

  5. In Solution Explorer, right-click the reference, and click Properties.

    The Properties window appears.

  6. Update the properties of the reference to specify the details of the CREATE ASSEMBLY statement that is generated when you build the database project.

  7. On the File menu, click Save All.

To add a reference to a SQLCLR assembly

  1. Open the solution that contains the database project to which you want to add a reference.

  2. On the Project menu, click Add Reference.

    The Add Reference dialog box appears.

  3. Click the Browse tab if it is not already active.

  4. Specify the assembly (.dll) to which you want to refer, and then click OK.

    The reference is added to the database project.

  5. In Solution Explorer, right-click the reference, and click Properties.

    The Properties window appears.

  6. Update the properties of the reference to specify the details of the CREATE ASSEMBLY statement that is generated when you build the database project.

  7. On the File menu, click Save All.

See Also

Concepts

Using References in Database Projects

Starting Team Development of Databases that Reference Other Databases

Starting Team Development of Databases that Reference SQLCLR Objects

Starting Team Development of Databases that Reference Shared Server Objects

An Overview of Database and Server Projects