title: Is there a way to tell if a table is being referenced in another Database/Server?
body: <p>I am building a <strong>personal use</strong> PHP script using the <code>MSSQL</code> driver in PDO (as such there is <strong>no plan to let anyone else but me</strong> use this) which when provided with a table name will return all objects that referenced it</p>
  
  <p>using <code>MySVR.MyDB.dbo.Staff</code> as my table i first start with this query</p>
  
  <pre><code>SELECT
      referencing_schema_name = SCHEMA_NAME(o.SCHEMA_ID),
      referencing_object_name = o.name,
      referencing_object_type_desc = o.type_desc,
      referenced_schema_name,
      referenced_object_name = referenced_entity_name,
      referenced_object_type_desc = o1.type_desc,
      referenced_server_name, referenced_database_name    
  FROM
      sys.sql_expression_dependencies sed
  INNER JOIN
      sys.objects o ON sed.referencing_id = o.[object_id]
  LEFT OUTER JOIN
      sys.objects o1 ON sed.referenced_id = o1.[object_id]
  WHERE
      referenced_entity_name = 'Staff'
      AND referenced_server_name IS NULL
      AND referenced_database_name IS NULL
  ORDER BY
      o.name
  </code></pre>
  
  <p>which lists all the objects on <code>MyDB</code> which reference my Staff Table</p>
  
  <p>I know that my <code>Staff</code> table is used on another database inside a stored procedure, <code>MySVR.MySecDB.dbo.spMyDB_Staff</code>. To confirm it, I alter the PDO connection string and rerun the query but with <code>referenced_database_name = 'MyDB'</code> to make sure I get only results that reference <code>MySVR.MyDB.dbo.Staff</code> and not <code>MySVR.MySecDB.dbo.Staff</code>.</p>
  
  <p>The problem however is how would I detect if <code>Staff</code> is being referenced in <code>MySecDB</code> or any other database outside <code>MyDB</code> for me to use do this? Likewise how would I detect if my table is being referenced on another server like in <code>MyOtherSVR.anotherDBDB.dbo.spMySVR_MyDB_Staff</code>?</p>
  
  <p>I did think about running the query on every server and database (altering the PDO connection string in a loop) and checking if I got any results but that seems excessive to me.</p>
  
  <p>So is there a way to tell if a table is being referenced in another database / server?</p>