This question is an extension of a previous question that I asked. Table Locking When using Unions
With the same query we are using that is causing blocking the various queries used in these UNIONS
are CTE queries.
For example:
CREATE TABLE #TempTable (
[Columns]
)
WITH CTE1 (Columns)
(
Select [Columns] FROM [Tables] WHERE [WHERE-Condition]
),
CTE2 (Columns)
(
Select [Columns] FROM [Tables] WHERE [WHERE-Condition]
),
CTE3 (Columns)
(
Select [Columns] FROM [Tables] WHERE [WHERE-Condition]
),
CTE4 (Columns)
(
Select [Columns] FROM [Tables] WHERE [WHERE-Condition]
),
CTE5 (Columns)
(
Select [Columns] FROM [Tables] WHERE [WHERE-Condition]
),
CTE6 (Columns)
(
Select [Columns] FROM [Tables] WHERE [WHERE-Condition]
),
CTE7 (Columns)
(
Select [Columns] FROM [Tables] WHERE [WHERE-Condition]
)
INSERT INTO #TempTable
SELECT * FROM CTE1
UNION
SELECT * FROM CTE2
UNION
SELECT * FROM CTE3
UNION
SELECT * FROM CTE4
UNION
SELECT * FROM CTE5
UNION
SELECT * FROM CTE6 WHERE [Where-Condition]
UNION
SELECT * FROM CTE7
Assuming this particular scenario (and we do not have any explicit transactions declared) does the physical table locking structure change, because we are using CTE queries?
When the SELECT
for CTE1 is finished as part of the UNION
, are the physical tables still locked or are the locks still released?
Based on the answer from my previous question, I was under the impression that in the context of a UNION
when the SELECT
for one of the various SELECT
statements completes the tables are released. Is that incorrect?
We have not modified the isolation level for this transaction. As I understand it, that means we are using READ_COMMITTED
. The question is still applicable with just 2 SELECT
statements. I am trying to get an understanding on how this functionality works. The particular query I am working with uses 7, which is the only reason I specified 7.