Search code examples
sql-serverstored-proceduressql-server-2000

How can I programmatically determine if a stored procedure selects from another database?


On an MS SQL Server 2000 installation I have numerous stored procedures that pull data from databases other than the one it's stored in. All selects occur on the same database server. For example:

select * from [OtherDatabase]..table

How can I find which procedures do that sort of thing without eyeballing each one?


Solution

  • The following is a stored proc that will search for text contained in any stored proc. You could use this to search for the various databases that might get called.

    CREATE PROCEDURE FindTextInSP
    @StringToSearch varchar(100) 
    AS 
       SET @StringToSearch = '%' +@StringToSearch + '%'
       SELECT Distinct SO.Name
       FROM sysobjects SO (NOLOCK)
       INNER JOIN syscomments SC (NOLOCK) on SO.Id = SC.ID
       AND SO.Type = 'P'
       AND SC.Text LIKE @stringtosearch
       ORDER BY SO.Name
    GO