In our day to day T-SQL querying we use lot of query hints to modify the way a particular query will be executed. When we specify query hint SQL Server produces optimized plan using this query hint. This can be dangerous if it is not tested before in UAT as it is known fact that query plan which SQL Server makes using optimizer, which is its prized possession, is the best. Algorithm written for optimizer at low level is not known to ordinary people, how it makes best/optimized, most cost effective plan is not known to outside world but we know it does. Query hints specify that the indicated hints should be used throughout the query and they affect all operators in the statement. One such query hint is NOLOCK. As the name suggests many users feel, that when this hint is specified in the query, the operation does not takes lock. This is not the case!
I will demonstrate it using simple query. I create a simple table with e_id as PK col, name, address and cell no.
Begin tran
select * from dbo.employee where e_id='a1'
exec sp_lock
If you see below this transaction has SPID 55 which is ID for the code which is just executed. It has taken two locks IS,S
In Mode Column S=Shared lock IS=Intent Shared
In type column DB=databse TAB=table Now let us run same query with NOLOCK query hint and see if it actually takes any lock
select * from dbo.employee with (NOLOCK) where e_id='a1'
begin tran
update dbo.employee set e_name='SHASHANK' where e_id='a1'
Naomi N edited Revision 2. Comment: Minor edit
Shanky_621 edited Revision 1. Comment: Corrected Typo