I'm working on a multi-tenanted solution. As such we have a lot of databases on our SQL Server. I'm looking at a locking issue and need to be able to see which locks are being waited on.
I have queried the sys.dm_tran_locks dynamic view, but would like to also include the, database, object and index names referred to by the resource_associated_entity_id column. This links through to the sys.partitions table, but that table only returns rows for the current database and the locks I'm looking at are distributed around several databases.
Is there a way to get this information without creating a cursor and using dynamically generated queries?
Technically "no". There is no meta-data function that will get index names outside of the current database context. This sad fact also adversely affects using the following when trying to use them in a similarly useful manner:
So you have two options:
database_id
, and connects via "context connection = true" to query the intended system view (which is how I have solved this issue in terms of index names and sys.objects in SQL#, but the technique of taking indatabase_id
as a parameter to a SQLCLR Function--Scalar or TVF--and having it connect locally via dynamically built SQL to said database would work the same for any other meta-data).EDIT
To make it even easier, pass in the database name and concatenate that into the
SqlCommand.CommandText
and use the IDs asSqlParameter
s. So the signature would be, in terms of getting the index name:GetIndexName(@DatabaseName sysname, @object_id INT, @index_id INT)
and used as follows:
GetIndexName(DB_NAME(dmv.database_id), dmv.object_id, dmv.index_id)
Just FYI, here is a list of meta-data functions, grouped by whether or not you can specify the database or are current-database-only:
Can Specify Database
database_id
parameter)database_id
parameter)Current-Database-Only