Read specific errors using a temp table
Ever need to quickly check recent errors in your SQL Server logs? This handy T-SQL snippet allows you to query the SQL Server error log and gather information about various processes and messages. It's a great way to get a snapshot of what's been happening in your SQL Server environment.
Querying SQL Server Error Logs
The following SQL code creates a temporary table and populates it by reading the last seven SQL Server error logs (from 0 to 6). It then selects all entries where the process is not 'logon', helping you filter out routine login messages and focus on actual errors or important events.
create table #t (dt datetime, process varchar(200), msg varchar(1000)) insert into #t exec sp_readerrorlog 0,1,'error' insert into #t exec sp_readerrorlog 1,1,'error' insert into #t exec sp_readerrorlog 2,1,'error' insert into #t exec sp_readerrorlog 3,1,'error' insert into #t exec sp_readerrorlog 4,1,'error' insert into #t exec sp_readerrorlog 5,1,'error' insert into #t exec sp_readerrorlog 6,1,'error' select * from #t where process <> 'logon'---
Further Reading and Related Topics
For more insights into SQL Server, error handling, and performance monitoring, check out these excellent resources:
- Manufacturer information inside SQL Server - Learn how to dig into hardware details within your SQL Server instance.
- How to use Extended Events to catch all errors in SQL Server - A deeper dive into using Extended Events for comprehensive error capturing.
- SQL Socket & Core information - Understand how SQL Server utilizes your system's sockets and cores.
- Get IO warning from log - Identify potential I/O bottlenecks or issues from your SQL Server logs.
- Get TempDB files count - A quick tip for checking the number of TempDB files.
- How to find SQL Server Cluster information - Discover details about your SQL Server cluster setup.