Kill SQL Server Sessions with Targeted Filters Using KILL and dm_exec_sessions This article covers a practical T-SQL script that kills one or more SQL Server sessions (SPIDs) while giving you precise control over which sessions are targeted. The script uses sys.dm_exec_sessions to query active sessions and a cursor to …
Read MoreSQL Server Backup Status Report: All Databases
Check SQL Server Backup Age Across 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 Database backups are your last line …
Read MoreSQL Server User Permissions and Role Memberships
Audit SQL Server Database User Permissions and Role Memberships This script queries the SQL Server system catalog to produce a complete audit of database-level permissions, covering direct object grants to users, permissions inherited through database roles, and permissions granted to the public role — all in a single …
Read MoreSQL Server Search Stored Procedure and View Text
Apr 9, 2026 / · 6 min read · sql server sql scripts sys.sql_modules sys.objects stored procedures views database administration object search maintenance scripts schema management ·Search Stored Procedure and View Definitions in SQL Server This script searches the text of stored procedures, views, functions, and triggers in the current database using sys.sql_modules, returning any programmable object whose name or definition contains a given keyword. Purpose and Overview Finding which stored …
Read MoreSQL Server Error Log Search Script: xp_readerrorlog
Apr 8, 2026 / · 7 min read · sql server sql scripts xp_readerrorlog sp_readerrorlog error log database administration monitoring diagnostics maintenance scripts sys.sp_enumerrorlogs ·Search the SQL Server Error Log with xp_readerrorlog This script reads and filters the SQL Server error log using xp_readerrorlog and sys.sp_readerrorlog, supporting keyword search, date range filtering, and access to archived log files — all from T-SQL without touching the file system. Purpose and Overview The SQL …
Read MoreSQL Server Verify Backup Files: RESTORE VERIFYONLY
Verify SQL Server Backup Files with RESTORE VERIFYONLY Backup files that cannot be restored are worthless, and many DBAs discover problems only when they attempt an actual restore under pressure. The scripts in this article use RESTORE VERIFYONLY to confirm that backup files are readable and internally consistent …
Read MoreSQL Server Top Queries by CPU and IO: dm_exec_query_stats
Apr 4, 2026 / · 6 min read · sql server performance sql scripts sys.dm_exec_query_stats sys.dm_exec_sql_text sys.dm_exec_query_plan query tuning database administration monitoring execution plan ·Find the Top CPU and IO Consuming Queries in SQL Server 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 …
Read MoreSQL Server Index Fragmentation: dm_db_index_physical_stats
Apr 3, 2026 / · 6 min read · sql server index maintenance sql scripts sys.dm_db_index_physical_stats fragmentation database administration performance maintenance scripts indexes rebuild reorganize ·Report Index Fragmentation Across All Tables in SQL Server This script queries sys.dm_db_index_physical_stats to report external and internal fragmentation for every index in the current database, filtering to indexes with significant fragmentation and enough pages to make maintenance worthwhile. Purpose and Overview …
Read MoreSQL Server Database Size Report for All Databases
Apr 2, 2026 / · 6 min read · sql server monitoring sql scripts sys.master_files sys.databases database administration capacity planning storage reporting maintenance scripts ·Report Database Size in MB and GB for All SQL Server Databases This script queries sys.master_files and sys.databases to report the total allocated size in megabytes and gigabytes for every user database on the SQL Server instance, sorted by largest database first. Purpose and Overview Knowing how large each database …
Read MoreSQL Server Unused Indexes: sys.dm_db_index_usage_stats
Apr 1, 2026 / · 7 min read · sql server performance sql scripts sys.dm_db_index_usage_stats index maintenance database administration nonclustered indexes query tuning maintenance scripts indexes ·Find Unused Nonclustered Indexes in SQL Server This script queries sys.dm_db_index_usage_stats to identify nonclustered indexes that have never been used by any read operation since the last SQL Server restart, yet still carry write overhead with every INSERT, UPDATE, and DELETE on the table. Purpose and Overview Every …
Read More