SQL Server Agent Job Failure History Report
Apr 11, 2026 / · 8 min read · sql server sql server agent msdb sql scripts database administration job monitoring maintenance scripts reporting sysjobs sysjobhistory sys.server_principals ·Report SQL Server Agent Job Status and Failure History This script queries the SQL Server Agent job history stored in msdb to produce a detailed report of each job's most recent run for each status (failed, succeeded, and so on), failure and success counts over the last 60 days, total lifetime run counts, and job step …
Read MoreThis T-SQL script kills one or more SQL Server sessions (SPIDs) chosen by filters: login, database, session state, open transactions and the age of the last request. It reads sys.dm_exec_sessions, builds a KILL statement for each match, and by default only prints what it would do. Purpose and Overview Clearing …
Read MoreSQL Server Backup Status Report: All Databases
This script queries the SQL Server backup history catalog to report the most recent full, differential, and transaction log backup for every database, including how many hours and minutes ago each backup completed. Purpose and Overview Which databases on this instance have not had a full backup since yesterday, and …
Read MoreA single catalog query can list every grant, deny, and role membership in a database, and the result is the starting point for an access review. The script below returns one flat result set that covers permissions granted directly to a user, permissions a user holds through a database role, and permissions granted to …
Read MoreSQL Server Restore Database Script with MOVE
Apr 9, 2026 / · 6 min read · restore database backup recovery dynamic sql sql server restore filelistonly move option database administration scripts ·Restoring a SQL Server database to a different server or folder fails when the file paths recorded in the backup do not exist on the target. The MOVE option of RESTORE DATABASE redirects each data and log file to a path you choose, and this script builds that statement from four variables so it can be previewed before …
Read MoreSQL Server Search Stored Procedure and View Text
Apr 9, 2026 / · 10 min read · sql server sql scripts sys.sql_modules sys.objects stored procedures views database administration object search maintenance scripts schema management ·Before a column is renamed or a table is dropped, the question is which procedures, views, and functions still mention it. This script answers that by searching the stored text of every SQL module in the current database for a keyword, matching on the object name as well as the definition. It returns the schema, object …
Read MoreSQL Server Error Log Search Script: xp_readerrorlog
Apr 8, 2026 / · 9 min read · sql server sql scripts xp_readerrorlog sp_readerrorlog error log database administration monitoring diagnostics maintenance scripts sys.sp_enumerrorlogs ·A database goes suspect at 2 a.m., a backup job reports failure, or one login is being refused again and again, and the answer is a line in the SQL Server error log. This script finds that line from T-SQL: it filters by keyword, narrows by time window, reads archived logs and loads the result into a temp table for …
Read MoreSQL Server Verify Backup Files: RESTORE VERIFYONLY
Verify SQL Server Backup Files with RESTORE VERIFYONLY A backup file that cannot be restored is worthless, and the problem often surfaces only when a restore is attempted under pressure. The two scripts below use RESTORE VERIFYONLY to confirm that backup files are complete and readable before a real disaster occurs. …
Read MoreSQL Server Top Queries by CPU and IO: dm_exec_query_stats
This script queries sys.dm_exec_query_stats to identify the most resource-intensive queries currently cached in the plan cache, showing logical reads, writes, CPU time, and elapsed time — with the full query text and execution plan for each. Purpose and Overview When a SQL Server instance is under CPU or I/O pressure, …
Read MoreSQL Server Index Fragmentation: dm_db_index_physical_stats
This T-SQL query lists the rowstore indexes in the current database whose leaf level is fragmented and large enough to be worth maintaining, and returns a ready-to-review ALTER INDEX statement for each one. It reads sys.dm_db_index_physical_stats in SAMPLED mode and filters on avg_fragmentation_in_percent and …
Read More