search all tables for a string

By slightlybehindthecurve

I needed to search the ‘description’ field of every table in my database for a string.
To do that, I generated a column of search commands.
Then I copied those search commands into a new query window in sql server enterprise manager and executed them. I could see which tables contained the string I was looking for.

Here is the sql to generate the column of commands. A trick is to concatenate the literal single quote string.

**********************************************
SEARCH all tables , ALL COLUMNS NAMED ‘DESCRIPTION’ FOR RECORDS CONTAINING A KEY WORD
*******************

select (‘Select ‘ + char(39) + table_name + char(39) + ‘ as TableName ,* from ‘ + table_name + ‘ where [description] like ‘ + char(39) + ‘%Back Flow%’ + char(39) + ‘ ‘ ) as copyandexecute , column_name from INFORMATION_SCHEMA.columns
where column_name = ‘Description’
order by column_name

Leave a Reply