Click here to Skip to main content
Click here to Skip to main content

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

By , 16 Feb 2013
 
  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)

About the Author

1Lam
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 17 Feb 2013
Article Copyright 2011 by 1Lam
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid