If you ever need to use the OBJECT_NAME()
function to get an object’s name from a different database in SQL Server, you might run into issues if you don’t know how it works.
You probably know that OBJECT_NAME()
accepts an object_id
argument that tells SQL Server which object to get the name from.
What you may or may not know, is that this function also accepts an optional database_id
argument that tells SQL Server which database the object_id
belongs to.
By default, SQL Server assumes that object_id
is in the context of the current database. In this case, a query that references an object_id
in another database will return NULL or (even worse) incorrect results.