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
Open the solution that contains the database project to which you want to add a reference.
For more information, see How to: Open a Database or Server Project.
On the Project menu, click Add Database Reference.
The Add Database Reference dialog box appears.
To add a reference to a database project, perform the following steps:
Click Database projects in the current solution.
In the list, click the database project to which you want to add a reference.
To define a SETVAR variable for a server, perform the following steps:
Click Define server variable.
In Name, type a name for the variable that represents the server.
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.
To specify a SETVAR variable for the database to which you want to refer, follow these steps:
Select the Define database variable check box.
Clear the Literal check box if it was selected.
In Name, type a name for the variable that represents the database.
In Value, type the name of the database that the variable represents.
Important Note: 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.
To specify a literal value for the database to which you want to refer, follow these steps:
Select the Define database variable check box.
Select the Literal check box.
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.
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.
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.
If you want to update the objects and scripts, click OK.
The Preview Changes - Rename Server or Database dialog box appears.
(Optional) Clear the check box next to one or more changes if you do not want that change to be applied.
(Optional) Click any change to display its details in the Preview changes pane.
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
Open the solution that contains the database project to which you want to add a reference.
For more information, see How to: Open a Database or Server Project.
On the Project menu, click Add Database Reference.
The Add Database Reference dialog box appears.
To add a reference for a metafile, perform the following steps:
Click Database project metafile (.dbmeta) or schema (.dbschema).
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.
To define a SETVAR variable for a server, perform the following steps:
Click Define server variable.
In Name, type a name for the variable that represents the server.
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.
To specify a SETVAR variable for the database to which you want to refer, follow these steps:
Select the Define database variable check box.
Clear the Literal check box if it was selected.
In Name, type a name for the variable that represents the database.
In Value, type the name of the database that the variable represents.
Important Note: 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.
To specify a literal value for the database to which you want to refer, follow these steps:
Select the Define database variable check box.
Select the Literal check box.
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.
If the referenced project might reference another project, you can select the Suppress errors cause 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.
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.
If you want to update the objects and scripts, click OK.
The Preview Changes - Rename Server or Database dialog box appears.
(Optional) Clear the check box next to one or more changes if you do not want that change to be applied.
(Optional) You can click any change to display its details in the Preview changes pane.
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
Open the solution that contains the database project to which you want to add a reference.
For more information, see How to: Open a Database or Server Project.
On the Project menu, click Add Database Reference.
The Add Database Reference dialog box appears.
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
Open the solution that contains the database project to which you want to add a reference.
For more information, see How to: Open a Database or Server Project.
On the Project menu, click Add Reference.
The Add Reference dialog box appears.
Click the Projects tab if it is not already active.
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.
In Solution Explorer, right-click the reference, and click Properties.
The Properties window appears.
Update the properties of the reference to specify the details of the CREATE ASSEMBLY statement that is generated when you build the database project.
On the File menu, click Save All.
To add a reference to a SQLCLR assembly
Open the solution that contains the database project to which you want to add a reference.
For more information, see How to: Open a Database or Server Project.
On the Project menu, click Add Reference.
The Add Reference dialog box appears.
Click the Browse tab if it is not already active.
Specify the assembly (.dll) to which you want to refer, and then click OK.
The reference is added to the database project.
In Solution Explorer, right-click the reference, and click Properties.
The Properties window appears.
Update the properties of the reference to specify the details of the CREATE ASSEMBLY statement that is generated when you build the database project.
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