Nolock In SQL Server

Introduction

When the database engine alters its data, it uses several unusual forms of controls. Locks are the name given to these unique controls. These locks safeguard the integrity of the database by demonstrating that the database records were involved in the transactions.
The SQL Server often employs several locks to separate the transaction at various levels. We will talk about the distinction between Nolock and With Nolock in SQL Server Database in this article.

What is Nolock in SQL Server?

The Nolock only applies to SELECT statements and is also known as READUNCOMMITTED. Other transactions cannot edit the data in the table since it is specified that no shared locks can be issued against the table.

To demonstrate how Nolock functions, let’s look at an example:

BEGIN TRANSACTION
      INSERT INTO TRANSACTIONMASTER
      (Product, SaleDate, SalePrice)          
      VALUES            
      ('PoolTable', GETDATE(), 500)

We can observe that TRANSACTIONMASTER still has locks issued against it after putting a record into it. Execute the following query with the Nolock table hint in the other query window to check for newly added records.

SELECT COUNT(*) FROM TRANSACTIONMASTER WITH(NOLOCK)

The transaction that I placed in testable is still pending commitment since the select statement yields 11 entries. The entered transactions can still be rolled back by using the command shown below in the query window, according to this.

Rollback Transaction

The record is eliminated from the TRANSACTIONMASTER via the Rollback statement. We may use the choose statement to verify the changes:

SELECT COUNT(*) FROM TRANSACTIONMASTER WITH(NOLOCK)

We may see that the Nolock intimation is meant to prevent the database engine from issuing locks on the tables because the aforementioned statement yields 10 entries.

What in SQL Server is With (Nolock)?

It is an explicit command that specifically refers to a certain table or view. It’s comparable to the Nolock hint. Once the command has been sent, it stops using locks on the data in the database. The advantage of utilising With Nolock is that no deadlock is experienced when queries are being performed on the table; in addition, no locks need to be held against the data, which will save up memory.
There is nothing to be done with subqueries while using WITH (nolock). It may be utilised with either single or subqueries, though. The transaction isolation levels of WITH (nolock) and Readuncommitted are comparable. But because it will provide contradictory results, using WITH (nolock) may not be safe.

Conclusion

As can be seen, the Nolock reads data that has not been committed and may be rolled back, which is the difference between Nolock and With (Nolock). We may thus conclude that Nolock reads “Dirty Data” in a SQL Server database when used with merely a Select operation. Exclusive and shared locks are not issued by (Nolock), though. When using (Nolock), it is possible to read an uncommitted transaction that can be rolled back in the midst of the read.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories