读取SQL的Agent代理的日志文件
发布时间:2021-01-17 06:34:48 所属栏目:MySql教程 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 use msdbgoif exists (select * from sysobjects where name = N'sp_sqlagent_read_errorlog' and type ='P') drop proc dbo.sp_sqlagent_read_errorl
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 use msdb go if exists (select * from sysobjects where name = N'sp_sqlagent_read_errorlog' and type ='P') drop proc dbo.sp_sqlagent_read_errorlog go create proc dbo.sp_sqlagent_read_errorlog @fileversion int = null as set nocount on declare @rc int declare @version int declare @pos int declare @errorlog_file nvarchar(255) declare @filename nvarchar(255) declare @filename_no_ext nvarchar(255) declare @dirname nvarchar(255) declare @buf nvarchar(255) -- SQL Server 7.0 if (charindex(N'7.00',@@version,0) > 0) begin exec @rc = master.dbo.xp_regread N'HKEY_LOCAL_MACHINE',N'SOFTWAREMicrosoftMSSQLServerSQLServerAgent',N'ErrorLogFile',@errorlog_file OUTPUT,N'no_output' end -- SQL Server 2000 needs to use instance aware Registry read if (charindex(N'8.00',0) > 0) begin exec @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'no_output' end -- reverse the string to find the last slash '' select @buf = reverse(@errorlog_file) -- determine posiktion of last slash,now first slash in reversed string select @pos = len(@buf) - charindex(char(92),@buf,0) + 1 -- extract the directory only part,part before the last slash select @dirname = substring(@errorlog_file,@pos) -- extract the filename,part after the last slash select @filename = substring(@errorlog_file,@pos + 1,len(@errorlog_file) - @pos) -- extract the filename with extension,part after dot in @filename select @filename_no_ext = substring(@filename,charindex(N'.',@filename,0)) -- change errorlog file to version X if (@fileversion is not null) select @errorlog_file = @dirname +N'' + @filename_no_ext + N'.' + convert(nchar,@fileversion) -- read file exec master.dbo.xp_readerrorlog 1,@errorlog_file go -- sample usage -- read currently active log file,SQLAGENT.OUT -- exec msdb.dbo.sp_sqlagent_read_errorlog -- read version 1 of log,SQLAGENT.1 -- exec msdb.dbo.sp_sqlagent_read_errorlog 1 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |