Does anyone have a good method for searching an entire database for a given value?
I have a specific string I’m looking for, it’s in TableA, and it’s also a FK to some other table, TableB, except I don’t know which table/column that is.
Assuming there’s a jillion tables and I don’t want to look through them all, and maybe will have to do this in several different cases, what would be the best way?
Since I didn’t want a Code-SQL bridge, my only all-SQL idea was:
select tablename and column_name from INFORMATION_SCHEMA.COLUMNS
…then use a cursor to flip through all the columns, and for all the datatypes of nvarchar I would execute dynamic SQL like:
SELECT * from @table where @column = @myvalue
Needless to say, this is slow AND a memory hog.
Anyone got any ideas?
Here are a couple of links talking about how to do this:
Both of them use the approach you were hoping to avoid. Refine them so that they only searched columns that were foreign keys should improve their performance by eliminating the searching of unnecessary tables.