2009-06-21 74 views
10

如果我已經被告知表名(或proc)名稱,但沒有找到該對象所在的連接數據庫,是否有任何簡單的腳本來搜索它?也許搜索系統數據庫中的某個地方? (我正在使用SQL Server 2005)在SQL Server中查找對象(跨數據庫)

回答

25

有一個Information_Schema模式,它是來自SYS模式的表中的一組視圖,您可以查詢以獲取所需內容。

Information_Schema的缺點是您必須爲每種類型的對象編寫一個查詢。最重要的是Information_Schema更易於閱讀。

Sys模式最初看起來有點神祕,但它在一個地方有相同的信息。

基本上,每個數據庫中都有一個名爲SysObjects的表,其中包含所有對象及其類型的名稱。

所以,你想在數據庫中搜索如下:

Select [name] as ObjectName, Type as ObjectType 
From Sys.Objects 
Where 1=1 
and [Name] like '%YourObjectName%' 

現在,如果你想搜索的表和存儲的特效來限制這只是,你會怎麼做

Select [name] as ObjectName, Type as ObjectType 
From Sys.Objects 
Where 1=1 
and [Name] like '%YourObjectName%' 
and Type in ('U', 'P') 

如果你查看對象類型,你會發現一個完整的列表視圖,觸發器等。

現在,如果你想在每個數據庫中搜索這個,你將不得不迭代遍歷數據庫。您可以執行以下操作之一:

如果要在不帶任何子句的情況下搜索每個數據庫,請使用sp_MSforeachdb,如此處的答案中所示。

如果您只想搜索特定的數據庫,請使用「USE DBName」,然後使用搜索命令。

在這種情況下,您將從參數化中受益匪淺。請注意,您正在搜索的數據庫的名稱將不得不在每個查詢(DatabaseOne,DatabaseTwo ...)中被替換。檢查了這一點:

Declare @ObjectName VarChar (100) 

Set @ObjectName = '%Customer%' 

Select 'DatabaseOne' as DatabaseName, [name] as ObjectName, Type as ObjectType 
From DatabaseOne.Sys.Objects 
Where 1=1 
and [Name] like @ObjectName 
and Type in ('U', 'P') 
UNION ALL 
Select 'DatabaseTwo' as DatabaseName, [name] as ObjectName, Type as ObjectType 
From DatabaseTwo.Sys.Objects 
Where 1=1 
and [Name] like @ObjectName 
and Type in ('U', 'P') 
UNION ALL 
Select 'DatabaseThree' as DatabaseName, [name] as ObjectName, Type as ObjectType 
From DatabaseThree.Sys.Objects 
Where 1=1 
and [Name] like @ObjectName 
and Type in ('U', 'P') 
+3

'WHERE 1 = 1`是什麼意思? – itsho 2014-12-07 07:57:28

+2

@itsho,如果宇宙破裂,Raj More不想浪費時間運行數據庫查詢。 :) – user1172763 2015-02-27 15:43:57

0

您可以使用sp_MSforeachdb搜索所有數據庫。

聲明@RETURN_VALUE詮釋

聲明@命令1爲nvarchar(2000)

集@命令1 = 「你的命令放在這裏」

EXEC @RETURN_VALUE = sp_MSforeachdb @命令1 = @命令1

Raj

2

最簡單的方法就是點擊information_schemas ...

SELECT * 
FROM information_schema.Tables 
WHERE [Table_Name]='????' 

SELECT * 
FROM information_schema.Views 
WHERE [Table_Name]='????' 

SELECT * 
FROM information_schema.Routines 
WHERE [Routine_Name]='????' 
6
sp_MSforeachdb 'select db_name(), * From ?..sysobjects where xtype in (''U'', ''P'') And name = ''ObjectName''' 

而不是「對象名」插入對象,你正在尋找。第一列將顯示對象所在的數據庫的名稱。

