title: How can I reference another DB from a VS DB project?
body: <p>We have several databases, say DB1, DB2, DB3 etc.</p>
  
  <p>They have to have identical code base, so we use a DB project in Visual Studio 2012 and generate a SQL script for deployment based on comparison between the project and UAT/Prod DB1. Then this script is applied to DB1-DBn.</p>
  
  <p>For the very first time in the history of this DB project I had to create a function that contained a hardcoded database name, example:</p>
  
  <pre><code>inner join DB1.schema.table1 as t1 on
  </code></pre>
  
  <p>And now the project cannot be built or comparison cannot be updated or script generated (Update and Generate Script buttons disabled) due to a number of errors pertaining to that database reference, as VS seems to believe that DB1 does not exist.</p>
  
  <p>I tried to add a project level SQLCMD variable <code>$(DB)</code> and set it to <code>DB1</code> default value and use it as</p>
  
  <pre><code>inner join [$(DB1)].schema.table1 as t1 on
  </code></pre>
  
  <p>to work around the errors, but it did not seem to make any difference.</p>
  
  <p>Edit:</p>
  
  <p>A suggestion was made to add a circular project reference to itself and assign to it the same variable I was trying to add manually, not sure how to accomplish that.</p>