Find every occurrence of a value in a database | Database Journal

Find every occurrence of a value in a database

Aug 10, 2009
1 minute read

>>Script Language and Platform: SQL Server
Script will iterate every field within a database for every occurrence of a specified value.
Takes a single string input and does a LIKE search on every field in the database.
Outputs a data set for each table and column combination where the specified field value is found. A row of data is reported for each field value returned. The column header of the returned data set is formatted to report the table name separated with an underscore from the column name.

The script uses a cursor to iterate the tables schema. It then uses a nested cursor to iterate the columns schema. It avoids data types that may throw an error.

Have tested it on a database with 8835 columns, 119560 rows, 2648822 fields of genuine data. Have a 2.13Gb dual core and it took 32secs.

Author: Phil Styler


–Find every occurence of a specified field value within a database by
—  iterating every table and column.
–NB: the data set header returned, is formatted to report the
— table name separated by an underscore from the column name.
–Written by Phil Styler (from New Zealand), Sept 2008.
DECLARE @TableName nvarchar(250)
DECLARE @ColumnName nvarchar(250)
DECLARE @ColumnType nvarchar(250)
DECLARE @TextToFind nvarchar(250)
DECLARE @ReturnValue nvarchar(max)
SET @TextToFind = ‘Test’  –Set the text to search foropen the tables cursor
DECLARE TableCursor CURSOR FOR SELECT [NAME] FROM sys.tables ORDER BY [NAME]
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableNameset the table to check
— Loop through table cursor
WHILE @@FETCH_STATUS = 0
–keep iterating the tables until there are none left
BEGIN
    DECLARE ColumnCursor CURSOR FOR SELECT column_name
        FROM information_schema.columns WHERE table_name = @TableNameopen the columns cursor
    OPEN ColumnCursor
    FETCH NEXT FROM ColumnCursor INTO @ColumnName  
    — Loop through the columns cursor
    WHILE @@FETCH_STATUS = 0 –keep iterating the columns until there are none left
    BEGIN
        –initialise the return variable
        SET @ReturnValue = ”
        –get the columns data type
        SET @ColumnType = (SELECT Data_Type FROM information_schema.columns
            WHERE table_name = @TableName AND column_name = @ColumnName)
        –PRINT @TableName + ‘.’ +@ColumnName + ‘ DataType =+ @ColumnTypecheck all columns types except: “image”
        IF @ColumnType != ‘image’
        BEGINsearch the column within the current table for the search text
            SET @ReturnValue = ‘IF EXISTS(SELECT [’ + @ColumnName + ‘] AS+ @TableName + ‘_’ + @ColumnName +FROM [’ + @TableName + ‘]
                            WHERE [’ + @ColumnName + ‘] LIKE%+ @TextToFind +%”)
                            SELECT [’ + @ColumnName + ‘] AS+ @TableName + ‘_’ + @ColumnName +FROM [’ + @TableName + ‘]
                            WHERE [’ + @ColumnName + ‘] LIKE%+ @TextToFind +%”’
            EXECUTE sp_EXECUTESQL @ReturnValue
            –PRINT @TableName + ‘.’ +@ColumnName
            –PRINT ‘@ReturnValue =+ @ReturnValue
        END
        ELSE
        BEGIN
            –unwanted column type, do not check as will throw an error
            PRINT @TableName + ‘.’ +@ColumnName +not checked because DataType =+ @ColumnType
        END        
        FETCH NEXT FROM ColumnCursor INTO @ColumnNameget the next column
    ENDClose Column cursor
    CLOSE ColumnCursor
    DEALLOCATE ColumnCursor
    FETCH NEXT FROM TableCursor INTO @TableNameget the next table
ENDClose Table cursor
CLOSE TableCursor
DEALLOCATE TableCursor



Disclaimer:
We hope that the information on these script pages is
valuable to you. Your use of the information contained in these pages,
however, is at your sole risk. All information on these pages is provided
“as -is”, without any warranty, whether express or implied, of its accuracy,
completeness, or fitness for a particular purpose…

Disclaimer Continued

Back to Database Journal Home

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.