I am issuing a single SQL query consisting of multiple SELECTs grouped using UNION:
SELECT *
FROM employee
LEFT JOIN department
ON emplo
Using UNION
will remove any duplicate records that may be returned from either of the unioned queries, so not exactly atomic. Use UNION ALL
if you want all records from all unioned queries. UNION ALL
can be much faster that UNION
also.
Yes the statement is atomic but yes the data can change between the 2 reads.
Read Committed
only guarantees that you don't read dirty data it promises nothing else about consistency of reads for that you would need a higher isolation level.
As you said that you would accept a SQL Server Example...
(Assumes under pessimistic read committed isolation level)
CREATE TABLE employee
(
name VARCHAR(50),
DepartmentID INT
)
CREATE TABLE department
(
DepartmentID INT
)
INSERT INTO department VALUES (1)
INSERT INTO employee VALUES ('bob',1)
declare @employee TABLE
(
name VARCHAR(50),
DepartmentID INT
)
WHILE ((SELECT COUNT(*) FROM @employee) < 2)
BEGIN
DELETE FROM @employee
INSERT INTO @employee
SELECT employee.*
FROM employee
LEFT JOIN department
ON employee.DepartmentID = department.DepartmentID
UNION
SELECT employee.*
FROM employee
RIGHT JOIN department
ON employee.DepartmentID = department.DepartmentID
END;
SELECT * FROM @employee
while (1=1)
UPDATE employee SET name = CASE WHEN name = 'bob' THEN 'bill' else 'bob' END
Now go back to connection 1
name DepartmentID
-------------------------------------------------- ------------
bill 1
bob 1
(Remember to switch back to Connection 2 to kill it!)
The specific documentation covering this READ COMMITED
behaviour is here
The shared lock type determines when it will be released. Row locks are released before the next row is processed. Page locks are released when the next page is read, and table locks are released when the statement finishes.
EDIT: Note that my answer is incorrect but I do not want to delete it because I think it links to good questions and has good comments.
Every individual transaction is atomic.
A UNION
using multiple subqueries is a single T-SQL command, a single transaction, and will be atomic.
This is, in part, a reason to avoid inefficient queries (or sprocs, for that matter) as their atomic nature can delay other transactions.
EDIT: Please see this question for more interesting information on atomicity of subqueries
Is update with nested select atomic operation?
EDIT: Apparently I am wrong.
This is a good discussion on the topic: Atomic UPSERT in SQL Server 2005 where Remus poses a good example. Sorry for doubting you, Martin....