If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > find text string in database

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-02-03, 12:56
qslx qslx is offline
Registered User
 
Join Date: Dec 2003
Posts: 2
Unhappy find text string in database

Hello,
I'd like to find a specific text string searching in all tables within same database.
Is there a way to make only one query to all tables at the same time?

Thank you very much for your attention.

Qslx
Reply With Quote
  #2 (permalink)  
Old 12-02-03, 13:25
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,732
No. You would have to write a procedure that looped through all the tables and checked every column.

This question makes me suspect that there are some design issues with your database schema.

blindman
Reply With Quote
  #3 (permalink)  
Old 12-02-03, 13:30
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
Ya mean like:

Code:
USE Northwind
GO

CREATE TABLE myTable99 (TABLE_NAME sysname, COLUMN_NAME sysname, Occurs int)
GO

SET NOCOUNT ON

DECLARE @SQL varchar(8000), @TABLE_NAME sysname, @COLUMN_NAME sysname, @Sargable varchar(80), @Count int

SELECT @Sargable = 'Beer'

DECLARE insaneCursor CURSOR FOR 
	SELECT c.TABLE_NAME, c.COLUMN_NAME 
	  FROM INFORMATION_SCHEMA.Columns c INNER JOIN INFORMATION_SCHEMA.Tables t
	    ON t.TABLE_SCHEMA = c.TABLE_SCHEMA AND t.TABLE_NAME = c.TABLE_NAME
	 WHERE c.DATA_TYPE IN ('char','nchar','varchar','nvarchar','text','ntext')
	   AND t.TABLE_TYPE = 'BASE TABLE'

OPEN insaneCursor
 
FETCH NEXT FROM insaneCursor INTO @TABLE_NAME, @COLUMN_NAME

WHILE @@FETCH_STATUS = 0
	BEGIN
		SELECT @SQL = 'INSERT INTO myTable99 (TABLE_NAME, COLUMN_NAME, Occurs) SELECT '
				+ '''' +  @TABLE_NAME + '''' + ','
				+ '''' + @COLUMN_NAME + '''' + ','
				+ 'COUNT(*) FROM [' + @TABLE_NAME  
				+ '] WHERE [' + @COLUMN_NAME + '] Like '
				+ ''''+ '%' + @Sargable + '%' + ''''
		--SELECT @SQL
		EXEC(@SQL)
		IF @@ERROR <> 0 
			BEGIN 
				SELECT @SQL
				SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = @TABLE_NAME
				GOTO Error
			END 
		FETCH NEXT FROM insaneCursor INTO @TABLE_NAME, @COLUMN_NAME
	END

SELECT * FROM myTable99 WHERE Occurs <> 0 


Error:
CLOSE insaneCursor
DEALLOCATE insaneCursor

GO

DROP TABLE myTable99
GO

SET NOCOUNT OFF
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #4 (permalink)  
Old 12-03-03, 12:19
qslx qslx is offline
Registered User
 
Join Date: Dec 2003
Posts: 2
Talking find text string in database

Hi Brett,
Thanks a lot for you help.
It works great!

Cheers,
Reply With Quote
  #5 (permalink)  
Old 12-03-03, 13:06
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,732
brett, don't you have any hobbies?
Reply With Quote
  #6 (permalink)  
Old 12-03-03, 13:31
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
Yeah...SQL

That was a cut and paste from my toolbox....

You kidding...I'm writting a sql server version of the window explorer find function...using xp_cmdshell, because it's tooooooooooooo painful to deal with server ops...
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #7 (permalink)  
Old 12-03-03, 14:14
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,732
I can't tell when you're kidding and when you're not!
Reply With Quote
  #8 (permalink)  
Old 12-03-03, 14:47
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
I'd say I'm an Enigma...but that's taken already...8-)

And I finally got server ops to give me clearence, so I won't have to build the explorer

Are Margarittas a hobby?
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #9 (permalink)  
Old 12-07-11, 08:44
earsby earsby is offline
Registered User
 
Join Date: Dec 2011
Posts: 1
Genius

I just registered to say thanks for this script.

I use it all the time to find references in systems... its mega
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On