Repository navigation
Expand file tree
/
Copy path22_Stored_Procedures.sql
More file actions
251 lines (216 loc) · 8.55 KB
/
Copy path22_Stored_Procedures.sql
File metadata and controls
251 lines (216 loc) · 8.55 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* ==============================================================================
SQL Stored Procedures
---------------------------------------------------------------------------------
This script shows how to work with stored procedures in SQL Server,
starting from basic implementations and advancing to more sophisticated
techniques.
Table of Contents:
1. BASIC STORED PROCEDURE
2. PARAMETERS IN STORED PROCEDURE
3. MULTIPLE QUERIES IN STORED PROCEDURE
4. VARIABLES IN STORED PROCEDURE
5. CONTROL FLOW IF/ELSE IN STORED PROCEDURE
6. ERROR HANDLING TRY/CATCH IN STORED PROCEDURE
=================================================================================
*/
-- Run this line first: every query in this script uses SalesDB
USE SalesDB;
GO
/* ==============================================================================
1. BASIC STORED PROCEDURE
============================================================================== */
-- Define the Stored Procedure
CREATE PROCEDURE GetCustomerSummary AS
BEGIN
SELECT
COUNT(*) AS TotalCustomers,
AVG(Score) AS AvgScore
FROM Sales.Customers
WHERE Country = 'USA';
END
GO
--Execute Stored Procedure
EXEC GetCustomerSummary;
GO
/* ==============================================================================
2. PARAMETERS IN STORED PROCEDURE
============================================================================== */
-- Edit the Stored Procedure
ALTER PROCEDURE GetCustomerSummary @Country NVARCHAR(50) = 'USA' AS
BEGIN
-- Reports: Summary from Customers and Orders
SELECT
COUNT(*) AS TotalCustomers,
AVG(Score) AS AvgScore
FROM Sales.Customers
WHERE Country = @Country;
END
GO
--Execute Stored Procedure
EXEC GetCustomerSummary @Country = 'Germany';
EXEC GetCustomerSummary @Country = 'USA';
EXEC GetCustomerSummary;
GO
/* ==============================================================================
3. MULTIPLE QUERIES IN STORED PROCEDURE
============================================================================== */
-- Edit the Stored Procedure
ALTER PROCEDURE GetCustomerSummary @Country NVARCHAR(50) = 'USA' AS
BEGIN
-- Query 1: Find the Total Nr. of Customers and the Average Score
SELECT
COUNT(*) AS TotalCustomers,
AVG(Score) AS AvgScore
FROM Sales.Customers
WHERE Country = @Country;
-- Query 2: Find the Total Nr. of Orders and Total Sales
SELECT
COUNT(OrderID) AS TotalOrders,
SUM(Sales) AS TotalSales
FROM Sales.Orders AS o
JOIN Sales.Customers AS c
ON c.CustomerID = o.CustomerID
WHERE c.Country = @Country;
END
GO
--Execute Stored Procedure
EXEC GetCustomerSummary @Country = 'Germany';
EXEC GetCustomerSummary @Country = 'USA';
EXEC GetCustomerSummary;
GO
/* ==============================================================================
4. VARIABLES IN STORED PROCEDURE
============================================================================== */
-- Edit the Stored Procedure
ALTER PROCEDURE GetCustomerSummary @Country NVARCHAR(50) = 'USA' AS
BEGIN
-- Declare Variables
DECLARE @TotalCustomers INT, @AvgScore FLOAT;
-- Query 1: Find the Total Nr. of Customers and the Average Score
SELECT
@TotalCustomers = COUNT(*),
@AvgScore = AVG(Score)
FROM Sales.Customers
WHERE Country = @Country;
PRINT('Total Customers from ' + @Country + ':' + CAST(@TotalCustomers AS NVARCHAR));
PRINT('Average Score from ' + @Country + ':' + CAST(@AvgScore AS NVARCHAR));
-- Query 2: Find the Total Nr. of Orders and Total Sales
SELECT
COUNT(OrderID) AS TotalOrders,
SUM(Sales) AS TotalSales
FROM Sales.Orders AS o
JOIN Sales.Customers AS c
ON c.CustomerID = o.CustomerID
WHERE c.Country = @Country;
END
GO
--Execute Stored Procedure
EXEC GetCustomerSummary @Country = 'Germany';
EXEC GetCustomerSummary @Country = 'USA';
EXEC GetCustomerSummary;
GO
/* ==============================================================================
5. CONTROL FLOW IF/ELSE IN STORED PROCEDURE
============================================================================== */
ALTER PROCEDURE GetCustomerSummary @Country NVARCHAR(50) = 'USA' AS
BEGIN
-- Declare Variables
DECLARE @TotalCustomers INT, @AvgScore FLOAT;
/* --------------------------------------------------------------------------
Prepare & Cleanup Data
-------------------------------------------------------------------------- */
IF EXISTS (SELECT 1 FROM Sales.Customers WHERE Score IS NULL AND Country = @Country)
BEGIN
PRINT('Updating NULL Scores to 0');
UPDATE Sales.Customers
SET Score = 0
WHERE Score IS NULL AND Country = @Country;
END
ELSE
BEGIN
PRINT('No NULL Scores found');
END;
/* --------------------------------------------------------------------------
Generating Reports
-------------------------------------------------------------------------- */
SELECT
@TotalCustomers = COUNT(*),
@AvgScore = AVG(Score)
FROM Sales.Customers
WHERE Country = @Country;
PRINT('Total Customers from ' + @Country + ':' + CAST(@TotalCustomers AS NVARCHAR));
PRINT('Average Score from ' + @Country + ':' + CAST(@AvgScore AS NVARCHAR));
SELECT
COUNT(OrderID) AS TotalOrders,
SUM(Sales) AS TotalSales,
1/0 AS FaultyCalculation -- Intentional error for demonstration
FROM Sales.Orders AS o
JOIN Sales.Customers AS c
ON c.CustomerID = o.CustomerID
WHERE c.Country = @Country;
END
GO
--Execute Stored Procedure
EXEC GetCustomerSummary @Country = 'Germany';
EXEC GetCustomerSummary @Country = 'USA';
EXEC GetCustomerSummary;
GO
/* ==============================================================================
6. ERROR HANDLING TRY/CATCH IN STORED PROCEDURE
============================================================================== */
ALTER PROCEDURE GetCustomerSummary @Country NVARCHAR(50) = 'USA' AS
BEGIN
BEGIN TRY
-- Declare Variables
DECLARE @TotalCustomers INT, @AvgScore FLOAT;
/* --------------------------------------------------------------------------
Prepare & Cleanup Data
-------------------------------------------------------------------------- */
IF EXISTS (SELECT 1 FROM Sales.Customers WHERE Score IS NULL AND Country = @Country)
BEGIN
PRINT('Updating NULL Scores to 0');
UPDATE Sales.Customers
SET Score = 0
WHERE Score IS NULL AND Country = @Country;
END
ELSE
BEGIN
PRINT('No NULL Scores found');
END;
/* --------------------------------------------------------------------------
Generating Reports
-------------------------------------------------------------------------- */
SELECT
@TotalCustomers = COUNT(*),
@AvgScore = AVG(Score)
FROM Sales.Customers
WHERE Country = @Country;
PRINT('Total Customers from ' + @Country + ':' + CAST(@TotalCustomers AS NVARCHAR));
PRINT('Average Score from ' + @Country + ':' + CAST(@AvgScore AS NVARCHAR));
SELECT
COUNT(OrderID) AS TotalOrders,
SUM(Sales) AS TotalSales,
1/0 AS FaultyCalculation -- Intentional error for demonstration
FROM Sales.Orders AS o
JOIN Sales.Customers AS c
ON c.CustomerID = o.CustomerID
WHERE c.Country = @Country;
END TRY
BEGIN CATCH
/* --------------------------------------------------------------------------
Error Handling
-------------------------------------------------------------------------- */
PRINT('An error occurred.');
PRINT('Error Message: ' + ERROR_MESSAGE());
PRINT('Error Number: ' + CAST(ERROR_NUMBER() AS NVARCHAR));
PRINT('Error Severity: ' + CAST(ERROR_SEVERITY() AS NVARCHAR));
PRINT('Error State: ' + CAST(ERROR_STATE() AS NVARCHAR));
PRINT('Error Line: ' + CAST(ERROR_LINE() AS NVARCHAR));
PRINT('Error Procedure: ' + ISNULL(ERROR_PROCEDURE(), 'N/A'));
END CATCH;
END
GO
--Execute Stored Procedure
EXEC GetCustomerSummary @Country = 'Germany';
EXEC GetCustomerSummary @Country = 'USA';
EXEC GetCustomerSummary;