监控SQL:执行表中所有sql语句、记录每个语句运行时间(3)

时间:2021-04-19 07:40:03

原文:监控SQL:执行表中所有sql语句、记录每个语句运行时间(3)


通过执行一个 带参数的存储过程  exec  OpreateTB('OpreateUser','IsRun')  更新表的数据

表结构:(RunTime,RunStatus,BetweenTime,RunLog,IsRun)。

字段解释:

1、RunTime 这条语句的执行时间  

2、RunStatus 为执行语句是否成功  1表示执行成功   0 表示异常  NULL 就是还未执行 -1表示回滚

3、BetweenTime :执行这条语句所用时间

4、RunLog 执行 返回的消息结果  如(1行影响)

5、IsRun :是否执行语句 0 表示 未执行,1表示已执行

执行过程如果有错误语句直接回滚 并且 Runlog 都更新为'回滚'。

执行 按照 OrderNumber 升序执行语句。


  1. --delete from tb_CMd
  2. CREATE TABLE [dbo].[TB_CMD](
  3. [RowGuid] [nvarchar](50) NOT NULL,
  4. [RunTime] [date] NULL,
  5. [RunStatus] [int] NULL,
  6. [SqlEvent] [nvarchar](max) NULL,
  7. [OrderNumber] [int] NULL,
  8. [IsRun] [bit] NULL,
  9. [OpreateUser] [nchar](10) NULL,
  10. [SqlType] [nchar](10) NULL,
  11. [BetweenTime] [int] NULL,
  12. [RunLog] [nvarchar](200) NULL,
  13. CONSTRAINT [PK_TB_CMD] PRIMARY KEY CLUSTERED
  14. (
  15. [RowGuid] ASC
  16. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  17. ) ON [PRIMARY]
  18. GO
  19. ALTER TABLE [dbo].[TB_CMD] ADD CONSTRAINT [DF_TB_CMD_RowGuid] DEFAULT (newid()) FOR [RowGuid]
  20. GO
  21. insert into tb_CMd (sqlevent,OrderNumber,IsRun,OpreateUser )values('create table tb (id int ,name varchar(10))',1,0,'Tom')
  22. insert into tb_CMd (sqlevent,OrderNumber,IsRun,OpreateUser )values('insert into tb select 1,''test1''',2,0,'Tom')
  23. insert into tb_CMd (sqlevent,OrderNumber,IsRun,OpreateUser )values('insert into tb select 1,''test1''',3,0,'Jack')
  24. insert into tb_CMd (sqlevent,OrderNumber,IsRun,OpreateUser )values('insert into tb select 2,''test2''',4,0,'Tom')
  25. insert into tb_CMd (sqlevent,OrderNumber,IsRun,OpreateUser )values('update tb set name =''test_1'' where id =1''',5,0,'Tom')
  26. insert into tb_CMd (sqlevent,OrderNumber,IsRun,OpreateUser )values('delete from tb where id=1',6,0,'Tom')
  27. insert into tb_CMd (sqlevent,OrderNumber,IsRun,OpreateUser )values('select * from#tb',7,0,'Tom')
  28. insert into tb_CMd (sqlevent ,OrderNumber,IsRun,OpreateUser )values('drop table tb',8,1,'Tom')
  29. go
  30. select * from tb_cmd order by OrderNumber asc

这里用一个存储过程来实现:


  1. create proc dbo.OpreateTB
  2. --@OpreateUser nvarchar(100),
  3. --@IsRun nvarchar(10)
  4. as
  5. declare @i int
  6. declare @start datetime
  7. declare @sql nvarchar(max)
  8. declare @OrderNumber int
  9. declare @error int
  10. declare @ROWCOUNT int
  11. set @i = 1;
  12. while @i <= (select COUNT(*) from [TB_CMD])
  13. begin
  14. --按照[OrderNumber]进行了排序,每次取出1条
  15. ;with t
  16. as
  17. (
  18. select *,
  19. ROW_NUMBER() over(order by [OrderNumber]) rownum
  20. from [TB_CMD]
  21. )
  22. select @sql = [SqlEvent],
  23. @OrderNumber = [OrderNumber]
  24. from t
  25. where rownum = @i
  26. set @start = GETDATE()
  27. exec(@sql);
  28. select @error = @@ERROR,
  29. @ROWCOUNT = @@ROWCOUNT
  30. update [TB_CMD]
  31. set BetweenTime = datediff(ms,@start,GETDATE()),
  32. RunLog = case when @error = 0 then '('+cast(@ROWCOUNT as varchar)+'行影响)'
  33. else '回滚'
  34. end,
  35. RunStatus = case when @error = 0 then 1
  36. when @error <> 1 then 0
  37. end,
  38. IsRun = 1
  39. where [OrderNumber] = @OrderNumber --这里也修改了
  40. set @i = @i + 1
  41. end
  42. go
监控SQL:执行表中所有sql语句、记录每个语句运行时间(3)
监控SQL:执行表中所有sql语句、记录每个语句运行时间(3)
发布了416 篇原创文章 · 获赞 135 · 访问量 94万+