Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 April SQL Server: T-SQL script to generate a DeadLock in a Database

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

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

The DeadLock in a database system is a common thing and all Database Administrators are responsible for detecting and managing a DeadLocks.

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 happens, when Database Administrator wants to generate DeadLock situation and intends to create a script for detecting a DeadLock.

First, create two tables with sample data:

1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE TABLE tbl_SampleTable_A
(ID INT IDENTITY(1,1), Name VARCHAR(50))
GO
 
CREATE TABLE tbl_SampleTable_B
(ID INT IDENTITY(1,1), Name VARCHAR(50))
GO
 
INSERT INTO tbl_SampleTable_A(Name) VALUES ('dbrnd')
GO 20
 
INSERT INTO tbl_SampleTable_B(Name) VALUES ('dbrnd')
GO 20

Step 1: Open first connection and Execute below query.

1
2
BEGIN TRAN
UPDATE tbl_SampleTable_A SET Name = 'Anvesh' WHERE ID=1

Step 2: Open second connection and Execute below queries.

1
2
3
BEGIN TRAN
UPDATE tbl_SampleTable_B SET Name = 'Anvesh' WHERE ID=2
UPDATE tbl_SampleTable_A SET Name = 'Anvesh' WHERE ID=2

Step 3: Execute below query in first connection again.

1
UPDATE tbl_SampleTable_B SET Name = 'Anvesh' WHERE ID=1

Up to Step 2, you can find that queries are running and waiting to complete Step 1. Here, Step 1 became a blocker step for Step 2.

Once you execute the query of Step 3 on first connection, you will get DeadLock situation and will get the message.

1
2
Msg 1205, Level 13, State 51, Line 4
Transaction (Process ID 46) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

Apr 8, 2016Anvesh Patel
Database Theory: What is Optimistic Locking and Pessimistic Locking?SQL Server: 8 different ways to Detect a DeadLock in a Database
Comments: 2
  1. Ramakrishna
    April 9, 2016 at 2:58 am

    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..

    • Anvesh Patel
      Anvesh Patel
      April 10, 2016 at 5:27 am

      Hi Ramakrishna, I have prepared different ways to find deadlock in sql server.
      You can visit this recent post.
      https://www.dbrnd.com/2016/04/sql-server-8-different-ways-to-detect-a-deadlock-in-a-database/

Anvesh Patel
Anvesh Patel

Database Engineer

April 8, 2016 SQL ServerAnvesh Patel, database, database research and development, dbrnd, DeadLock, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....