Monitoring SQL: execution table all sql statements, each statement run record time (3)

Original: Monitoring SQL: execution table all sql statements, each statement run record time (3)

By executing a procedure with parameters stored   exec OpreateTB data ( 'OpreateUser', 'IsRun' ) update the table  

Table Structure: (RunTime, RunStatus, BetweenTime, RunLog, IsRun).

Field Explanation:
1, the execution time RunTime this statement  
2, RunStatus whether the statement is successful indicating success 0 1 NULL indicates abnormality has not been performed is -1 for rollback
3, BetweenTime: the statement is executed by the time
4, RunLog execution returns results message (Affects 1)
. 5, IsRun: 0 indicates whether the statement is not executed, an execution indicates

execution error if statements directly rolled back and Runlog are updated to 'rollback'.
According to the statement execution OrderNumber ascending order.


   
   
  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

Here with a stored procedure to achieve:


   
   
  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


Published 416 original articles · won praise 135 · views 940 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12020037.html