Sql-server – Referencing a stored procedure by a name description across all databases in a SQL Server 2000 Instance

sql-server-2000ssrsstored-procedures

Does anyone have a valid method in referencing system information to search for a stored procedure across all databases in a SQL Server 2000 instance? I am currently tasked with migrating crystal reports to SSRS and the customer has roughly 500 or more crystal reports with stored procedures spread over multiple databases. I know I can filter each database's programmability folder by text but I was looking for an outcome in which I can query master.dbo.sysdatabases and reference that to syscomments in which I can specify text and find a stored procedure tied to a database.

Best Answer

Just use a cursor to loop through the databases and query the sysobjects table:

Select * from sysobjects 
where type='P' 
and name='StoredProcName'

(Cursor code not included.) Or use the (undocumented) sp_MSforeachdb:

EXEC sp_MSforeachdb 'USE ? SELECT ''?'' as dbname, name FROM sysobjects 
      WHERE type = ''P'' AND name = ''StoredProcName''

(don't split that over two lines, though)

Also, sp_MSforeachdb has known issues, like failing on very long database names, so I usually prefer the cursor.