在使用SQL Server 2005的性能報表進行調優的過程中,有些語句不知在哪個數據庫中執行,可參看這篇文章[原]一次SQL Server調優經歷 ,於是針對這個問題,我寫了兩段腳本用於按照對象名找出其所在的數據庫。
在編寫的過程中發現SQL Server 2005比SQL Server 2000在操作集合方面完善很多。第一段SQL Server 2005專用腳本使用了集合的交操作,非常簡單地完成任務了,而第二段通用的就要借助臨時表。
運行方式很簡單,把“用戶修改區域”裡面改成你需要查找的對象名,然後把SQL腳本copy到查詢分析器中運行就OK了。
例如:將用戶修改區域改成:
set @target=N'
select ''ASPnet_Paths'' union all
select ''ASPnet_Profile''
';
執行腳本之後將會打印以下語句。
Object(s) Found In Database: MSPetShop4Services
SQL Server 2005:
--------------------說明-------------------
--在同一個實例的SQL Server 2005數據庫
--中按照對象名找出其所在的數據庫
--@taget為對象的名字集合,關系為'與'
--Powered by killkill
--------------------說明-------------------
use master;
declare @target varchar(max);
----------------用戶修改區域---------------
--示例 Example
--set @target=N'
--select ''<tableName/vIEwName/spName/etc..>'' union all
--select ''<tableName/vIEwName/spName/etc..>''
--';
set @target=N'
select ''ASPnet_Paths'' union all
select ''ASPnet_Profile''
';----------------用戶修改區域---------------
declare @cmd varchar(max);
declare cur_cmd cursor
for
select
'use '+ name+ ';'+
'if not exists ('+
@target+
'except '+
'select name from '+
'sys.objects ' +
'intersect ('+
@target+
')'+
')'+
'print ''Object(s) Found In Database: '+
name+
''' '
from sys.databases
where name not in ('master','tempdb','model','msdb');
open cur_cmd;
fetch next from cur_cmd into @cmd;
While @@fetch_status=0
begin
exec(@cmd);
--print @cmd; --only for debug
fetch next from cur_cmd into @cmd;
end
print N'Powered by killkill';
close cur_cmd;
deallocate cur_cmd;
SQL 2000/2005 通用:
--------------------說明-------------------
--在同一個實例的SQL Server 2000/2005數據庫
--中按照對象名找出其所在的數據庫
--@taget為對象的名字集合,關系為'與'
--Powered by killkill
--------------------說明-------------------
use master;
declare @target varchar(8000);
----------------用戶修改區域---------------
--示例 Example
--set @target=N'
--select ''<tableName/vIEwName/spName/etc..>'' union all
--select ''<tableName/vIEwName/spName/etc..>''
--';
set @target=N'
select ''CZCOK2'' union all
select ''CZGSKG''
';
----------------用戶修改區域---------------
declare @cmd varchar(8000);
declare cur_cmd cursor
for
select
'use '+name+'; '+
N'
set nocount on;
declare @targetCount int;
declare @foundCount int;
if object_id(''tempdb..#tmpObjectNames'') is not null
drop table #tmpObjectNames
create table #tmpObjectNames
(
name varchar(500)
)
insert into #tmpObjectNames'+
@target+
'select @foundCount=count(*) from sysobjects inner join #tmpObjectNames
on sysobjects.name = #tmpObjectNames.name collate Chinese_PRC_CI_AI_WS
select @targetCount=count(*) from #tmpObjectNames
if ( @foundCount = @targetCount )
print ''Object(s) Found In Database: '+name+'''
set nocount off;
drop table #tmpObjectNames;
'
from sysdatabases
where name not in ('master','tempdb','model','msdb');
open cur_cmd;
fetch next from cur_cmd into @cmd;
While @@fetch_status=0
begin
exec(@cmd);
--print @cmd; --only for debug
fetch next from cur_cmd into @cmd;
end
print N'Powered by killkill';
close cur_cmd;
deallocate cur_cmd;
在一兩個數據庫中找出某些表的所屬數據庫還是可以接受的,但是在三十多個數據庫裡找出某些表所屬的數據庫,還是得借助上面的腳本啦。