-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15. Triggers and Transactions.sql
108 lines (81 loc) · 2.15 KB
/
15. Triggers and 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
CREATE DATABASE SoftUniBank
USE SoftUniBank
CREATE TABLE Accounts
(
AccountId INT PRIMARY KEY IDENTITY,
[Name] NVARCHAR(20) NOT NULL,
[Balance] DEC(18,4)
)
INSERT INTO Accounts ([Name], Balance)
VALUES ('Pesho', 100),
('Kiril', 50)
CREATE PROCEDURE usp_SendMoney @senderAccountId INT,
@recieverAccountId INT, @amount DECIMAL(15,2)
AS
BEGIN TRANSACTION
DECLARE @senderAccount INT = (SELECT AccountId FROM Accounts WHERE AccountId = @senderAccountId)
DECLARE @recieverAccount INT = (SELECT AccountId FROM Accounts WHERE AccountId = @recieverAccountId)
IF(@senderAccount IS NULL OR @recieverAccountId IS NULL)
BEGIN
ROLLBACK
RAISERROR('Account doesn''t exist!', 16, 1)
RETURN
END
DECLARE @currentAmount DECIMAL(15,2) = (SELECT Balance FROM Accounts WHERE AccountId = @senderAccountId )
IF (@currentAmount - @amount < 0)
BEGIN
ROLLBACK
RAISERROR('Insufficient funds!', 16, 2)
RETURN
END
UPDATE Accounts
SET Balance -= @amount
WHERE AccountId = @senderAccountId
UPDATE Accounts
SET Balance += @amount
WHERE AccountId = @recieverAccountId
COMMIT
SELECT
*
FROM Accounts
EXEC usp_SendMoney 1, 2, 97
--
CREATE TRIGGER tr_TownsUpdate ON Towns FOR UPDATE
AS
IF(EXISTS(
SELECT * FROM inserted
WHERE [Name] IS NULL OR LEN([Name]) = 0))
BEGIN
RAISERROR('Town Name can''not be empty', 16, 1)
ROLLBACK
RETURN
END
UPDATE Towns SET Name = '' WHERE TownID = 1
SELECT * FROM Towns
--
CREATE DATABASE AnotherBank
USE AnotherBank
DROP TABLE Accounts
CREATE TABLE Accounts
(
Username VARCHAR(10) NOT NULL PRIMARY KEY,
[Password] VARCHAR(20) NOT NULL,
Active CHAR(1) NOT NULL DEFAULT 'Y'
)
INSERT INTO Accounts(Username, [Password], Active)
VALUES('Pesho', '123456', 'Y'),
('Gosho', '123456', 'N')
CREATE TRIGGER tr_AccountDelete ON Accounts
INSTEAD OF DELETE
AS
UPDATE a SET Active = 'N'
FROM Accounts AS a
JOIN DELETED AS d ON d.Username = a.Username
WHERE a.Active = 'Y'
SELECT * FROM Accounts
DELETE FROM Accounts WHERE Accounts.Username = 'Pesho'
SELECT
*
FROM Accounts AS a
JOIN DELETED AS d ON d.Username = a.Username
--2:40