Find an object in SQL Server (cross-database)

后端 未结 10 1181
太阳男子
太阳男子 2021-02-01 16:52

If I\'ve been told a table (or proc) name, but not which connected database the object is located in, is there any simple script to search for it? Maybe search somewhere in the

10条回答
  •  日久生厌
    2021-02-01 17:54

    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
    

提交回复
热议问题