Database Scripts

Beta
 Log In    |   Sign Up

SQL Server : Create a Deadlock

Added on Feb-26-2012 by admin
For SQL Server

Tags : deadlocktesting

For testing purposes you might wish to create a deadlock in SQL Server. This script will create a deadlock to assist your testing.

Versions

SQL Server 2000 +

DOWNLOAD

-- 1) Create Objects for Deadlock 
USE TEMPDB
CREATE TABLE dbo.foo (col1 INT)
INSERT dbo.foo SELECT 1
CREATE TABLE dbo.bar (col1 INT)
INSERT dbo.bar SELECT 1
-- 2) Run in first connection and the server this connection was SPID 59
BEGIN TRAN
UPDATE tempdb.dbo.foo SET col1 = 1
-- 3) Run in second connection and the server this connection was SPID 62
BEGIN TRAN
UPDATE tempdb.dbo.bar SET col1 = 1
UPDATE tempdb.dbo.foo SET col1 = 1
-- 4) Run in first connection
UPDATE tempdb.dbo.bar SET col1 = 1

    

Report Script

blog comments powered by Disqus