title: OBJECT_ID of object in another database - how to find database ID or name/fully qualified object name?
body: <p>Example:</p>
  
  <pre><code>USE AnotherDB
  -- This works - same ID as from other DB
  SELECT OBJECT_ID('AnotherDB.ASchema.ATable')
  -- This works
  SELECT OBJECT_NAME(OBJECT_ID('AnotherDB.ASchema.ATable'))
  
  USE ThisDB
  -- This works - same ID as from other DB
  SELECT OBJECT_ID('AnotherDB.ASchema.ATable')
  -- Gives NULL
  SELECT OBJECT_NAME(OBJECT_ID('AnotherDB.ASchema.ATable'))
  </code></pre>
  
  <p>Obviously the metadata functions expect a current database.  The BOL entries typically have language like this for functions like <code>OBJECT_NAME</code> etc.:</p>
  
  <blockquote>
    <p>The Microsoft SQL Server 2005 Database
    Engine assumes that object_id is in
    the context of the current database. A
    query that references an object_id in
    another database returns NULL or
    incorrect results.</p>
  </blockquote>
  
  <p>The reasons I need to be able to do this:</p>
  
  <ol>
  <li><p>I can't USE the other database from within an SP</p></li>
  <li><p>I can't create a proxy UDF stub (or alter anything) in the other databases or in master (or any other database besides my own) to help me out.</p></li>
  </ol>
  
  <p>So how can I get the database from <code>OBJECT_ID('AnotherDB.ASchema.ATable')</code> when I'm in ThisDB?</p>
  
  <p>My goal is to take a possibly partially qualified name from a configuration table, resolving it in the current context to a fully qualified name, use PARSENAME to get the database name and then dynamic SQL to build a script to be able to get to the meta data tables directly with <code>database.sys.*</code> or <code>USE db; sys.*</code></p>