Here is a script that I hope solve your needs. Beware if you are databases are of some size, it take some time for it to complete. You get one result set for every table where is at least a matching row.
Maybe I should try to explain the script, but I let if suffice with that this is a quite an advanced exercise in dynamic SQL, and it took me quite a while to get it right.
I like to call out this line in @dbquery:
-- AND c.column_encryption_key_id IS NULL
If you have columns encrypted with Always Encrypted, you need to uncomment this line. I left it commented as I found less likely that you have such columns, and the line would not work on SQL 2014 and earlier.
DECLARE @db sysname,
@sp_executesql nvarchar(500),
@query nvarchar(MAX),
@dbstmts nvarchar(MAX),
@value nvarchar(50) = 'Reggio nell''Emilia'
SELECT @query =
'DECLARE @sq char(1) = char(39)
SELECT @dbstmts =
(SELECT ''IF EXISTS (SELECT *
FROM '' + quotename(s.name) + ''.'' + quotename(t.name) + ''
WHERE '' + quotename(c.name) + '' = @value)
SELECT db_name() AS DatabaseName, '' +
quotename(s.name, @sq) + '' AS SchemaName, '' +
quotename(t.name, @sq) + '' AS TableName, '' +
quotename(c.name, @sq) + '' AS ColumnName, *
FROM '' + quotename(s.name) + ''.'' + quotename(t.name) + ''
WHERE '' + quotename(c.name) + '' = @value'' +
char(13) + char(10)
FROM sys.tables t
JOIN sys.schemas s ON s.schema_id = t.schema_id
JOIN sys.columns c ON c.object_id = t.object_id
WHERE type_name(c.system_type_id) LIKE ''%char%''
AND (c.max_length = -1 OR c.max_length >= len(@value))
-- AND c.column_encryption_key_id IS NULL
FOR XML PATH(''''), TYPE).value(''.'', ''nvarchar(MAX)'')'
DECLARE dbcur CURSOR STATIC LOCAL FOR
SELECT quotename(name) FROM sys.databases
WHERE database_id > 4
AND state = 0 -- Only online databases
AND is_read_only = 0
ORDER BY name
OPEN dbcur
WHILE 1 = 1
BEGIN
FETCH dbcur INTO @db
IF @@fetch_status <> 0
BREAK
SELECT @sp_executesql = @db + '.sys.sp_executesql'
EXEC @sp_executesql @query, N'@dbstmts nvarchar(MAX) OUTPUT, @value nvarchar(50)',
@dbstmts OUTPUT, @value
PRINT @db
IF @dbstmts IS NOT NULL
BEGIN
--PRINT @dbstmts
EXEC @sp_executesql @dbstmts, N'@value nvarchar(50)', @value
END
END
DEALLOCATE dbcur