Find all references to an object in an SQL Server database

后端 未结 10 1717
深忆病人
深忆病人 2020-12-13 10:02

I\'m trying to find all references to an object in an SQL Server database.

How can I quickly search? SQL Server Management Studio does not seem to do it. I use http:

10条回答
  •  悲&欢浪女
    2020-12-13 10:26

    If you want to use OMG Ponies sql as a keyboard shortcut in SSMS, add the following SP to your master db.

    USE [master]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE PROCEDURE [dbo].[SP_FindAllReferences]
    @targetText nvarchar(128)
    AS
    BEGIN
        SET NOCOUNT ON;
    
        declare @origdb nvarchar(128)
        select @origdb = db_name()
    
        declare @sql nvarchar(1000)
    
        set @sql = 'USE [' + @origdb +'];' 
        set @sql += 'select object_name(m.object_id), m.* '
        set @sql += 'from sys.sql_modules m  where m.definition like N' + CHAR(39) + '%' + @targetText + '%' + CHAR(39)
    
        exec (@sql)
    
        SET NOCOUNT OFF;
    END
    

    Then you just need to add dbo.SP_FindAllReferences to your keyboard shortcuts and then you can use it in the context of any DB on your server.

    Cheers!

    NB: If you are using SQL Server 2005 you will have to replace

    @sql +=
    

    with

    @sql = @sql +
    

提交回复
热议问题