Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / SQL Server / Find and Remove Locks in Microsoft SQL Server

March 3, 2023 SQL Server

Find and Remove Locks in Microsoft SQL Server

Locks in MS SQL Server are one way to ensure the integrity of data when changes are made by multiple users at the same time. MSSQL locks objects on a table when a transaction starts and releases the lock when the transaction ends. In this article, we will learn how to find locks in the MS SQL Server database and remove them.

It is possible to simulate a lock on one of the tables using an unfinished transaction (one that has not been completed by rollback or commit). For example, use the following SQL query:

USE tesdb1
BEGIN TRANSACTION
DELETE TOP(1) FROM tblStudents

Before any changes are made, SQL Server will lock the table. Now try opening SQL Server Management Studio and executing a simple SQL select query:

SELECT * FROM tblStudents

The query will freeze in the “Executing query” state until it timed out. This is because the SELECT query is trying to access data in a table that is locked by SQL Server.

Hang query in MS SQL due to table lock (block)

You can configure row-level or full-table-level locks in Microsoft SQL Server.

To get a list of all blocked queries in MSSQL Server, run the command

select cmd,* from sys.sysprocesses
where blocked > 0

You can also display a list of locks for a specific database:
SELECT * FROM master.dbo.sysprocesses
WHERE
dbid = DB_ID('testdb12') and blocked <> 0
order by blocked

The Blocked column shows the process ID of the process that blocked the resources. It also shows the wait time for this query (waittime in milliseconds). If required, this can be used to search for the earliest or latest locks.

find blocked processes in sql server

In some cases, a lock can be caused by an entire process tree. To find the source lock process, use the following query for SPID until you find the process with blocked=0, which is the initial process holding the lock.

select * FROM
master.dbo.sysprocesses
where 1=1
--and blocked <> 0
and spid = 59

The process SPID gives you the T-SQL code of the last SQL query executed by that process or transaction:

DBCC INPUTBUFFER(59)

dbcc get sql query string

To force kill the process and release the lock, run the command:

KILL number_of_session
GO

In my case, it will be:

KILL 59

kill process in mssql

You can create a separate stored procedure if locks are constantly occurring and you want to identify the most resource-intensive queries:

CREATE PROCEDURE GetCurrentQueryCode
@SPID int
AS
DECLARE @sql_handle binary(20), @stmt_start int, @stmt_end int
SELECT @sql_handle = sql_handle, @stmt_start = stmt_start/2, @stmt_end = CASE WHEN stmt_end = -1 THEN -1 ELSE stmt_end/2 END
FROM master.dbo.sysprocesses
WHERE spid = @SPID AND ecid = 0
DECLARE @line nvarchar(4000)
SET @line = (SELECT SUBSTRING([text], COALESCE(NULLIF(@stmt_start, 0), 1),
CASE @stmt_end WHEN -1 THEN DATALENGTH([text]) ELSE (@stmt_end - @stmt_start) END) FROM ::fn_get_sql(@sql_handle))
print @line

Now, if you want to see the SQL query that locked the table, all you need to do is specify its SPID:

Exec GetCurrentQueryCode 51

SQL server stored procedure to find lock query

You can also get the query code from the sql_handle of the locking process. For example

select * from sys.dm_exec_sql_text (0x0100050069139B0650B35EA64702000000000000)

select * from sys.dm_exec_sql_text

You can use the Microsoft SQL Server Management Studio to search for locks in MS SQL Server. You can use one of the following methods:

  • Right-click on the server, open the Activity Monitor and expand Processes. You will see a list of requests waiting for resources to be released with a SUSPENDED status. MSSQL: Activity Monitor SUSPENDED processes
  • Select a database and navigate to Reports -> All Blocking Transactions. It also shows a list of locked queries and the SPID of the locking source. Microsoft SQL Server - All Blocking Transactions
1 comment
3
Facebook Twitter Google + Pinterest
previous post
Copy/Paste Not Working in Remote Desktop (RDP) Clipboard
next post
Internet Time Synchronization Failed on Windows

Related Reading

Connect to MS SQL Server Database in Visual...

June 20, 2023

MS SQL Server Setup Stucks on Install/Uninstall

January 9, 2023

Upgrade Microsoft SQL Server Evaluation Edition to Standard/Enterprise

December 9, 2021

Configuring Always-On High Availability Groups on SQL Server

December 2, 2021

SQL Server: Could Not Find the Database Engine...

September 30, 2019

1 comment

kapsiR March 3, 2023 - 7:03 am

I can really recommend sp_whoisactive in this case:
https://github.com/amachanic/sp_whoisactive

It’s very helpful if you are looking for blocking queries and more!

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • MS SQL Server Setup Stucks on Install/Uninstall
  • Connect to MS SQL Server Database in Visual Studio Code
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top