apologies is this is a very simple question but I can’t seem to find an answer anywhere..
When running a query using the UNION ALL query (as shown below) does SQL ensure that the locks are placed on both tables prior to data acquisition or is it the case that the locks are placed independently on each table, as appears to be the case from looking at the execution plan.
select i from T1 with (updlock, holdlock)
UNION ALL
select i from T2 with (updlock, holdlock)
A updlock will be taken on each row, from each table, as it is read. Depending on the size of the tables, you may see escalation. If you want to force locks on both tables prior to any data being read, specify the tablock hint.
Fire up Profiler, create an empty trace definition, filter to your test database and add just Lock:Acquired, Lock:Released events and you can see the sequence.