DEV Community

Query Filter
Query Filter

Posted on

dev14

/* -------------------------------------------------------------
   ONE BLOCK → ALL tables with INTERNTIMESTAMP column
   Works on ANY old Sybase ASE 12.5 – 16.0
   ------------------------------------------------------------- */

declare @sql nvarchar(4000)     -- 4000 is enough for 100+ tables
select @sql = ''

/* Build the big UNION ALL query – simple and rock-solid */
select @sql = @sql +
       'select ''' + o.name + ''' as table_name, count(*) as rows_last_45_days from ' + 
       db_name() + '..'+ o.name +                  -- fully qualified!
       ' where INTERNTIMESTAMP >= dateadd(day, -10045, getdate()) union all '
from sysobjects o
where o.type = 'U'
  and exists (select 1 
              from syscolumns c 
              where c.id = o.id 
                and c.name = 'INTERNTIMESTAMP')     -- this is the correct check
order by o.name

/* Safely strip the trailing ' union all ' */
if len(@sql) > 11
   select @sql = substring(@sql, 1, len(@sql)-11)
else
   select @sql = 'select ''No tables with column INTERNTIMESTAMP'' as table_name, 0 as rows_last_45_days'

/* Show what we built (optional – you can comment this out) */
-- print @sql

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

Top comments (0)