Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Find paricular word or text in all database objects on the server

5.00/5 (2 votes)
16 Feb 2013CPOL 12.2K  
Create the following table on a local database:CREATE TABLE [dbo].[TB_TEMP_TABLESTORE]( [SEARCH_STRING] [nvarchar](128) NULL, [DB_NAME] [nvarchar](128) NULL, [OBJECT_NAME] [nvarchar](128) NULL, [OBJECT_TYPE] [nvarchar](50) NULL) ON [PRIMARY]Rename Yourdatabase to the same...
  1. Create the following table on a local database:

     

    CREATE TABLE [dbo].[TB_TEMP_TABLESTORE](
    	[SEARCH_STRING] [nvarchar](128) NULL,
    	[DB_NAME] [nvarchar](128) NULL,
    	[OBJECT_NAME] [nvarchar](128) NULL,
    	[OBJECT_TYPE] [nvarchar](50) NULL
    ) ON [PRIMARY]
     

  2. Rename Yourdatabase to the same database where you stored table.

  3. This will give you all instances throughout the entire Server.

  4. 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)