All of my tables in the DIM
schema share a bit column called USER_CHECKED
. With an agent job, I want to output (I don't particularly care where/what to) the name of each table in the schema and the number of times USER_CHECKED
has the value 0
. Before I can write this agent job, I need to write the underlying query. The natural solution is to use sp_MSforeachtable
, but that's unsupported. Instead, I've done something like this
USE [MY-DB]
SELECT
TABLE_CATALOGUE,
CONCAT('SELECT COUNT(CASE WHEN [USER_CHECKED] = 0 THEN 1 END) FROM MY-DB.DIM.', 'TABLE_CATALOGUE') AS TO_RUN
FROM
information_schema.tables
WHERE
TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA = 'DIM'
As a result, the TO_RUN
column contains exactly what query I want to run. However, I've got no idea how to run it. In principle, I could use a cursor, but they're usually a terrible idea. Is there an alternative way to "execute" this column?