use mytest drop proc [dbo].[end_schedstats] go CREATE proc [dbo].[end_schedstats] as set nocount on if not exists (select 1 from sys.objects where object_id = object_id ( N'[dbo].[my_schedstats]') and OBJECTPROPERTY(object_id, N'IsUserTable') = 1) begin raiserror ('end_schedstats without begin..',16,1) with nowait end declare @i int, @myspid smallint, @now datetime begin select @now = getdate() select s.[scheduler_id] sched_id, s.[preemptive_switches_count]-m.[preemptive_switches_count] preemptive_switches , s.[context_switches_count]-m.[context_switches_count] context_switches, s.[idle_switches_count]-m.[idle_switches_count] idle_switches, s.[yield_count]-m.[yield_count] yield_count, s.[current_tasks_count]-m.[current_tasks_count] current_tasks, s.[runnable_tasks_count]-m.[runnable_tasks_count] runnable_tasks, s.[current_workers_count]-m.[current_workers_count] current_workers, s.[active_workers_count]-m.[active_workers_count] active_workers , s.[work_queue_count]-m.[work_queue_count] work_queue, s.[load_factor]-m.[load_factor] load_factor, @now-m.now elapsed_time from sys.dm_os_schedulers s, [dbo].[my_schedstats] m where s.[scheduler_id]=m.[scheduler_id] and s.[scheduler_id] < 255 end