SQL SERVER – Applying NOLOCK to Every Single Table in Select Statement – SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

READ UNCOMMITTED changes session read isolation without adding NOLOCK to every table. A workshop procedure made the distinction useful.

An unsettled mosaic in its assembly jig is inspected beside a stable finished sample.

-- Original WideWorldImporters demonstration; use a separate lab connection.
-- Table-hint form for the original two-table comparison:
SELECT ct.CityName, sp.StateProvinceName
FROM Application.Cities AS ct WITH (NOLOCK)
JOIN Application.StateProvinces AS sp WITH (NOLOCK)
  ON ct.StateProvinceID = sp.StateProvinceID;

-- Session-level form:
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT ct.CityName, sp.StateProvinceName
FROM Application.Cities AS ct
JOIN Application.StateProvinces AS sp
  ON ct.StateProvinceID = sp.StateProvinceID;
-- Restore the connection's intended isolation level before further work.
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

That procedure had more than 90 tables and 14 views. Editing each table temporarily was awkward. For ordinary reads, NOLOCK is equivalent to a READUNCOMMITTED table hint. A session setting has broader reach, while explicit hints and procedure behavior can affect statements.

Dirty reads can observe uncommitted data, miss rows or duplicate rows. Schema locks can still block them. They don’t rewrite stored data directly. Incorrect results can nevertheless drive incorrect decisions or downstream updates.

Use a separate test connection and restore its intended isolation level. READ COMMITTED assumes that was the previous level. That assumption doesn’t hold for every application. Diagnose the blocking chain and correctness requirements before choosing an isolation design.

Related reading

Dirty-read isolation is not a general blocking cure, it is a correctness tradeoff that needs an explicit decision.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Query Hint, SQL Scripts, SQL Server, Transaction Isolation
Previous Post
SQL SERVER- High CPU and Transaction Type WorkFileGroup_fake_worktable and Workfile
Next Post
Right-Sizing SQL Server: Reading CPU and Memory Use Before Consolidation

Related Posts

12 Comments. Leave new

  • try with (nolock) with using cross apply or outer apply ??

    Reply
  • James DiBernardo
    September 4, 2018 3:01 am

    What permissions does the user running the query need for :
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

    Reply
    • I don’t think there is a special permission needed. Are you getting any error when you run

      SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

      Please help me to reproduce the error.

      Reply
  • If both are same, then would prefer to use (NoLock) hint. Have flexibility to apply for the tables only which are fine to read dirty data.

    Reply
  • What if SP don’t reach till last statement due to some error, will it affect other running SPs?

    Reply
  • For SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED the control is with user. If user forgets and did a select & his session is active, we are not able to do truncate and reload as part of ETL process. Is there any way this can be set at DB or schema level by DBA?

    Reply
  • Timothy james Ultican
    November 8, 2018 3:09 am

    Pinal, I’ve been following your website, I think since the late 90’s, maybe early 2000’s love the blog, keep up the great work!!

    Question, shouldn’t there be a preface with Transaction Isolation Level read uncommitted? For example, if I have an EDW that has a nightly ETL batch starting at midnight, and as long as I don’t have end user read/running reports until after the nightly batch is finished then theoretically I don’t ever have to worry about a dirty read?? Am I understanding the Isolation level correctly? Thanks!!!

    Reply
  • Shashank Shekhar
    February 20, 2023 12:48 pm

    I added SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED on the top of the stored procedure eg.
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
    GO
    Create PROCEDURE [dbo].xxxx
    as
    begin
    zzzzz
    end
    SET TRANSACTION ISOLATION LEVEL READ COMMITTED
    GO

    I did on Friday but on Monday from all stored procedure Dirty Read line were missing whereas other changes were still as it is.

    Reply

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.