title: SQL Server cross database alias
body: <p>I'm trying to understand how I can use an alias to reference another database in the same instance, without having to use a hardcoded name.</p>
  
  <p>The scenario is as below:</p>
  
  <p>I have a data db with stores data, an audit db which keeps all changes made. for various reason, i want to keep the audit data in a separate database, not least because it can get quite large and for reporting purposes.</p>
  
  <p>In the data db, I don't want to reference this by a hardcoded name but an alias so that in different environments, I don't have to change the name and various sp's to reference the new name.</p>
  
  <p>for example:</p>
  
  <pre><code>mydevdata
  mydevaudit
  </code></pre>
  
  <p>If a sp exists in <code>mydevdata</code> such as which calls the <code>mydevaudit</code>, I don't want to change the sp when I go to test where the db's may be called <code>mytestdata</code> and <code>mytestaudit</code>. Again, for various reasons, the database names can change, more to do with spaces an instances etc. </p>
  
  <p>So if I had procedure in <code>mydevdata</code>:</p>
  
  <pre><code>proc A
  begin
  
  insert into mydevaudit.table.abc(somecol)
  select 1
  
  end
  </code></pre>
  
  <p>when I go to test, I don't want to be change the procedure to reference another name, (assume for sake of argument that happened)</p>
  
  <p>Instead I am looking to do something like:</p>
  
  <pre><code>proc A
  begin
  
  insert into AUDITEBALIAS.table.abc(somecol)
  select 1
  
  end
  </code></pre>
  
  <p>I am interested in finding out how I could do something like that, and the pro's and cons.</p>
  
  <p>Also, dymnamic SQL is not an option.</p>
  
  <p>thanks in advance for you help.</p>