DEV Community

Query Filter
Query Filter

Posted on

cursor2

/* -------------------------------------------------------------
   FINAL WORKING VERSION – ALL 56 tables, no errors, no truncation
   Works on ANY old Sybase ASE 12.5 – 16.0
   ------------------------------------------------------------- */

set nocount on
go

print 'TABLE_NAME                             ROWS_LAST_45_DAYS'
print '-------------------------------------------------------------'
go

/* ---- Everything from here must be in ONE batch ---- */
declare @table_name varchar(255)
declare @sql        varchar(1000)
declare @cnt        int

declare table_cursor cursor for
    select o.name
    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

open table_cursor

fetch next from table_cursor into @table_name

while @@fetch_status = 0
begin
    select @sql = 'select @cnt = count(*) from ' + @table_name + 
                  ' where INTERNTIMESTAMP >= dateadd(day, -45, getdate())'

    execute sp_executesql @sql, N'@cnt int output', @cnt output

    print rtrim(@table_name) + replicate(' ', 38 - len(@table_name)) + convert(varchar(20), @cnt)

    fetch next from table_cursor into @table_name
end

close table_cursor
deallocate table_cursor
go
/* ------------------------------------------------------------- */
Enter fullscreen mode Exit fullscreen mode

Top comments (0)