I have CTEs which all uses NOLOCK inside. But then selects from those CTEs in parent CTEs using children CTEs doesn’t use NOLOCK in presumption that it is already NOLOCK’d. And the final select doesn’t use NOLOCK either.
Something like that:
with cte1 as
(select * from tab1 (nolock)),
cte2 as
(select * from cte1)
select * from cte2
or shall I write
with cte1 as
(select * from tab1 (nolock)),
cte2 as
(select * from cte1 (nolock))
select * from cte2 (nolock)
thanks
You don’t need the outer
nolockto avoid taking shared locks ontab1. You can easily verify this by setting up a SQL Profiler trace capturing the various events in thelockscategory, filtering on the spid of an SSMS connection and trying both versions.nolockis quite a dangerous setting though, are you aware of all of the possible downsides to using it (dirty reads, reading data twice or not at all)?