SQL Server: T-SQL script to generate a DeadLock in a Database

The DeadLock in a database system is a very common thing and all Database Administrator responsible to detect and manage a DeadLock situation.

In this post, I am sharing T-SQL script to generate a DeadLock situation in a SQL Server.

Most of the time, We are facing DeadLock problem in our Production Database Server.
But What happen, when Database Administrator wants to generate DeadLock situation and wants to create a script for detecting a DeadLock.

First, create two tables with sample data:

Step 1: Open first connection and Execute below query.

Step 2: Open second connection and Execute below queries.

Step 3: Execute below query in first connection again.

Up to Step 2, you can find that queries are running and waiting to complete Step 1. Here, Step 1 became a blocking step for Step 2.
Once you execute the query of the Step 3 on first connection, you will get DeadLock situation and will get below message.

Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.

I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.

More from dbrnd.com

Leave a Reply

2 Comments on "SQL Server: T-SQL script to generate a DeadLock in a Database"

Notify of

Sort by:   newest | oldest | most voted
6 months 15 days ago

Hi Amit,as per you,you have a script to find deadlock information, but i didn’t get such type of script at any where…. Ex: SP_who2 will tell Lock information by that way any script to findout Dead lock details..