DEV Community

Query Filter
Query Filter

Posted on

cursor

/* -------------------------------------------------------------
   ONE BLOCK → prints ALL 56 tables with correct counts
   Works 100 % on ALL old Sybase ASE versions
   ------------------------------------------------------------- */

set nocount on
go

print 'TABLE_NAME                           ROWS_LAST_10045_DAYS'
print '-----------------------------------------------------------'

declare @table_name varchar(255)

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
    declare @cnt int
    declare @sql varchar(1000)

    select @sql = 'select count(*) from ' + @table_name + 
                  ' where INTERNTIMESTAMP >= dateadd(day,-10045,getdate())'

    execute (@sql) into @cnt

    print rtrim(@table_name) + replicate(' ', 35-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)