DEV Community

Query Filter
Query Filter

Posted on

diag2

/* -------------------------------------------------------------
   ONE BLOCK → ALL 56 tables with correct counts
   Works even on the most restrictive old ASE servers
   ------------------------------------------------------------- */

set nocount on
go

/* Use the safe maximum size – 8000 works everywhere */
declare @sql varchar(8000)
select @sql = ''

/* Build the query – we use CHAR(10) for line breaks so it never truncates */
select @sql = @sql + 
       'select ''' + o.name + ''' as table_name, count(*) from ' + 
       db_name() + '..' + o.name + 
       ' where INTERNTIMESTAMP >= dateadd(day,-10045,getdate())' + char(10) +
       'union all ' + char(10)
from sysobjects o
where o.type = 'U'
  and exists (select 1 from syscolumns c where c.id = o.id and c.name = 'INTERNTIMESTAMP')
order by o.name

/* Remove the final 'union all ' + line breaks */
if len(@sql) > 20
   select @sql = substring(@sql, 1, len(@sql) - 20)   -- removes last 20 chars safely
else
   select @sql = 'select ''No tables found'' as table_name, 0'

/* Optional: see the full generated SQL (uncomment if needed) */
-- print @sql

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

Top comments (0)