如何在涉及sys表的2个数据库之间加入查询?

时间:2010-09-03 13:40:04

标签: sql-server tsql

-- find last usage info, 
-- how far back this information goes depends on the 
-- server/database settings

select --
ss.name as SchemaName, so.name as SprocName
,so.create_date as SprocCreated, so.modify_date as SprocModified 
,so.object_id
,stat.last_user_seek,stat.last_user_scan,stat.last_user_lookup,stat.last_user_update
,stat.last_system_seek,stat.last_system_scan,stat.last_system_lookup, stat.last_system_update
from sys.objects so

inner join sys.schemas ss on so.schema_id=ss.schema_id

left join sys.dm_db_index_usage_stats stat
on so.object_id=stat.object_id

where ss.name<>'sys' and so.type='P' 
union 
select 
ss.name as SchemaName, so.name as SprocName
,so.create_date as SprocCreated, so.modify_date as SprocModified 
,so.object_id
,stat.last_user_seek,stat.last_user_scan,stat.last_user_lookup,stat.last_user_update
,stat.last_system_seek,stat.last_system_scan,stat.last_system_lookup, stat.last_system_update
from db_dit.dbo.sys.objects so 

inner join sys.schemas ss on so.schema_id=ss.schema_id

left join sys.dm_db_index_usage_stats stat
on so.object_id=stat.object_id

where ss.name<>'sys' and so.type='P'
order by case when stat.object_id is null then 0 else 1 end,ss.name,so.name

我正在尝试验证我的dev对dev的更改是否被推送到一个简单的查询中,但我似乎无法查询另一个db上的sys表我得到Invalid object name 'db_dit.dbo.sys.objects'.

1 个答案:

答案 0 :(得分:2)

代替db_dit.dbo.sys.objectsdb_dit.sys.objects

sys已经是架构,不需要dbo

例如

select * from tempdb.sys.objects s1
    join msdb.sys.objects s2 on s1.name = s2.name