0
set ANSI_NULLS ON 
set QUOTED_IDENTIFIER ON 
go 


/********************************************************************** 
Naziv procedure  : sp_rfv_FIND 
Ime i prezime autora: Srdjan Nadrljanski 
Datum kreiranja  : 13.06.2013. 
Namena    : Traži sql objekat na celom serveru 
Tabele    : 
Ulazni parametri : 
Izlazni parametri : 
Datum zadnje izmene : 
Opis izmene   : 
exec sp_rfv_FIND 'TUN','' 
**********************************************************************/ 
CREATE PROCEDURE [dbo].[sp_rfv_FIND] ( @SEARCHSTRING VARCHAR(255), 
             @notcontain Varchar(255) 
             ) 
AS 


declare @text varchar(1500),@textinit varchar (1500) 
set @textinit= 
'USE @sifra 

insert into ##temp2 
select ''@sifra''as dbName,a.[Object Name],a.[Object Type] 
from(
SELECT DISTINCT sysobjects.name AS [Object Name] , 
case 
when sysobjects.xtype = ''C'' then ''CHECK constraint'' 
when sysobjects.xtype = ''D'' then ''Default or DEFAULT constraint'' 
when sysobjects.xtype = ''F'' then ''Foreign Key'' 
when sysobjects.xtype = ''FN'' then ''Scalar function'' 
when sysobjects.xtype = ''P'' then ''Stored Procedure'' 
when sysobjects.xtype = ''PK'' then ''PRIMARY KEY constraint'' 
when sysobjects.xtype = ''S'' then ''System table'' 
when sysobjects.xtype = ''TF'' then ''Function'' 
when sysobjects.xtype = ''TR'' then ''Trigger'' 
when sysobjects.xtype = ''U'' then ''User table'' 
when sysobjects.xtype = ''UQ'' then ''UNIQUE constraint'' 
when sysobjects.xtype = ''V'' then ''View'' 
when sysobjects.xtype = ''X'' then ''Extended stored procedure'' 
end as [Object Type] 
FROM sysobjects 
WHERE 
sysobjects.type in (''C'',''D'',''F'',''FN'',''P'',''K'',''S'',''TF'',''TR'',''U'',''V'',''X'') 
AND sysobjects.category = 0 
AND CHARINDEX(''@SEARCHSTRING'',sysobjects.name)>0 
AND ((CHARINDEX(''@notcontain'',sysobjects.name)=0 or 
CHARINDEX(''@notcontain'',sysobjects.name)<>0)) 
)a' 

    set @textinit=replace(@textinit,'@SEARCHSTRING',@SEARCHSTRING) 
    set @textinit=replace(@textinit,'@notcontain',@notcontain) 


SELECT name AS dbName,cast(null as varchar(255)) as ObjectName,cast(null as varchar(255)) as ObjectType 
into ##temp1 
from master.dbo.sysdatabases order by name 

SELECT * INTO ##temp2 FROM ##temp1 WHERE 1 = 0 


declare @sifra VARCHAR(255),@suma int,@brojac int 

set @suma=(select count(dbName) from ##temp1) 

DECLARE c_k CURSOR LOCAL FAST_FORWARD FOR 
SELECT dbName FROM ##temp1 ORDER BY dbName DESC 

OPEN c_k 
FETCH NEXT FROM c_K INTO @sifra 
SET @brojac = 1 
WHILE (@@fetch_status = 0) AND (@brojac <= @suma) 
BEGIN 

    set @text=replace(@textinit,'@sifra',@sifra) 

    exec (@text) 

    SET @brojac = @brojac +1 

    DELETE FROM ##temp1 WHERE dbName = @sifra 

     FETCH NEXT FROM c_k INTO @sifra 
END 
close c_k 
DEALLOCATE c_k 

select * from ##temp2 
order by dbName,ObjectType 
drop table ##temp2 
drop table ##temp1 
相關問題