创建12

发布: (2025年12月4日 GMT+8 05:48)
2 min read
原文: Dev.to

Source: Dev.to

单次执行脚本

以下 T‑SQL 脚本可在 DBVisualizer、isql 或任何其他工具中运行。
它返回最近 45 天内每个包含 INTERNTIMESTAMP 列的用户表中新增的行数。

脚本

/* -------------------------------------------------------------
   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

相关文章

阅读更多 »

创建11

SQL 声明 @sql 为 varchar8000;将 @sql 设为空字符串;将 @sql 追加为 `SELECT ''+name+'' , count FROM '+name+ ' WHERE INTERNTIMESTAMP >= DATEADD(dd,-45,GETDATE) UNION …`

比较5

SQL Script sql -- 声明用于模式 UID 的变量,仅运行一次 DECLARE @uid1 int, @uid2 int SELECT @uid1 = uid FROM sysusers WHERE name = 'GLOBAL_COMET_US_1' SEL...

比较4

sql -- 首先获取用户ID(可选),以提高可读性 DECLARE @uid1 int, @uid2 int SELECT @uid1 = uid FROM sysusers WHERE name = 'GLOBAL_COMET_US_1' SELE...