Why This Error Happens
SQL Server 2025 blocks the SHUTDOWN statement whenever it detects that the statement is being executed inside an open transaction (i.e., after a BEGIN TRANSACTION that has not yet been committed or rolled back) or from within the body of a stored procedure, trigger, or function. The SHUTDOWN command is designed to run only as a standalone, top-level batch statement because it terminates the Database Engine process itself — SQL Server cannot guarantee transactional consistency if the engine is halted mid-transaction. When this rule is violated, the engine raises Error 6007 and aborts the shutdown request. A secondary but related cause is the calling login lacking the server-level role membership required to issue the command.
Step-by-Step Resolution Guide
Step 1: Locate the Calling Code
- Search stored procedures, functions, and triggers for any embedded
SHUTDOWNcall by running:
SELECT OBJECT_SCHEMA_NAME(object_id) AS SchemaName,
OBJECT_NAME(object_id) AS ObjectName,
definition
FROM sys.sql_modules
WHERE definition LIKE '%SHUTDOWN%';
- Also check SQL Server Agent job steps, since scheduled jobs commonly wrap
SHUTDOWNinside a T-SQL step:
SELECT j.name AS JobName, s.step_name, s.command
FROM msdb.dbo.sysjobsteps s
JOIN msdb.dbo.sysjobs j ON s.job_id = j.job_id
WHERE s.command LIKE '%SHUTDOWN%';
- Inspect any
.sqldeployment or maintenance scripts stored on disk (typically underC:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Jobs\or your organization’s custom scripts directory) forBEGIN TRAN ... SHUTDOWN ... COMMITpatterns.
Step 2: Remove the Transaction or Procedure Wrapper
- Extract the
SHUTDOWNstatement so it is the only statement in its batch, with no enclosingBEGIN TRANSACTION, stored procedure body, or trigger body. - Run it as a standalone batch in SQL Server Management Studio (SSMS):
USE master;
GO
SHUTDOWN WITH NOWAIT;
GO
- If the procedure was built to allow administrators to restart the service remotely, redesign it so the procedure only returns a status flag, and have the calling application (or an Agent job step set to run a standalone T-SQL command) issue
SHUTDOWNdirectly instead of embedding it.
Step 3: Verify the Login’s Server-Level Permissions
SHUTDOWNrequires the executing login to be a member of the sysadmin fixed server role (or hold theSHUTDOWNpermission granted explicitly in newer builds). Confirm membership with:
SELECT IS_SRVROLEMEMBER('sysadmin') AS IsSysAdmin;
- To see all server role memberships for the current login:
SELECT sp.name AS LoginName, r.name AS ServerRole
FROM sys.server_role_members srm
JOIN sys.server_principals sp ON srm.member_principal_id = sp.principal_id
JOIN sys.server_principals r ON srm.role_principal_id = r.principal_id;
- If the login is missing the role, add it from an existing sysadmin session:
ALTER SERVER ROLE sysadmin ADD MEMBER [DOMAIN\ServiceAccount];
Step 4: Review the SQL Server Error Log
- Rather than assuming a hardcoded path, retrieve the exact active log location directly from the instance:
SELECT SERVERPROPERTY('ErrorLogFileName') AS ErrorLogPath;
- On a default SQL Server 2025 installation on Windows Server 2025 / Windows 11, this typically resolves to:
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOG
- Open the file in Notepad, or view recent entries through SSMS under Management > SQL Server Logs > Current, and locate the timestamp matching the Error 6007 event to confirm the offending batch and login.
Step 5: Re-run the Corrected Script
- Execute the corrected, standalone script from SSMS, or from a command prompt using
sqlcmdagainst the target host:
sqlcmd -S YourServerName -E -Q "SHUTDOWN WITH NOWAIT"
- If using SQL Authentication instead of Windows Authentication:
sqlcmd -S YourServerName -U sa -P YourPassword -Q "SHUTDOWN WITH NOWAIT"
Alternative or Verification Steps
- Verify the fix: Re-run the original workflow and confirm Error 6007 no longer appears by checking the error log again:
EXEC sp_readerrorlog 0, 1, 'Error: 6007';
No matching rows should be returned if the issue is resolved.
- Alternative if the primary steps fail: Open a brand-new query window in SSMS (guaranteeing no open transaction state), confirm
@@TRANCOUNTis0:
SELECT @@TRANCOUNT;
Then issue the shutdown manually:
SHUTDOWN WITH NOWAIT;
Afterward, review the Windows Event Viewer under Windows Logs > Application, filtering on Source MSSQLSERVER, to confirm the clean shutdown and service stop were logged without further errors.