首页 数据库 mysql教程 读取SQL Server日志及代理日志

读取SQL Server日志及代理日志

Jun 07, 2016 pm 05:20 PM
sql server

读取SQL Server日志及代理日志,最近闲的没事,为了以后的工作提高效率,其实是不想让自己的眼睛和手 太累。于是写了如下脚本 来解

最近闲的没事,为了以后的工作提高效率,,其实是不想让自己的眼睛和手 太累。于是写了如下脚本 来解放自己。  

---查看每个磁盘剩余空间大小(M)  
Exec master.dbo.xp_fixeddrives  
--或者  
declare @Fixed_tb table(Drive_NO char(1),Remainder_M bigint) 
INSERT INTO @Fixed_tb exec master.dbo.xp_fixeddrives 
select Drive_NO '驱动盘符',Remainder_M'剩余M',cast(((Remainder_M/1024)+0.001*(Remainder_M%1024))as dec(18,2))'剩余G' from @Fixed_tb 
GO  
 
-----SQL SERVER 日志   
 
declare @tmp table  (LogDate datetime,ProcessInfo varchar(32),Text nvarchar(max)) 
 
insert into @tmp 
EXEC master.dbo.xp_readerrorlog 0, 1, NULL, NULL, NULL, NULL, N'desc'---读取SQL Server 日志  
 
select * from @tmp where 1=1 
/* 
 一共有7个参数:  
 
1. 存档编号 
2. 日志类型(1为SQL Server日志,2为SQL Agent日志) 
3. 查询包含的字符串 
4. 查询包含的字符串 
5. LogDate开始时间 
6. 结果排序,按LogDate排序(可以为降序"Desc" Or 升序"Asc") 
7. 结果排序,按LogDate排序(可以为降序"Desc" Or 升序"Asc")  
 
在输入第5和第6个参数的时候,使用时间里包含有秒、毫秒时候,有时候查询速度非常慢,而且导致CPU占用率为100%。 
 
*/ 
 
 
--作业活动监视器 详细内容  
 
SELECT c.job_id,a.name,case when a.enabled =1 then '是' else '否' end '是否启用', 
 a.date_created '创建时间',a.date_modified '修改时间', 
 left(b.last_run_date,4)+'/'+SUBSTRING(convert(varchar(8),b.last_run_date),5,2)+'/'+right(b.last_run_date,2)+'  '+ 
 case when b.last_run_time=0 then '0:00:00'  
      when LEN(b.last_run_time)=3 then '0:0'+SUBSTRING(convert(varchar(6),b.last_run_time),1,1)+':'+RIGHT(b.last_run_time,2) 
      when LEN(b.last_run_time)=4 then '0:'+LEFT(b.last_run_time,2)+':'+RIGHT(b.last_run_time,2) 
      when len(b.last_run_time)=5 then left(b.last_run_time,1)+':'+SUBSTRING(convert(varchar(6),b.last_run_time),2,2)+':'+right(b.last_run_time,2) 
      else left(b.last_run_time,2)+':'+SUBSTRING(convert(varchar(6),b.last_run_time),3,2)+':'+right(b.last_run_time,2)end'上次运行时间', 
 left(c.next_run_date,4)+'/'+SUBSTRING(convert(varchar(8),c.next_run_date),5,2)+'/'+right(c.next_run_date,2)+'  '+ 
 case when c.next_run_time=0 then '0:00:00'  
      when LEN(c.next_run_time)=3 then '0:0'+SUBSTRING(convert(varchar(6),c.next_run_time),1,1)+':'+RIGHT(c.next_run_time,2) 
      when LEN(c.next_run_time)=4 then '0:'+LEFT(c.next_run_time,2)+':'+RIGHT(c.next_run_time,2) 
      when len(c.next_run_time)=5 then left(c.next_run_time,1)+':'+SUBSTRING(convert(varchar(6),c.next_run_time),2,2)+':'+right(c.next_run_time,2) 
      else left(c.next_run_time,2)+':'+SUBSTRING(convert(varchar(6),c.next_run_time),3,2)+':'+right(c.next_run_time,2)end '下次运行时间', 
  case   when substring(b.last_outcome_message,1,CHARINDEX('。', b.last_outcome_message)) is NULL then 
  '未知' else substring(b.last_outcome_message,1,CHARINDEX('。', b.last_outcome_message)) end '上次运行结果' 
 
FROM 
[msdb].[dbo].[sysjobs_view] a  
join    [msdb].[dbo].[sysjobservers] b 
on a.job_id =b.job_id  
join [msdb].[dbo].[sysjobschedules] c 
on a.job_id =c.job_id  
where a.category_id =0 or a.category_id =3 
 
----每个作业详细运行步骤及结果  
 
select a.name ,a.description,a.date_created,a.date_modified, 
  b.message,   
  left(b.run_date,4)+'/'+SUBSTRING(convert(varchar(8),b.run_date),5,2)+'/'+right(b.run_date,2)+'  '+ 
   case when b.run_time=0 then '0:00:00'  
    when LEN(b.run_time)=3 then '0:0'+SUBSTRING(convert(varchar(6),b.run_time),1,1)+':'+RIGHT(b.run_time,2) 
    when LEN(b.run_time)=4 then '0:'+LEFT(b.run_time,2)+':'+RIGHT(b.run_time,2) 
    when len(b.run_time)=5 then left(b.run_time,1)+':'+SUBSTRING(convert(varchar(6),b.run_time),2,2)+':'+right(b.run_time,2) 
    else left(b.run_time,2)+':'+SUBSTRING(convert(varchar(6),b.run_time),3,2)+':'+right(b.run_time,2)end'运行时间', 
    case when b.run_status=1 then '成功' else '失败' end '状态' 
 
 FROM [msdb].[dbo].[sysjobs_view] a ,[msdb].[dbo].[sysjobhistory] b 
 where a.job_id =b.job_id and (a.category_id =0 or a.category_id =3) 

linux

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

microsoft sql server是什么软件 microsoft sql server是什么软件 Feb 28, 2023 pm 03:00 PM

microsoft sql server是什么软件

PHP和SQL Server数据库开发 PHP和SQL Server数据库开发 Jun 20, 2023 pm 10:38 PM

PHP和SQL Server数据库开发

SQL Server还是MySQL?最新研究揭秘最佳数据库选择。 SQL Server还是MySQL?最新研究揭秘最佳数据库选择。 Sep 08, 2023 pm 04:34 PM

SQL Server还是MySQL?最新研究揭秘最佳数据库选择。

浅析PHP连接SQL Server的五种方法 浅析PHP连接SQL Server的五种方法 Mar 21, 2023 pm 04:32 PM

浅析PHP连接SQL Server的五种方法

如何使用PDO连接到Microsoft SQL Server数据库 如何使用PDO连接到Microsoft SQL Server数据库 Jul 29, 2023 pm 01:49 PM

如何使用PDO连接到Microsoft SQL Server数据库

SQL Server与MySQL较量,如何选择最佳数据库方案? SQL Server与MySQL较量,如何选择最佳数据库方案? Sep 10, 2023 am 08:07 AM

SQL Server与MySQL较量,如何选择最佳数据库方案?

SQL Server与MySQL对比:哪个数据库更适合高可用性架构? SQL Server与MySQL对比:哪个数据库更适合高可用性架构? Sep 10, 2023 pm 01:39 PM

SQL Server与MySQL对比:哪个数据库更适合高可用性架构?

在Go语言中使用SQL Server:完整指南 在Go语言中使用SQL Server:完整指南 Jun 18, 2023 am 09:18 AM

在Go语言中使用SQL Server:完整指南

See all articles