create12

Published: (December 3, 2025 at 04:48 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

One‑single execution script

The following T‑SQL script works in DBVisualizer, isql, or any other tool.
It returns the number of rows added in the last 45 days for every user table that contains an INTERNTIMESTAMP column.

Script

/* -------------------------------------------------------------
   ONE SINGLE EXECUTION – works in DBVisualizer / isql / any tool
   Shows rows from last 45 days for every table that has INTERNTIMESTAMP
   ------------------------------------------------------------- */

declare @sql nvarchar(8000)   -- use nvarchar if your ASE supports it (15.7+)
        -- or varchar(8000) on very old versions

select @sql = ''

/* Build the big UNION ALL query */
select @sql = @sql + 
       'select ''' + o.name + ''' as table_name, count(*) as rows_last_45_days from ' + o.name +
       ' where INTERNTIMESTAMP >= dateadd(day, -45, 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')
order by o.name

/* Safely remove the trailing '' union all '' – no negative length */
if len(@sql) > 11
   select @sql = left(@sql, len(@sql) - 11)   -- removes last 11 chars
else
   select @sql = 'select ''No tables found'' as table_name, 0 as rows_last_45_days'

/* Execute it */
exec(@sql)
go
Back to Blog

Related posts

Read more »

create11

sql declare @sql varchar8000 select @sql = '' select @sql = @sql + 'select ''+name+'' , count from '+name+ ' where INTERNTIMESTAMP >= dateadddd,-45,getdate unio...

compare5

SQL Script sql -- Declare variables for schema UIDs run once DECLARE @uid1 int, @uid2 int SELECT @uid1 = uid FROM sysusers WHERE name = 'GLOBAL_COMET_US_1' SEL...

compare4

sql -- First get the user IDs once optional, for readability DECLARE @uid1 int, @uid2 int SELECT @uid1 = uid FROM sysusers WHERE name = 'GLOBAL_COMET_US_1' SELE...