-
Notifications
You must be signed in to change notification settings - Fork 2
/
Practices.sql
142 lines (116 loc) · 3.56 KB
/
Practices.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
-- Add a new order and return it details
-- Stored Procedure
CREATE Procedure InsertOrder
(
@CustomerID NCHAR(5),
@EmployeeID INT,
@OrderDate DATETIME,
@ShipName NVARCHAR(40),
@ShipCity NVARCHAR(15),
@ShipAddress NVARCHAR(60)
)
as
BEGIN
BEGIN TRY
BEGIN TRANSACTION;
DECLARE @InsertedOrders TABLE (
OrderID INT,
CustomerID NCHAR(5),
EmployeeID INT,
OrderDate DATETIME,
ShipName NVARCHAR(40),
ShipCity NVARCHAR(15),
ShipAddress NVARCHAR(60)
);
INSERT INTO Orders (CustomerID, EmployeeID, OrderDate, ShipName, ShipCity, ShipAddress)
OUTPUT INSERTED.OrderID, INSERTED.CustomerID, INSERTED.EmployeeID, INSERTED.OrderDate, INSERTED.ShipName, INSERTED.ShipCity, INSERTED.ShipAddress
INTO @InsertedOrders
VALUES(@CustomerID, @EmployeeID, @OrderDate, @ShipName, @ShipCity, @ShipAddress)
COMMIT;
SELECT * FROM @InsertedOrders;
END TRY
BEGIN CATCH
ROLLBACK;
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_STATE() AS ErrorState,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
END;
-- Function
CREATE FUNCTION GetOrder(@OrderID INT)
RETURNS TABLE
AS
RETURN
(
SELECT O.OrderID, O.CustomerID, O.EmployeeID, O.OrderDate, O.ShipName, O.ShipAddress, O.ShipCity
FROM Orders as O
WHERE O.OrderID = @OrderID
);
-- TEST
EXEC InsertOrder 'ALFKI', 1, '2023-07-07', 'Alfreds Futterkiste', 'Obere Str. 57', 'Berlin'
SELECT * FROM GetOrderDetails(11078)
-- Add new customer and check product stock
-- Stored Procedure
CREATE PROCEDURE InsertCustomer
(
@CustomerID NCHAR(5),
@CompanyName NVARCHAR(40),
@ContactName NVARCHAR(30),
@ContactTitle NVARCHAR(30),
@Address NVARCHAR(60),
@City NVARCHAR(15),
@Region NVARCHAR(15),
@PostalCode NVARCHAR(10),
@Country NVARCHAR(15),
@Phone NVARCHAR(24),
@Fax NVARCHAR(24)
)
as
BEGIN
BEGIN TRY
BEGIN TRANSACTION;
INSERT INTO Customers (CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax)
VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax)
COMMIT;
END TRY
BEGIN CATCH
ROLLBACK;
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_STATE() AS ErrorState,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
END;
-- Trigger
CREATE TRIGGER AfterInsertCustomer
ON Customers
AFTER INSERT
AS
BEGIN
DECLARE @CompanyName nvarchar(40);
DECLARE @CustomerID nchar(5);
SELECT @CompanyName = i.CompanyName, @CustomerID = i.CustomerID
FROM inserted i;
PRINT 'New customer has been added: ' + @CompanyName + ' with ID: ' + @CustomerID;
END;
-- Function
CREATE FUNCTION CheckStock(@ProductID INT)
RETURNS INT
as
BEGIN
DECLARE @UnitsInStock INT
SELECT @UnitsInStock = UnitsInStock - UnitsOnOrder
FROM Products
WHERE ProductID = @ProductID;
RETURN @UnitsInStock;
END;
-- TEST
EXEC InsertCustomer 'NEWCO', 'New Company', 'John Doe', 'CEO', '123 Main St.', 'New City', 'Region', '12345', 'Country', '123-456-7890', '123-456-7891';
SELECT dbo.CheckStock(1);