I have several queries to be run on a customer table. Each of these queries identifies possible data quality issues in the customer table. For example, one query is to find if a customer's age is < 18, which cannot / should not be the case.
I have a cursor that fetches each of these queries one by one into a variable.
I am struggling to run each of these queries and aggregate the results.
DECLARE c1 CURSOR FOR SELECT [ruleQuery] FROM [dbo].[SQL_DataQuality_Rules]
OPEN rules_cursor
FETCH NEXT FROM rules_cursor INTO @rulequery
WHILE @@FETCH_STATUS = 0
BEGIN
--Help needed here to execute @rulequery which would be something like
--SELECT sum(case when age < 18 then 1 else 0 end) as 'Fail' FROM...
FETCH NEXT FROM rules_cursor INTO @rulequery
END
CLOSE c1
DEALLOCATE c1
Since I would be calling this procedure from excel, I would like to have all the fail counts in one table in excel.