I have captured table and column names from INFORMATION_SCHEMA.COLUMNS and .TABLES in a temporary table. I want to search these select columns in their respective tables for a specific int value. I created a cursor to retrieve the tablename and columnname from the temp table and I am attempting to construct a query using these values.
SET @query = ' INSERT INTO #Results ' +
'SELECT ' +'[@cpstable]'+ ',' + '[@column]' + ',count(*)' +
'FROM ' + @cpstable +
'WHERE ' + @column + ' = 684' +
'group by ' + '[@cpstable]' + ',' + '[@column]' +
'having count(*) > 0'
exec(@query)
In the select clause, I just want to see the table name and columname with a count of the number to records with a value of 684. But I want the variables to substitute in on the from and where clause. How can I do this?
↧