I have a SQL Server database with dozens of tables that contain HTML content. I need to query these tables to see if they contain a particular email address within their content fields.
I am able to identify the tables and columns that need to be searched with this query:
SELECT
sys.columns.name AS ColumnName,
tables.name AS TableName
FROM
sys.columns JOIN sys.tables ON sys.columns.object_id = tables.object_id
WHERE
sys.columns.name LIKE '%content%'
AND (sys.columns.max_length = -1 OR sys.columns.max_length > 100)
The above query returns 86 tables/columns that should be searched. I am able to manually query those tables to check if the respective columns contain the email address of concern. Here's an example:
SELECT *
FROM TableContainingContent
WHERE SomeContent LIKE '%@mail-domain-of-concern.com%' OR SomeContent LIKE '%mailto:%'
I want to avoid manually making 86 of these queries. My first thought is to pull the list of tables/columns from the first query into a spreadsheet to ease the process of manually forming these queries. Although, it would be great to find a way to dynamically query all of these tables/columns using something like sp_executesql
, as the number of tables/columns changes daily.
Any thoughts on the best way to accomplish this would be greatly appreciated.
UPDATE: This is different from the similar question posted in the comments. I'm not looking to search all tables, only the tables containing columns that match the specified criteria.