Home » Script to search for a string in an entire SQL Server database

Script to search for a string in an entire SQL Server database

by Vlad Drumea
4 comments

This post contains a script that I’ve written and used a few times when I’ve needed to search for a string in an entire SQL Server database.

You can find more of my scripts by checking out the SQL Server Scripts category of my blog.

What it does

The script can search for a string in a SQL Server database by doing the following steps:

  1. Uses the System Information Schema views to look for tables containing string (NVARCHAR, VARCHAR, CHAR, NCHAR, TEXT, and NTEXT) columns.
  2. Builds a WHERE clause based on the columns found in step 1 and the parameter values provided.
  3. Queries the matching record counts from tables identified at step 1 with the WHERE clause built during step 2.
  4. Stores info for tables with at least 1 matching record.
  5. Returns a summary and queries the identified tables to retrieve the relevant records.

Side-note: TEXT and NTEXT are deprecated, so you should look into switching to VARCHAR(MAX) and NVARCHAR(MAX).

Parameters

I’m listing here only the parameters that can/should be provided with different values:

  • @SearchString – this is the string that you want to look for in all the tables of your database
  • @UseLike – set this to 1 if you’re not expecting an exact match (Column = @SearchString), 0 for partial match (Column LIKE '%'+@SearchString+'%')
  • @IsUnicode – set it to 1 if the string you’ve provided has Unicode characters and the target columns are N(VAR)CHAR and/or NTEXT, if not and you’re database uses mostly (VAR)CHAR and/or TEXT then set it to 0
  • @CaseSensitive – set it to 1 if you’re using a case-sensitive collation and are uncertain about the case of the string in the columns

Note about potential performance impact

The performance of this script depends on a combination of factors:

  • The size of your database
  • Your table design and indexing strategy
  • The data type of your columns
  • The type of search you opt for

So, on smaller databases this script performs ok in most situations, but the larger the database is the more you’d want to avoid @UseLike = 1, @CaseSensitive = 1, and make sure to match @IsUnicode to the data type used by the majority of your columns.

Obviously, I can’t stop you from running this with @UseLike = 1 on a 2TB database, but just know that you might have to wait a while for it to finish and it will also potentially block other operations in the process.

Script

You can also find this script in my SQL Server Scripts repo.

Example result set

If any matches are found, the script returns multiple result sets.

The first result set is a summary consisting of the following:

  • the table name(s)
  • number of matching records per table
  • search string
  • WHERE clause
  • the query used to get the data from the identified table(s)

The subsequent result set(s) starts with a column containing the table name followed by the rest of the columns in said table.

The following is a demo result set from AdventureWorks2019 using “elizabeth” as the value for @SearchString, and with @IsUnicode, @UseLike, and @CaseSensitive all set to 1.


And that’s pretty much it. I needed to write variations of this a few times along the years and figured I could share it in case someone might need to find a string in a database

You may also like

4 comments

Sab December 14, 2023 - 09:08

Very interesting .Thanks for sharing your tricks 🙂

Reply
Searching for a String in a SQL Server Database – Curated SQL December 15, 2023 - 15:05

[…] Vlad Drumea is looking for a string: […]

Reply
Jerry Greenaway June 16, 2024 - 18:52

I an trying to use your code to search for a hexadecimal character [char(0x1e)] ~ but it does not work.
I know the character exists:
select charindex(char(0x1e), Description)
from from xxx.JobMtl with(nolock)
works for the same database.

Ant ideas ?

Reply
Vlad Drumea June 17, 2024 - 00:30

Hi Jerry,
It doesn’t find it because it’s designed for strings to be passed as input, not hex characters.
I would say first try and pass @SearchString on lines 66 and 72 to the CHAR() function and see if that yields any results.
So line 66 would look like this
N”’%’ + CHAR(@SearchString) + N’%” ‘
And line 72 would look like this
END + N”” +CHAR(@SearchString) + N”’ ‘

If that doesn’t work, you’ll need to make more changes and alter the way the WHERE clause is built so that it uses CHARINDEX instead of =, this will also mean that you’ll have to move the CASE from lines 53-57 so that the column name is taken as the second argument of the CHARINDEX function instead of being in front of the LIKE and = operators.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.