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 2017 March SQL Server: When records lock, use %%lockres%% to find the page information of records

SQL Server: When records lock, use %%lockres%% to find the page information of records

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

I shared a couple of articles on SQL Server database locking and how to detect a deadlock. Please access few of them.

Different ways to detect a deadlock
What happens after the Deadlock?

I recently found an undocumented function %%lockres%% which we can use to locate the page information of records especially for those records which hold a Lock.

It returns a hash value same as like column “resource_description” of table sys.dm_tran_locks, but the good thing is you can use it in your SELECT statement and can correlate the column resource_description of table sys.dm_tran_locks.

By this way, you can get the exact record, and it’s page info which currently held by a Lock.

Let me demonstrate this,
Prerequisites:
Use below article and generate a sample deadlock situation in your database server.

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

Check the result of below query and note down the result of column “resource_description”:

1
select *from sys.dm_tran_locks

SQL Server Deadlock Result

Use %%lockres%% and SELECT both the tables:
You can easily correlate the result of sys.dm_tran_locks column resource_description and the result of %%lockres%%. You can find an exact record which held by a Lock.

1
2
3
4
5
SELECT %%lockres%% AS RecordPageInfo,*
FROM tbl_SampleTable_A
 
SELECT %%lockres%% AS RecordPageInfo,*
FROM tbl_SampleTable_B

SQL Server Deadlock Record Page Info

After this exercise, Please COMMIT all your open transactions.

Mar 4, 2017Anvesh Patel
SQL Server: How to SELECT Random records from a Table?SQL Server Interview: How to Reset or Recover SA Password? (System Administrator Login)
Anvesh Patel
Anvesh Patel

Database Engineer

March 4, 2017 SQL Server%%lockres%%, Anvesh Patel, data page, database, database research and development, dbrnd, DeadLock, dm_tran_locks, lock, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, 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....