Click here to Skip to main content
12,076,206 members (45,962 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

5.6K views
3 bookmarked
Posted

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

, 16 Feb 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

1Lam
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralWhat is 'DNU', and a modification Pin
hmdmgd39-Aug-13 14:31
memberhmdmgd39-Aug-13 14:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160208.1 | Last Updated 17 Feb 2013
Article Copyright 2011 by 1Lam
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid