Inserting a Row into MyTable and Rolling Back the Transaction : TRANSACTION « Transaction « SQL Server / T-SQL Tutorial






4>
5>
6> CREATE TABLE MyTable (
7>  key_col int NOT NULL IDENTITY (1,1),
8>  abc     char(1) NOT NULL
9> )
10> INSERT INTO MyTable VALUES ('a')
11> INSERT INTO MyTable VALUES ('b')
12> INSERT INTO MyTable VALUES ('c')
13> SELECT * FROM MyTable ORDER BY key_col
14>
15> BEGIN TRAN
16>  INSERT INTO MyTable VALUES ('e')
17> ROLLBACK TRAN
18> INSERT INTO MyTable VALUES ('f')
19> SELECT
20>  *
21> FROM
22>  MyTable
23> ORDER BY
24>  key_col
25>
26>
27> drop table MyTable
28> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)
key_col     abc
----------- ---
          1 a
          2 b
          3 c

(1 rows affected)

(1 rows affected)
key_col     abc
----------- ---
          1 a
          2 b
          3 c
          5 f

(4 rows affected)
1>








23.1.TRANSACTION
23.1.1.A transaction is bound by the ACID test. ACID stands for Atomicity, Consistency, Isolation (or Independence), and Durability:
23.1.2.Explicit Transaction Commands
23.1.3.Using Transactions
23.1.4.BEGIN TRANSACTION
23.1.5.Using Explicit Transactions
23.1.6.Forcing an exclusive table lock.
23.1.7.Transaction spread across batches:
23.1.8.Exception before transaction committing
23.1.9.statements coded as a transaction
23.1.10.A script with nested transactions
23.1.11.One batch that contains two transactions
23.1.12.What happens with stored proc transactions and exceptions?
23.1.13.Declare variable in a transaction
23.1.14.Inserting a Row into MyTable and Rolling Back the Transaction