SQL Server: Find Tables Without Primary Keys
Apr 16, 2026 / · 7 min read · sql server primary keys database structure heap tables sql scripts schema audit performance sys.tables sys.dm_db_index_physical_stats sys.indexes sys.key_constraints sys.partitions ·Tables without primary keys are heap structures with no guaranteed row ordering and no unique row identifier. They cause full table scans in queries that could use seeks, slow down DELETE and UPDATE operations, and are incompatible with SQL Server replication and Change Data Capture. This script finds every user table …
Read MoreSQL Server Backup History: Duration and Size Report
Apr 13, 2026 / · 6 min read · sql server backup recovery msdb sql scripts database administration backup monitoring maintenance scripts reporting sys.databases backupset ·Query Backup Duration and File Size from SQL Server History This script queries SQL Server's msdb backup history to report the backup type, start and finish time, duration in minutes, and file size in both megabytes and gigabytes for every backup across all user databases on the server. Purpose and Overview Knowing …
Read MoreSQL Server Long-Running Queries: Find Active Sessions
This script queries sys.dm_exec_requests and sys.dm_exec_sql_text to show all currently executing queries on the SQL Server instance, sorted by how long they have been running, along with CPU time, I/O activity, blocking status, and wait type information. Purpose and Overview A query that normally completes in seconds …
Read MoreSQL 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 More