Good for when you make code changes across a project that affects other projects or updates to a new server:
CREATE PROCEDURE [dbo].[spr_View_SearchAllObject]
-- Add the parameters for the stored procedure here
@StringToSearch NVARCHAR(128)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
--SET NOCOUNT ON;
--DECLARE @StringToSearch varchar(100)
--SET @StringToSearch = 'TB_STATES'
SET @StringToSearch = '%' + @StringToSearch + '%'
DECLARE @SQL NVARCHAR(MAX)
TRUNCATE TABLE YourDatabase.DBO.TB_TEMP_TABLESTORE
DECLARE DBNAME_cursor CURSOR FOR
select [name] FROM [master].[sys].[databases] (NOLOCK)
where [name] not like '%DNU%'
order by 1
OPEN DBNAME_cursor
DECLARE @DBNAME_NAME varchar(100)
FETCH NEXT FROM DBNAME_cursor INTO @DBNAME_NAME
WHILE (@@FETCH_STATUS <> -1)
BEGIN
-- set up cursor and run for each database name
SET @SQL=N'USE [' + @DBNAME_NAME + ']
INSERT INTO [YourDatabase].[dbo].[TB_TEMP_TABLESTORE]
([SEARCH_STRING]
,[DB_NAME]
,[OBJECT_NAME]
,[OBJECT_TYPE])
SELECT Distinct ''' + @StringToSearch +''' AS [SEARCH_STRING],DB_NAME() as [DB_NAME], SO.Name AS [OBJECT_NAME], SO.[Type] AS [OBJECT_TYPE]
FROM sysobjects SO (NOLOCK)
WHERE [name] not like ''%DNU%''
AND
(
SO.Name LIKE ''' + @StringToSearch + '''
OR EXISTS (SELECT * FROM syscomments SC (NOLOCK)
WHERE SO.Id = SC.ID
AND SC.Text LIKE ''' + @StringToSearch + ''')
)
ORDER BY SO.Name'
--PRINT @SQL
EXEC sp_executesql @SQL
FETCH NEXT FROM DBNAME_cursor INTO @DBNAME_NAME
END
CLOSE DBNAME_cursor
DEALLOCATE DBNAME_cursor
SELECT * FROM [YourDatabase].[dbo].[TB_TEMP_TABLESTORE]
END