forked from SeemaSP/shoppingjquerycart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16 - Transactions.sql
122 lines (107 loc) · 4.07 KB
/
16 - Transactions.sql
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
120
121
122
-- Demonstration 16 - Transactions
-- Step 1: Open a new query window to the TSQL database
USE TSQL;
GO
-- Step 2: Create a table to support the demonstrations
-- Clean up if the tables already exists
IF OBJECT_ID('dbo.SimpleOrderDetails','U') IS NOT NULL
DROP TABLE dbo.SimpleOrderDetails;
IF OBJECT_ID('dbo.SimpleOrders','U') IS NOT NULL
DROP TABLE dbo.SimpleOrders;
GO
CREATE TABLE dbo.SimpleOrders(
orderid int IDENTITY(1,1) NOT NULL PRIMARY KEY,
custid int NOT NULL FOREIGN KEY REFERENCES Sales.Customers(custid),
empid int NOT NULL FOREIGN KEY REFERENCES HR.Employees(empid),
orderdate datetime NOT NULL
);
GO
CREATE TABLE dbo.SimpleOrderDetails(
orderid int NOT NULL FOREIGN KEY REFERENCES dbo.SimpleOrders(orderid),
productid int NOT NULL FOREIGN KEY REFERENCES Production.Products(productid),
unitprice money NOT NULL,
qty smallint NOT NULL,
CONSTRAINT PK_OrderDetails PRIMARY KEY (orderid, productid)
);
GO
-- Step 3: Execute a multi-statement batch with error
-- NOTE: THIS STEP WILL CAUSE AN ERROR
BEGIN TRY
INSERT INTO dbo.SimpleOrders(custid, empid, orderdate) VALUES (68,9,'2006-07-12');
INSERT INTO dbo.SimpleOrders(custid, empid, orderdate) VALUES (88,3,'2006-07-15');
INSERT INTO dbo.SimpleOrderDetails(orderid,productid,unitprice,qty) VALUES (1, 2,15.20,20);
INSERT INTO dbo.SimpleOrderDetails(orderid,productid,unitprice,qty) VALUES (999,77,26.20,15);
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrNum, ERROR_MESSAGE() AS ErrMsg;
END CATCH;
-- Step 4: Show that even with exception handling,
-- partial success occurred and some rows were inserted
SELECT orderid, custid, empid, orderdate
FROM dbo.SimpleOrders;
SELECT orderid, productid, unitprice, qty
FROM dbo.SimpleOrderDetails;
-- Step N: Clean up demonstration tables
IF OBJECT_ID('dbo.SimpleOrderDetails','U') IS NOT NULL
DROP TABLE dbo.SimpleOrderDetails;
IF OBJECT_ID('dbo.SimpleOrders','U') IS NOT NULL
DROP TABLE dbo.SimpleOrders;
GO
CREATE TABLE dbo.SimpleOrders(
orderid int IDENTITY(1,1) NOT NULL PRIMARY KEY,
custid int NOT NULL FOREIGN KEY REFERENCES Sales.Customers(custid),
empid int NOT NULL FOREIGN KEY REFERENCES HR.Employees(empid),
orderdate datetime NOT NULL
);
GO
CREATE TABLE dbo.SimpleOrderDetails(
orderid int NOT NULL FOREIGN KEY REFERENCES dbo.SimpleOrders(orderid),
productid int NOT NULL FOREIGN KEY REFERENCES Production.Products(productid),
unitprice money NOT NULL,
qty smallint NOT NULL,
CONSTRAINT PK_OrderDetails PRIMARY KEY (orderid, productid)
);
GO
-- Step 3: Create a transaction to wrap around insertion statements
-- to create a single unit of work
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO dbo.SimpleOrders(custid, empid, orderdate) VALUES (68,9,'2006-07-12');
INSERT INTO dbo.SimpleOrderDetails(orderid,productid,unitprice,qty) VALUES (1, 2,15.20,20);
COMMIT TRANSACTION
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrNum, ERROR_MESSAGE() AS ErrMsg;
ROLLBACK TRANSACTION
END CATCH;
-- Step 4: Verify success
SELECT orderid, custid, empid, orderdate
FROM dbo.SimpleOrders;
SELECT orderid, productid, unitprice, qty
FROM dbo.SimpleOrderDetails;
-- Step 5: Clear out rows from previous tests
DELETE FROM dbo.SimpleOrderDetails;
GO
DELETE FROM dbo.SimpleOrders;
GO
--Step 6: Execute with errors in data to test transaction handling
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO dbo.SimpleOrders(custid, empid, orderdate) VALUES (68,9,'2006-07-15');
INSERT INTO dbo.SimpleOrderDetails(orderid,productid,unitprice,qty) VALUES (99, 2,15.20,20);
COMMIT TRANSACTION
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrNum, ERROR_MESSAGE() AS ErrMsg;
ROLLBACK TRANSACTION
END CATCH;
-- Step 7: Verify that no partial results remain
SELECT orderid, custid, empid, orderdate
FROM dbo.SimpleOrders;
SELECT orderid, productid, unitprice, qty
FROM dbo.SimpleOrderDetails;
-- Step N: Clean up demonstration tables
IF OBJECT_ID('dbo.SimpleOrderDetails','U') IS NOT NULL
DROP TABLE dbo.SimpleOrderDetails;
IF OBJECT_ID('dbo.SimpleOrders','U') IS NOT NULL
DROP TABLE dbo.SimpleOrders;