use mytest drop proc [dbo].[end_waitstats] go CREATE proc [dbo].[end_waitstats] as set nocount on if not exists (select 1 from sys.objects where object_id = object_id ( N'[dbo].[my_waitstats]') and OBJECTPROPERTY(object_id, N'IsUserTable') = 1) begin raiserror ('end_waitstats without begin..',16,1) with nowait end declare @i int, @myspid smallint, @now datetime begin select @now = getdate() select @myspid = @@SPID select s.[wait_type], s.[waiting_tasks_count]-m.[waiting_tasks_count] waits, s.[wait_time_ms]-m.[wait_time_ms] wait_time, s.[signal_wait_time_ms]-m.[signal_wait_time_ms] signal_wait_time, @now-m.now elapsed_time from sys.dm_os_wait_stats s, [dbo].[my_waitstats] m where s.[wait_type]=m.[wait_type] and s.[wait_time_ms]-m.[wait_time_ms] > 0 and s.[wait_type] not in ( 'CLR_SEMAPHORE', 'LAZYWRITER_SLEEP', 'RESOURCE_QUEUE', 'SLEEP_TASK', 'SLEEP_SYSTEMTASK', 'SQLTRACE_BUFFER_FLUSH', 'WAITFOR') select s.session_id, s.cpu_time-m.cpu_time cpu_time, s.total_scheduled_time-m.total_scheduled_time tot_sched_time, s.total_elapsed_time-m.total_elapsed_time elapsed_time, s.reads-m.reads PIO, s.writes-m.writes writes , s.logical_reads-s.logical_reads LIO from sys.dm_exec_sessions s, [dbo].[my_otherstats] m where s.session_id=@myspid end