DEV Community

Query Filter
Query Filter

Posted on

create13

/* -------------------------------------------------------------
   ONE SINGLE BLOCK – paste & execute → 57 rows instantly
   Works even if your column is named anything in syscolumns
   ------------------------------------------------------------- */

declare @sql nvarchar(8000)
select @sql = ''

/* Build the UNION ALL query – we look for tables that have a column 
   named exactly INTERNTIMESTAMP in the application schema */
select @sql = @sql +
       'select ''' + o.name + ''' as table_name, count(*) as rows_last_45_days from ' + o.name +
       ' where exists (select 1 from syscolumns c where c.id = object_id(''' + o.name + ''') and c.name = ''INTERNTIMESTAMP'')' +
       '   and INTERNTIMESTAMP >= dateadd(day, -45, getdate()) union all '
from sysobjects o
where o.type = 'U'
  and object_id(o.name) in (
      select id 
      from syscolumns 
      where name = 'INTERNTIMESTAMP'     -- this is the real application column name
  )
order by o.name

/* Remove trailing ' union all ' safely */
if len(@sql) > 11
   select @sql = left(@sql, len(@sql) - 11)
else
   select @sql = 'select ''No tables with INTERNTIMESTAMP found'' as table_name, 0 as rows_last_45_days'

/* Run it */
exec(@sql)
go
Enter fullscreen mode Exit fullscreen mode

Top comments (0)