在SQL Server中查找对象(跨数据库)

时间:2009-06-21 23:33:42

标签: sql-server sql-server-2005

如果我被告知一个表(或proc)名称,但没有告诉该对象所在的连接数据库,是否有任何简单的脚本来搜索它?也许搜索系统数据库中的某个地方? (我正在使用SQL Server 2005)

10 个答案:

答案 0 :(得分:32)

有一个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')

答案 1 :(得分:9)

sp_MSforeachdb 'select db_name(), * From ?..sysobjects where xtype in (''U'', ''P'') And name = ''ObjectName'''

而不是'ObjectName'插入您正在寻找的对象。第一列将显示对象所在的数据库的名称。

答案 2 :(得分:3)

最简单的方法是点击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]='????'

答案 3 :(得分:1)

您可以使用sp_MSforeachdb搜索所有数据库。

声明@RETURN_VALUE int

声明@ command1 nvarchar(2000)

set @ command1 =“你的命令就在这里”

exec @RETURN_VALUE = sp_MSforeachdb @ command1 = @ command1

拉​​吉

答案 4 :(得分:1)

SELECT NAME AS ObjectName
    ,schema_name(o.schema_id) AS SchemaName, OBJECT_NAME(o.parent_object_id) as TableName
    ,type
    ,o.type_desc
FROM sys.objects o
WHERE o.is_ms_shipped = 0
    AND o.NAME LIKE '%UniqueID%'
ORDER BY o.NAME

答案 5 :(得分: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

答案 6 :(得分:0)

----选项2

SELECT DISTINCT
    o.name,
    o.xtype
FROM
    syscomments c
    INNER JOIN
        sysobjects o
        ON
            c.id=o.id
WHERE
    c.TEXT LIKE '%TableName%'
order by
    o.name desc,
    o.xtype desc

答案 7 :(得分:0)

您可以通过使用以下查询来实现:

EXEC sp_msforeachdb 
    'IF EXISTS
    (
        SELECT  1 
        FROM    [?].sys.objects 
        WHERE   name LIKE ''OBJECT_TO_SEARCH''
    )
    SELECT 
        ''?''       AS DB, 
        name        AS Name, 
        type_desc   AS Type 
    FROM [?].sys.objects 
    WHERE name LIKE ''OBJECT_TO_SEARCH'''

只需将OBJECT_TO_SEARCH替换为您感兴趣的实际对象名称(或对象名称的一部分,用%包围)。

此处有更多详细信息:https://peevsvilen.blog/2019/07/30/search-for-an-object-in-sql-server/

答案 8 :(得分:0)

select db_name(), * From sysobjects where xtype in ('U', 'P') And name = 'OBJECT_name'

第一列将显示对象所在数据库的名称。

答案 9 :(得分:0)

可能与顶部答案略有不同,因为DB_NAME()总是返回执行的内容db。因此,对我来说更像下面这样:

sp_MSforeachdb 'select DB_name(db_id(''?'')) as DB, * From ?..sysobjects where xtype in (''U'', ''P'') And name like ''[_]x[_]%'''

就我而言,我正在寻找名称以_x_开头的表

干杯, 翁德里(Endrej)