To simulate a deadlock with a sql-script

  • Hey,

    I would like to simulate a deadlock in the northwind-database using MS SQL-Server. I think that a sql-script would be the easiest way to do it. Does anyone have an example or a script to share that leads to a deadlock in the northwind database.

    Thanks a lot!

    Matt

  • Sure.

    Open two QueryAnalyzer windows.

    Paste this in the first:

    begin transaction

    Update Categories set CategoryName = RTrim(CategoryName)

    Update Customers set CompanyName = RTrim(CompanyName)

    Rollback

    paste this in the second

    begin transaction

    Update Customers set CompanyName = RTrim(CompanyName)

    Update Categories set CategoryName = RTrim(CategoryName)

    rollback

    Now alternate windows and run ONE STATEMENT at a time -- not the whole window. After the 3rd statement is run in each window you deadlock. You can then execute the rollback in the other window to get things back to normal (though the above is pretty benign in terms of an update). The one that deadlocks will rollback automatically.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply