SQL Server – Find Field Value in Database

posted on Saturday, August 16, 2008 11:23 AM

The following is a SQL Script that can be run in a database to return all tables and columns where a particular value is present.  This can be used for strings or values with a small modification.

This type of thing is great when moving applications/products between servers.  This is certainly a good script to include in your master table to be used over and over.

DECLARE @value VARCHAR(64)
DECLARE @sql VARCHAR(1024)
DECLARE @table VARCHAR(64)
DECLARE @column VARCHAR(64)

SET @value = 'valuehere'

CREATE TABLE #t (
    tablename VARCHAR(64),
    columnname VARCHAR(64)
)

DECLARE TABLES CURSOR
FOR

    SELECT o.name, c.name
    FROM syscolumns c
    INNER JOIN sysobjects o ON c.id = o.id
    WHERE o.type = 'U' AND c.xtype IN (167, 175, 231, 239)
    ORDER BY o.name, c.name

OPEN TABLES

FETCH NEXT FROM TABLES
INTO @table, @column

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = 'IF EXISTS(SELECT NULL FROM [' + @table + '] '
    --SET @sql = @sql + 'WHERE RTRIM(LTRIM([' + @column + '])) = ''' + @value + ''') '
    SET @sql = @sql + 'WHERE RTRIM(LTRIM([' + @column + '])) LIKE ''%' + @value + '%'') '
    SET @sql = @sql + 'INSERT INTO #t VALUES (''' + @table + ''', '''
    SET @sql = @sql + @column + ''')'

    EXEC(@sql)

    FETCH NEXT FROM TABLES
    INTO @table, @column
END

CLOSE TABLES
DEALLOCATE TABLES

SELECT *
FROM #t

DROP TABLE #t

Comments


# re: SQL Server – Find Field Value in Database
Posted by Abdul on 2/12/2009 2:51 AM
Gravatar Very Help full thanks for the post
# re: SQL Server – Find Field Value in Database
Posted by Aldo on 4/21/2009 11:29 AM
Gravatar This was very helpfull. Many thanks!
# re: SQL Server – Find Field Value in Database
Posted by LiveMan on 5/7/2009 10:10 AM
Gravatar Very good!!! Many thanks!
# re: SQL Server – Find Field Value in Database
Posted by mary on 5/30/2009 6:34 PM
Gravatar Thanks for the post.
# re: SQL Server – Find Field Value in Database
Posted by M. Kendall McIntosh on 6/16/2009 1:16 PM
Gravatar I think David H. Yardy wrote this code, and is my hero as a result!! :-)

I am just curious if any DB guru's out there, or David, has tested the effect of running this code on a server where server load is a constant concern. I am learning SQL and I have receintly read that use of "cursor" tables is fairly server intensive.

Great solution... just curious about the server load.
# re: SQL Server – Find Field Value in Database
Posted by dyardy on 6/19/2009 7:43 PM
Gravatar I agree that cursors are not the best performers however I primarily use this code for testing and redesign type efforts. One or two runs will have minimal or no impact on the overall performance of the server.
# re: SQL Server – Find Field Value in Database
Posted by Magnus on 6/24/2009 10:39 AM
Gravatar A really useful script.
I have a question:
I have put this script as a Stored Procedure (namned "SearchAll")in the master DB and when I run it it only searches in the master DB even if I have chosen another DB.
E.g:
"use Products
master.dbo.SearchAll 'Skates'
"
What should I do to make the SP search the DB I'm currently at?
# 
Posted by Gokul's Blog! on 8/12/2009 10:39 AM
Gravatar Find Value in a database column
# re: SQL Server – Find Field Value in Database
Posted by Mike Kaplan on 8/31/2009 6:54 PM
Gravatar thanks very helpfull ..
I made 2 small changes
1) added a way to limit the tables to a list
2) added a few print statements so i can watch the progress.

DECLARE @value VARCHAR(64)
DECLARE @sql VARCHAR(1024)
DECLARE @table VARCHAR(64)
DECLARE @column VARCHAR(64)
DECLARE @TABLE_LIST VARCHAR(200)

SET @value = '9176'
SET @TABLE_LIST = '''CLAIM'', ''CLAIM_SUPP'''
-- '''OR'', ''CA'', ''WA'''
print @value
PRINT @TABLE_LIST

CREATE TABLE #t (
tablename VARCHAR(64),
columnname VARCHAR(64)
)

set @sql = 'DECLARE TABLES CURSOR '
set @sql = @sql + ' FOR '
set @sql = @sql + ' SELECT o.name, c.name FROM syscolumns c INNER JOIN sysobjects o ON c.id = o.id WHERE o.type = ''U'' AND c.xtype IN (167, 175, 231, 239) '
set @sql = @sql + ' and o.name IN ( ' + @TABLE_LIST + ' )'
set @sql = @sql + ' ORDER BY o.name, c.name '
-- PRINT @sql
execute (@sql)
OPEN TABLES

FETCH NEXT FROM TABLES
INTO @table, @column

WHILE @@FETCH_STATUS = 0
BEGIN
print @table + ' - ' +@column
SET @sql = 'IF EXISTS(SELECT NULL FROM [' + @table + '] '
--SET @sql = @sql + 'WHERE RTRIM(LTRIM([' + @column + '])) = ''' + @value + ''') '
SET @sql = @sql + 'WHERE RTRIM(LTRIM([' + @column + '])) LIKE ''%' + @value + '%'') '
SET @sql = @sql + 'INSERT INTO #t VALUES (''' + @table + ''', '''
SET @sql = @sql + @column + ''')'

EXEC(@sql)

FETCH NEXT FROM TABLES
INTO @table, @column
END

CLOSE TABLES
DEALLOCATE TABLES

SELECT *
FROM #t

DROP TABLE #t
# re: SQL Server – Find Field Value in Database
Posted by Aleksandr on 11/4/2009 9:38 AM
Gravatar a BIG thanks
# re: SQL Server – Find Field Value in Database
Posted by Ajay Khatri on 1/6/2010 1:48 AM
Gravatar Great Scripts .... Many Thanks /....
Post Comment
Title *
Name *
Email
Url
Comment *
Please add 3 and 5 and type the answer here:

About Me

An engineer by training and a software developer at heart. My techniques and approaches meld engineering approaches with software technology.

Core to these principles is a systematic approach to the development of software with a strong lifecycle and process management emphasis through adoption of mature technologies.

Ten years designing heavy structural steel and concrete structures and 12 years in the software development profession have embedded strong project management and business knowledge in my approaches.

Subscribe to Rss Feed


Follow me on twitter @dyardy