Solved

How do I  search all SQL tables for a value?

Posted on 2007-12-04
2
2,155 Views
Last Modified: 2010-04-21
I have a Great Plains 9 database that I need to find a value... it's somewhere in one of the tables but I haven't been able to locate it.  

How do I script a search to iterate through each table looking for a string?
0
Comment
Question by:awsiemieniec
2 Comments
 
LVL 17

Accepted Solution

by:
Chris Mangus earned 500 total points
ID: 20407104
I use this code.  Replace @myValue with the value you're searching for.  If you are looking for numeric data then uncomment the appropriate line.
Declare @TN as varchar(200), @CN as varchar(200), @myValue varchar(30), @SQL as nvarchar(1000)

, @SN as varchar(200), @Exact_Match bit
 

Create Table #myTable (Table_Name varchar(200), Column_Name varchar(200), Number_Of_Rows int)
 

-- Replace @myValue with the value you're searching for in the database

Set @myValue = 'mysearchdata' 

-- 0 for LIKE match, 1 for exact match

Set @Exact_Match = 0    
 

Declare myCursor Cursor For

Select T.Table_Name, C.Column_Name, T.Table_Schema

From INFORMATION_SCHEMA.TABLES T Inner Join INFORMATION_SCHEMA.COLUMNS C 

On T.Table_Schema = C.Table_Schema And T.Table_Name = C.Table_Name

Where T.Table_Name <> 'dtproperties' And Table_Type = 'Base Table'

And C.Data_Type In ('varchar','char','nvarchar','nchar','sql_variant')

--And C.Data_Type In ('text','ntext')

--And C.Data_Type In ('tinyint','int','bigint','numeric','decimal','money','float','smallint','real','smallmoney')

--And C.Data_Type In ('datetime','dmalldatetime')

-- Fields not searched: image, uniqueidentifier, bit, varbinary, binary, timestamp

Open myCursor

Fetch Next From myCursor Into @TN, @CN, @SN

While @@Fetch_Status <> -1

Begin

	If @Exact_Match = 0

		Set @SQL = N'Insert Into #myTable Select ''' + @SN + '.' + @TN + ''', ''' + @CN + ''', Count(*) From [' + @SN + '].[' + @TN + '] Where [' + @CN + '] Like ''%' + @myValue + '%'''

	    Else

		Set @SQL = N'Insert Into #myTable Select ''' + @SN + '.' + @TN + ''', ''' + @CN + ''', Count(*) From [' + @SN + '].[' + @TN + '] Where [' + @CN + '] = ''' + @myValue + ''''

	--Print @SQL

	Exec sp_executesql @SQL 

	Fetch Next From myCursor Into @TN, @CN, @SN

End

Close myCursor

Deallocate myCursor

Select * From #myTable Where Number_Of_Rows > 0 Order By Table_Name

Drop Table #myTable

Open in new window

0
 

Author Closing Comment

by:awsiemieniec
ID: 31412709
Excellent help.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now