use mytest drop proc [dbo].[begin_waitstats] go CREATE proc [dbo].[begin_waitstats] as set nocount on if exists (select 1 from sys.objects where object_id = object_id ( N'[dbo].[my_waitstats]') and OBJECTPROPERTY(object_id, N'IsUserTable') = 1) drop table [dbo].[my_waitstats] drop table [dbo].[my_otherstats] create table [dbo].[my_waitstats] ([wait_type] nvarchar(60) not null, [waiting_tasks_count] bigint not null, [wait_time_ms] bigint not null, [signal_wait_time_ms] bigint not null, now datetime not null default getdate()) create table [dbo].[my_otherstats] (spid smallint not null, cpu_time bigint, total_scheduled_time int, total_elapsed_time int, reads bigint, writes bigint, logical_reads bigint) declare @i int, @myspid smallint, @now datetime begin select @now = getdate() select @myspid = @@SPID insert into [dbo].[my_waitstats] ( [wait_type], [waiting_tasks_count], [wait_time_ms], [signal_wait_time_ms], now) select [wait_type], [waiting_tasks_count], [wait_time_ms], [signal_wait_time_ms], @now from sys.dm_os_wait_stats insert into [dbo].[my_otherstats] select session_id,cpu_time, total_scheduled_time, total_elapsed_time, reads,writes, logical_reads from sys.dm_exec_sessions where session_id=@myspid end