-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39_Isolation_Levels.sql
More file actions
119 lines (97 loc) · 3.86 KB
/
39_Isolation_Levels.sql
File metadata and controls
119 lines (97 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**************************************************************
* SQL Server 2022 Isolation Levels Tutorial
* Description: This script demonstrates how to enable and use
* different transaction isolation levels in SQL Server,
* including Snapshot Isolation and other standard levels.
* It covers:
* - Enabling Snapshot Isolation and Read Committed Snapshot Isolation.
* - Example transactions using Snapshot Isolation.
* - Examples for Read Uncommitted, Read Committed, Repeatable Read,
* Serializable, and Snapshot isolation levels.
**************************************************************/
-------------------------------------------------
-- Region: 1. Enabling Snapshot Isolation
-------------------------------------------------
/*
Enable Snapshot Isolation and Read Committed Snapshot Isolation for the database.
Replace 'YourDatabaseName' with your actual database name.
*/
ALTER DATABASE YourDatabaseName
SET ALLOW_SNAPSHOT_ISOLATION ON;
GO
ALTER DATABASE YourDatabaseName
SET READ_COMMITTED_SNAPSHOT ON;
GO
-------------------------------------------------
-- Region: 2. Example Using Snapshot Isolation
-------------------------------------------------
/*
Snapshot Isolation provides a transaction with a consistent view of the data
as it existed at the start of the transaction, reducing blocking and deadlocks.
*/
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
BEGIN TRANSACTION;
-- Perform operations under snapshot isolation.
SELECT * FROM YourTable
WHERE SomeColumn = 'SomeValue';
COMMIT TRANSACTION;
GO
-------------------------------------------------
-- Region: 3. Isolation Levels Examples
-------------------------------------------------
/*
SQL Server supports several isolation levels to control the visibility of changes made by other transactions.
*/
/*-------------------------------------------------------------
3.1 Read Uncommitted:
Allows dirty reads (reads uncommitted changes).
-------------------------------------------------------------*/
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
BEGIN TRANSACTION;
-- Perform operations (may return uncommitted data)
SELECT * FROM YourTable;
COMMIT TRANSACTION;
GO
/*-------------------------------------------------------------
3.2 Read Committed:
Default isolation level. Prevents dirty reads by only reading committed data.
-------------------------------------------------------------*/
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
BEGIN TRANSACTION;
-- Perform operations
SELECT * FROM YourTable;
COMMIT TRANSACTION;
GO
/*-------------------------------------------------------------
3.3 Repeatable Read:
Prevents dirty and non-repeatable reads by holding shared locks until the transaction completes.
-------------------------------------------------------------*/
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN TRANSACTION;
-- Perform operations
SELECT * FROM YourTable;
COMMIT TRANSACTION;
GO
/*-------------------------------------------------------------
3.4 Serializable:
The strictest isolation level. Prevents dirty, non-repeatable, and phantom reads by holding range locks.
-------------------------------------------------------------*/
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRANSACTION;
-- Perform operations
SELECT * FROM YourTable;
COMMIT TRANSACTION;
GO
/*-------------------------------------------------------------
3.5 Snapshot (revisited):
Provides a consistent view of data as it existed at the start of the transaction.
-------------------------------------------------------------*/
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
BEGIN TRANSACTION;
-- Perform operations
SELECT * FROM YourTable;
COMMIT TRANSACTION;
GO
-------------------------------------------------
-- End of Script
-------------------------------------------------