SQL Server guides
Practical fixes and patterns for SQL Server automation, written from the scripts in SQL Rig for SQL Server.
- Fix Msg 15002: sp_addlinkedserver cannot be executed within a transaction
Why sp_addlinkedserver fails with Msg 15002 inside BEGIN TRAN, and a safe, idempotent way to create a linked server with TRY/CATCH cleanup instead. - Fix Msg 156: incorrect syntax near the keyword 'CASE' in EXEC parameters
Why EXEC dbo.Proc @p = CASE ... END fails with Msg 156, incorrect syntax near the keyword CASE, and three correct ways to pass a computed value. - Safe batched DELETE in SQL Server, with a dry run
How to delete millions of rows in SQL Server safely: DELETE TOP (N) in short transactions, a @DryRun default, row caps, progress logging, and log-growth notes. - How to make AI coding agents safe on SQL Server
Practical guardrails for using Cursor, Claude Code, and GitHub Copilot with SQL Server: rules files, least-privilege logins, dry-run defaults, -WhatIf, and review checklists. - Fix orphaned SQL Server users after a restore (Msg 15023 and Msg 916)
Why database users become orphaned after a restore to another server, how to find them by SID, and how to fix them with ALTER USER WITH LOGIN instead of sp_change_users_login. - SQL Server Agent job failed: find the real error in msdb job history
The job history says only 'The job failed. The last step to run was step 2.' How to pull the step-level error from msdb.dbo.sysjobhistory, capture full step output, and make PowerShell and CmdExec steps fail honestly. - SQL Server blocking: find the head blocker, even when it is sleeping
Queries are stuck on LCK_M waits and sp_who2 shows a BlkBy chain. How to find the head blocker with DMVs, why it is often a sleeping session with an open transaction, and what to do about it before reaching for KILL. - SQL Server wait stats: measure waits over a window, not since restart
sys.dm_os_wait_stats is cumulative since the last restart, so its top waits describe weeks of history, not the slowdown happening now. How to take two snapshots, diff them, filter idle waits, and keep a history without clearing the counters. - SQL Agent job failed: "Unable to determine if the owner has server access"
Why a SQL Server Agent job fails before step 1 with "Unable to determine if the owner of job has server access" (often Error 15404, code 0x534 or 0x5), how to find every job with a risky owner, and how to change the owner without breaking the job. - Linked server login failed for NT AUTHORITY\ANONYMOUS LOGON (Msg 18456 / 7399)
OPENQUERY or sp_testlinkedserver fails with Msg 7399 / 7303 wrapping Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON' (18456). How to diagnose the Windows double-hop, inspect linked login mappings, and fix it with an explicit least-privilege remote login or Kerberos constrained delegation. - VIEW SERVER STATE permission was denied (Msg 300): a least-privilege diagnostics login
Why a monitoring or diagnostics query fails with Msg 300 "VIEW SERVER STATE permission was denied" (or VIEW SERVER PERFORMANCE STATE on SQL Server 2022), why some DMVs fail quietly instead, and how to build a read-only diagnostics login without handing out sysadmin. - SQL Server blocked access to procedure 'sys.xp_cmdshell' (Msg 15281): move the command to an Agent PowerShell step
Why EXEC xp_cmdshell fails with Msg 15281 "SQL Server blocked access to procedure 'sys.xp_cmdshell'", why turning it on is the wrong fix, and how to move the command into a SQL Server Agent PowerShell step that runs under a least-privilege credential and proxy. - The transaction log for database is full due to 'LOG_BACKUP' (Msg 9002): fix and prevent it
Why SQL Server raises Msg 9002 "The transaction log for database is full due to 'LOG_BACKUP'", how to confirm the cause with log_reuse_wait_desc and backup history, how to free the log with a verified log backup, what to check when the space does not come back, and how to keep it from happening again. - Query Store forced plan not used: find plan forcing failures (NO_INDEX)
Why a plan you forced in SQL Server Query Store can silently stop being used, how to find it with force_failure_count and last_force_failure_reason_desc, how to see which plan actually ran, how to find the missing or renamed index, and how to catch the next failure with Extended Events. - Transaction was deadlocked (Msg 1205): find the deadlock graph and fix the cause
What SQL Server Msg 1205 "chosen as the deadlock victim" means, how to pull the deadlock graph from the built-in system_health session with T-SQL, how to read which statements and indexes were involved, how to fix the access order, and how to retry the victim safely.