SQL Server logs and agent error days are usually automatically switched after the instance restarts, and if the instance has not been restarted for a long time, it may accumulate too many logs and is inconvenient to view.
Log switchover: (Requires permission: Member of the sysadmin fixed server role)
--"SQL Server log" toggle exec msdb.dbo.sp_cycle_errorlog--"Agent error log" toggle exec Msdb.dbo.sp_cycle_agent_errorlog
Log View:
--Log view exec Xp_readerrorlog @FileNum, @Type, @SearchText1, @SearchText2, @StartTime, @EndTime, @order @filenum: Log number @Type : Query type (1:sql Server log; 2: Agent error log) @SearchText1: Search text @searchtext2: Search text (with @SearchText1 for "and" relationship) @StartTime: Log query start time @start Time: Log query end @order: Time sort (ASC or DESC)
Example:
--View current "SQL Server log" deadlock exec xp_readerrorlog 0,1, ' deadlock victim ', NULL, ' 2015-06-01 ', ' 2015-06-30 ', ' desc ' --View second "SQL Server Log" text has "error" and "Server" row exec xp_readerrorlog, ' Error ', ' Server ', ' 2015-06-01 ', ' 2015-06-30 ', ' ASC ' --View the current "Agent error Log" text with "error" in the line exec Xp_readerrorlog 0, 2, ' Error ', null,null,null, ' desc '
SQL Server error log switching and viewing