use ##database
go
drop procedure archivemonitor
go
drop procedure updt_umonitor
go
drop table     archivedetail
go
drop table     archivesummary
go
drop table     umonhistory
go
drop table     umonhistsummary
go

create table archivesummary
   (end_time      smalldatetime     not null,
    start_time    smalldatetime     not null,
    cpu_min       int               not null,
    cpu_max       int               not null,
    total_read    int               not null,
    total_write   int               not null,
    log_write     int               not null,
    xacts         int               not null,
    ddlks         int               not null,
    dcache        double precision  not null,
    pcache        double precision  not null,
    auxi1         int               not null,
    auxi2         int               not null,
    auxdp1        double precision  not null,
    auxdp2        double precision  not null,

   primary key clustered  (end_time))
go
sp_setpglockpromote 'table', archivesummary, 2, 2, 1
go

create table archivedetail
   (end_time      smalldatetime     not null,
    group_name    varchar (25)      not null,
    field_name    varchar (35)      not null,
    field_id      smallint          not null,
    value         int               not null,

   foreign key (end_time)  references archivesummary (end_time))
go
create clustered index C on archivedetail (end_time, group_name, field_name, field_id)
go
sp_setpglockpromote 'table', archivedetail, 2, 2, 1
go

create table umonhistsummary
   (seq           tinyint           not null,
    start_time    datetime          not null,
    end_time      datetime          not null,
    cpu_min       int               not null,
    cpu_max       int               not null,
    total_read    int               not null,
    total_write   int               not null,
    log_write     int               not null,
    xacts         int               not null,
    ddlks         int               not null,
    dcache        double precision  not null,
    pcache        double precision  not null,
    auxi1         int               not null,
    auxi2         int               not null,
    auxdp1        double precision  not null,
    auxdp2        double precision  not null,

   primary key clustered  (seq))
go
sp_setpglockpromote 'table', umonhistsummary, 2, 2, 1
go

create table umonhistory
   (seq           tinyint           not null,
    group_name    varchar (25)      not null,
    field_name    varchar (35)      not null,
    field_id      smallint          not null,
    value         int               not null,

   foreign key (seq)  references umonhistsummary (seq))
go
sp_setpglockpromote 'table', umonhistory, 2, 2, 1
go

insert into umonhistsummary values ( 1, getdate (), getdate (),  1,  1,  11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values ( 2, getdate (), getdate (),  2,  2,  22, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values ( 3, getdate (), getdate (),  3,  3,  13, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values ( 4, getdate (), getdate (),  4,  4,  14, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values ( 5, getdate (), getdate (),  5,  5,  15, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values ( 6, getdate (), getdate (),  6,  6,  16, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values ( 7, getdate (), getdate (),  7,  7,  17, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values ( 8, getdate (), getdate (),  8,  8,  18, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values ( 9, getdate (), getdate (),  9,  9,  19, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (10, getdate (), getdate (), 10, 10, 110, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (11, getdate (), getdate (), 11, 11, 111, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (12, getdate (), getdate (), 12, 12, 112, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (13, getdate (), getdate (), 13, 13, 113, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (14, getdate (), getdate (), 14, 14, 114, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (15, getdate (), getdate (), 15, 15, 115, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (16, getdate (), getdate (), 16, 16, 116, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (17, getdate (), getdate (), 17, 17, 117, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (18, getdate (), getdate (), 18, 18, 118, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (19, getdate (), getdate (), 19, 19, 119, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (20, getdate (), getdate (), 20, 20, 120, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (21, getdate (), getdate (), 21, 21, 121, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (22, getdate (), getdate (), 22, 22, 122, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (23, getdate (), getdate (), 23, 23, 123, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (24, getdate (), getdate (), 24, 24, 124, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (25, getdate (), getdate (), 25, 25, 125, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (26, getdate (), getdate (), 26, 26, 126, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (27, getdate (), getdate (), 27, 27, 127, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (28, getdate (), getdate (), 28, 28, 128, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (29, getdate (), getdate (), 29, 29, 129, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (30, getdate (), getdate (), 30, 30, 130, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (31, getdate (), getdate (), 31, 31, 131, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (32, getdate (), getdate (), 32, 32, 132, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (33, getdate (), getdate (), 33, 33, 133, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (34, getdate (), getdate (), 34, 34, 134, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (35, getdate (), getdate (), 35, 35, 135, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (36, getdate (), getdate (), 36, 36, 136, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (37, getdate (), getdate (), 37, 37, 137, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (38, getdate (), getdate (), 38, 38, 138, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (39, getdate (), getdate (), 39, 39, 139, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (40, getdate (), getdate (), 40, 40, 140, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (41, getdate (), getdate (), 41, 41, 141, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (42, getdate (), getdate (), 42, 42, 142, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (43, getdate (), getdate (), 43, 43, 143, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (44, getdate (), getdate (), 44, 44, 144, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (45, getdate (), getdate (), 45, 45, 145, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (46, getdate (), getdate (), 46, 46, 146, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (47, getdate (), getdate (), 47, 47, 147, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (48, getdate (), getdate (), 48, 48, 148, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (49, getdate (), getdate (), 49, 49, 149, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (50, getdate (), getdate (), 50, 50, 150, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (51, getdate (), getdate (), 51, 51, 151, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (52, getdate (), getdate (), 52, 52, 152, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (53, getdate (), getdate (), 53, 53, 153, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (54, getdate (), getdate (), 54, 54, 154, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (55, getdate (), getdate (), 55, 55, 155, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (56, getdate (), getdate (), 56, 56, 156, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (57, getdate (), getdate (), 57, 57, 157, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (58, getdate (), getdate (), 58, 58, 158, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (59, getdate (), getdate (), 59, 59, 159, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
insert into umonhistsummary values (60, getdate (), getdate (), 60, 60, 160, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
go
create clustered index  C on umonhistory (seq, group_name, field_name, field_id)
go

create procedure archivemonitor (@SEQ1  tinyint,
                                 @SEQ2  tinyint = NULL) as
   set ansinull                      on
   set flushmessage                  on
   set quoted_identifier             on
   set string_rtruncation            on
   set transaction isolation level   3

   declare @detailprnt  varchar (9),
           @detailrows  int

   print ' '

   if (@@trancount = 0)
      begin
         set chained on
      end
   else
      begin
         print '    <archivemonitor> CANNOT BE RUN FROM WITHIN A TRANSACTION.'

         print ' '

         return 1
      end

   if ((@SEQ1 = @SEQ2)  or  (@SEQ2 IS NULL))
      begin
         if (exists (select asm.end_time
                     from archivesummary asm,
                          umonhistsummary uhs
                     where uhs.seq      = @SEQ1
                       and asm.end_time = convert (smalldatetime, uhs.end_time)))
            begin
               print '    SEQUENCE <%1!> IS ALRADY IN THE ARCHIVAL DATABASE.',
                     @SEQ1

               print ' '
            end
         else
            begin
               insert into archivesummary
                  select end_time, start_time, cpu_min, cpu_max, total_read,
                         total_write, log_write, xacts, ddlks, dcache, pcache,
                         auxi1, auxi2, auxdp1, auxdp2
                  from umonhistsummary
                  where seq = @SEQ1

               insert into archivedetail
                  select uhs.end_time, uh.group_name, uh.field_name, uh.field_id,
                         uh.value
                  from umonhistsummary uhs,
                       umonhistory     uh
                  where uhs.seq = @SEQ1
                    and uh.seq  = @SEQ1

               select @detailrows = @@rowcount
            end
      end
   else if (@SEQ1 < @SEQ2)
      begin
         if (exists (select asm.end_time
                     from archivesummary asm,
                          umonhistsummary uhs
                     where uhs.seq      >= @SEQ1
                       and uhs.seq      <= @SEQ2
                       and asm.end_time  = convert (smalldatetime, uhs.end_time)))
            begin
               print '    THERE ARE SEQUENCES ALREADY ARCHIVED BETWEEN <%1!> AND <%2!>.',
                     @SEQ1, @SEQ2

               print ' '

               select SEQ      = uhs.seq,
                      END_TIME = asm.end_time
               from archivesummary asm,
                    umonhistsummary uhs
               where uhs.seq      >= @SEQ1
                 and uhs.seq      <= @SEQ2
                 and asm.end_time  = convert (smalldatetime, uhs.end_time)
               order by asm.end_time
            end
         else
            begin
               insert into archivesummary
                  select end_time, start_time, cpu_min, cpu_max, total_read,
                         total_write, log_write, xacts, ddlks, dcache, pcache,
                         auxi1, auxi2, auxdp1, auxdp2
                  from umonhistsummary
                  where seq >= @SEQ1
                    and seq <= @SEQ2

               insert into archivedetail
                  select uhs.end_time, uh.group_name, uh.field_name, uh.field_id,
                         uh.value
                  from umonhistsummary uhs,
                       umonhistory     uh
                  where uhs.seq >= @SEQ1
                    and uhs.seq <= @SEQ2
                    and uh.seq   = uhs.seq

               select @detailrows = @@rowcount
            end
      end
   else
      begin
         if (exists (select asm.end_time
                     from archivesummary asm,
                          umonhistsummary uhs
                     where ((uhs.seq >= @SEQ1)  or  (uhs.seq <= @SEQ2))
                       and asm.end_time = convert (smalldatetime, uhs.end_time)))
            begin
               print '    THERE ARE SEQUENCES ALREADY ARCHIVED BETWEEN <%1!> AND <%2!>.',
                     @SEQ1, @SEQ2

               print ' '

               select SEQ      = uhs.seq,
                      END_TIME = asm.end_time
               from archivesummary asm,
                    umonhistsummary uhs
               where ((uhs.seq >= @SEQ1)  or  (uhs.seq <= @SEQ2))
                 and asm.end_time = convert (smalldatetime, uhs.end_time)
               order by asm.end_time
            end
         else
            begin
               insert into archivesummary
                  select end_time, start_time, cpu_min, cpu_max, total_read,
                         total_write, log_write, xacts, ddlks, dcache, pcache,
                         auxi1, auxi2, auxdp1, auxdp2
                  from umonhistsummary
                  where seq >= @SEQ1
                     or seq <= @SEQ2

               insert into archivedetail
                  select uhs.end_time, uh.group_name, uh.field_name, uh.field_id,
                         uh.value
                  from umonhistsummary uhs,
                       umonhistory     uh
                  where ((uhs.seq >= @SEQ1)  or  (uhs.seq <= @SEQ2))
                    and uh.seq  = uhs.seq

               select @detailrows = @@rowcount
            end
      end

   commit work

   if (@detailrows IS NOT NULL)
      begin
         select @detailprnt = ltrim (substring (convert (char (12), convert (money, @detailrows), 1), 1, 9))

         print '    %1! DETAIL ROWS WERE ARCHIVED.', @detailprnt

         print ' '

      end

   commit work
go
sp_procxmode archivemonitor, anymode
go

create procedure updt_umonitor as
   set arithabort arith_overflow  off
   set flushmessage               on

   declare @dp1     double precision,
           @dp2     double precision,
           @int1    int,
           @int2    int,
           @maxseq  smallint,
           @seq     smallint

   print ' '

   if (proc_role ('sa_role') <> 1)
     begin
        print 'You must have System Administrator (SA) role to execute this stored procedure.'

        return 1
     end

   if (@@trancount > 0)
      begin
         print 'Updt_umonitor cannot be run from within a transaction.'

         return 1
      end
   else
      begin
         set chained off
      end

   set transaction isolation level 1

   --
   --  TABLE HAS BEEN RE-INITIALIZED OR UPDT_MONITOR TERMINATED?
   --

   select @seq = min (seq)
   from ##database.dbo.umonhistsummary ulast
   where start_time = end_time

   --
   --  OTHERWISE FIND THE OLDEST SEQUENCE VALUE.  HANDLE THE CYCLICAL
   --  LAST-FIRST PAIRING VIA ISNULL.
   --

   if (@seq IS NULL)
      begin
         select @seq = isnull (max (unew.seq), 1)
         from ##database.dbo.umonhistsummary ulast,
              ##database.dbo.umonhistsummary unew
         where unew.seq        = ulast.seq + 1
           and unew.start_time < ulast.end_time
      end

   update ##database.dbo.umonhistsummary
   set start_time   = getdate (),
       end_time     = getdate (),
       cpu_min      = -1,
       cpu_max      = -1,
       total_read   = -1,
       total_write  = -1,
       log_write    = -1,
       xacts        = -1,
       ddlks        = -1,
       dcache       = -1,
       pcache       = -1,
       auxdp1       = -1,
       auxdp2       = -1
   where seq = @seq

   print ' '

   dbcc monitor ('clear', 'all', 'on')

   dbcc monitor ('clear', 'spinlock_s', 'on')

   waitfor delay '00:##wait:00'              -- Server performance dependent

   dbcc monitor ('sample', 'spinlock_s', 'off')

   dbcc monitor ('sample', 'all', 'off')

   dbcc monitor ('select', 'spinlock_s', 'on')

   dbcc monitor ('select', 'all', 'on')

   delete from ##database.dbo.umonhistory
   where seq = @seq

   insert into ##database.dbo.umonhistory
      select @seq, group_name, field_name, field_id, value
      from master.dbo.sysmonitors holdlock

   --  UPDATE MIN-MAX CPU COLUMNS

   select @int1 = min ((100 * (c.value - i.value)) / c.value)
   from ##database.dbo.umonhistory c,
        ##database.dbo.umonhistory i
   where c.seq           = @seq
     and c.group_name like 'engine_%'
     and c.field_name    = 'clock_ticks'
     and c.value         > 0
     and i.seq           = @seq
     and i.group_name    = c.group_name
     and i.field_name    = 'idle_ticks'

   select @int2 = max ((100 * (c.value - i.value)) / c.value)
   from ##database.dbo.umonhistory c,
        ##database.dbo.umonhistory i
   where c.seq           = @seq
     and c.group_name like 'engine_%'
     and c.field_name    = 'clock_ticks'
     and c.value         > 0
     and i.seq           = @seq
     and i.group_name    = c.group_name
     and i.field_name    = 'idle_ticks'

   update ##database.dbo.umonhistsummary
   set end_time = getdate (),
       cpu_min  = @int1,
       cpu_max  = @int2
   where seq = @seq

   --  UPDATE READ/WRITE COUNT COLUMNS

   update ##database.dbo.umonhistsummary
   set total_read = (select sum (value)
                     from ##database.dbo.umonhistory
                     where seq           = @seq
                       and group_name like 'disk_%'
                       and field_name    = 'total_reads')
   where seq = @seq

   update ##database.dbo.umonhistsummary
   set total_write = (select sum (value)
                      from ##database.dbo.umonhistory
                      where seq           = @seq
                        and group_name like 'disk_%'
                        and field_name    = 'total_writes')
   where seq = @seq

   update ##database.dbo.umonhistsummary
   set log_write = (select sum (value)
                    from ##database.dbo.umonhistory
                    where seq           = @seq
                      and group_name like 'buffer_%'
                      and field_name    = 'log_page_writes')
   where seq = @seq

   -- UPDATE TRANSACT COUNT

   update ##database.dbo.umonhistsummary
   set xacts = (select value
                from ##database.dbo.umonhistory
                where seq        = @seq
                  and group_name = 'access'
                  and field_name = 'xacts')
   where seq = @seq

   -- UPDATE DEADLOCK COUNT

   update ##database.dbo.umonhistsummary
   set ddlks = (select value
                from ##database.dbo.umonhistory
                where seq        = @seq
                  and group_name = 'lock'
                  and field_name = 'deadlocks')
   where seq = @seq

   -- UPDATE LOCK GRANT%

   select @dp1 = sum (value)
   from ##database.dbo.umonhistory
   where seq           = @seq
     and group_name    = 'lock'
     and field_name like 'granted%'

   if (@dp1 = 0)
      begin
         update ##database.dbo.umonhistsummary
         set auxdp1 = 0
         where seq = @seq
      end
   else
      begin
         select @dp2 = sum (value)
         from ##database.dbo.umonhistory
         where seq           = @seq
           and group_name    = 'lock'
           and field_name like 'waited%'

         update ##database.dbo.umonhistsummary
         set auxdp1 = 100 * (@dp1 / (@dp1 + @dp2))
         where seq = @seq
      end

   -- UPDATE LOCK PROMOTE%

   select @dp1 = value
   from ##database.dbo.umonhistory
   where seq         = @seq
     and group_name  = 'lock'
     and field_name  = 'promotefailed'

   select @dp2 = sum (value)
   from ##database.dbo.umonhistory
   where seq           = @seq
     and group_name    = 'lock'
     and field_name like 'promoted_%_PAGE'

   if (@dp1 + @dp2 = 0)
      begin
         update ##database.dbo.umonhistsummary
         set auxdp2 = 0
         where seq = @seq
      end
   else
      begin
         update ##database.dbo.umonhistsummary
         set auxdp2 = (100 * @dp2) / (@dp1 + @dp2)
         where seq = @seq
      end

   -- UPDATE DATA CACHE%

   select @dp1 = sum (value)
   from ##database.dbo.umonhistory
   where seq           = @seq
     and group_name like 'buffer%'
     and field_name    = 'bufsearch_finds'

   select @dp2 = isnull (sum (value), 0)
    from ##database.dbo.umonhistory
    where seq           = @seq
      and group_name like 'buffer%'
      and field_name    = 'bufsearch_calls'

   if (@dp2 = 0)
      begin
         update ##database.dbo.umonhistsummary
         set dcache = 0
         where seq = @seq
      end
   else
      begin
         update ##database.dbo.umonhistsummary
         set dcache = (100 * @dp1) / @dp2
         where seq = @seq
      end

   -- UPDATE PROCEDURE CACHE%

   select @dp1 = value
   from ##database.dbo.umonhistory
   where seq        = @seq
     and group_name = 'procmgr'
     and field_name = 'procedure_requests'

   if (@dp1 = 0)
      begin
         update ##database.dbo.umonhistsummary
         set pcache = 100
         where seq = @seq
      end
   else
      begin
         select @dp2 = value
         from ##database.dbo.umonhistory
         where seq        = @seq
           and group_name = 'procmgr'
           and field_name = 'procedure_reads'

         update ##database.dbo.umonhistsummary
         set pcache = (100 * (@dp1 - @dp2)) / @dp1
         where seq = @seq
      end

   select @maxseq = max (seq)
   from ##database.dbo.umonhistsummary

   print ' '

   select SEQ            = seq,
          TIME           = convert (char (8), end_time, 8),
          'CPU%'         = convert (varchar (3), cpu_min) + '-' + convert (varchar (3), cpu_max),
          'TOTAL READS'  = total_read,
          'TOTAL WRITES' = total_write,
          'LOG WRITES'   = log_write,
          XACTS          = xacts,
          DDLKS          = convert (tinyint, ddlks)
   from ##database.dbo.umonhistsummary
   where (((@seq > 6)  and  (seq + 6 > @seq)  and  (seq <= @seq))
      or ((@seq <= 6)  and  ((seq <= @seq)  or  (seq + 6 > @maxseq + @seq))))
   order by end_time

   waitfor delay '00:##pause:00'             -- Server performance dependent
go
