Hi, I'm trying to get object names in one query where the objects comefrom multiple databases. The scope will likely be master when running from a response job to a blocking alert. Is there a trick I can use to avoid having to loop though each database andusing a database scope specific query?I created a function as an example of what I'd like to do (e.g., database id as a parameter), butthis will not work.Perhaps OPENQUERY or something? If somebody has done what I would like, I'd appreciate a tip. Thanks, Randy
SELECT
sys.dm_tran_locks.*,dbo
.fn_translockobjectid( sys.dm_tran_locks.resource_type, sys.dm_tran_locks.resource_database_id, sys.dm_tran_locks.resource_associated_entity_id ) as [ObjectId] FROM sys.dm_tran_locks WITH (NOLOCK) INNER JOIN sys.dm_os_waiting_tasks wt WITH (NOLOCK) ON wt.resource_address = sys.dm_tran_locks.lock_owner_addressCREATE
FUNCTION dbo.fn_translockobjectid(@resource_type
nvarchar(120),@resource_database_id
int,@resource_associated_entity_id
bigint )RETURNS
intAS
BEGIN
DECLARE
@ObjectId int,@SQL
nvarchar(max);SET
@SQL = N'USE ' + DB_NAME(@resource_database_id) + N'DECLARE @ObjectId int;
SET @ObjectId = CASE
WHEN @resource_type = ''OBJECT''
THEN @resource_associated_entity_id
WHEN @resource_type IN (''HOBT'', ''RID'', ''KEY'',''PAGE'')
THEN (SELECT object_id FROM sys.partitions
WHERE hobt_id = @resource_associated_entity_id)
WHEN @resource_type = ''ALLOCATION_UNIT''
THEN (
SELECT CASE
WHEN type IN (1, 3)
THEN (SELECT object_id FROM sys.partitions
WHERE hobt_id = allocation_unit_id)
WHEN type = 2
THEN (SELECT object_id FROM sys.partitions
WHERE partition_id = allocation_unit_id)
ELSE NULL
END
FROM sys.allocation_units
WHERE allocation_unit_id = @resource_associated_entity_id
)
ELSE NULL
END;
SELECT @ObjectId;'
EXEC
dbo.sp_executeSQL@SQL
, N'@resource_type nvarchar(120), @resource_associated_entity_id int
, @ObjectId int OUTPUT'
, @resource_type = @resource_type , @resource_associated_entity_id = @resource_associated_entity_id , @ObjectId = @ObjectId OUTPUTRETURN
@ObjectIdEND