-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL Stored Procedure.sql
More file actions
242 lines (184 loc) · 5.61 KB
/
Copy pathSQL Stored Procedure.sql
File metadata and controls
242 lines (184 loc) · 5.61 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
-- StoredProcedure vs Python
-- Connection: network (server) connection is a disadvantage with python
-- Pre-compile: Databaseserver knows abouth the queries makes perparations like EXEC plan
-- Flexibility: Make use of various features of python
-- Version control: Better in Python
-- Complex Logic: Better in Python
/*
CREATE PROCEDURE [procedure_name] as
BEGIN
-- SQL STATEMENTS
END
EXEC [procedure_name]
*/
use salesdb;
-- Step 1: Write a Query
-- For US customers Find the total number of customers and the average score
select
count(*) totalcustomers,
round(avg(score),0) avgscore
from customers
where country='USA';
-- Step 2: Turning the Query into Stored Procedure
delimiter $$
CREATE PROCEDURE sp_country_avgscore()
BEGIN
select
count(*) totalcustomers,
round(avg(score),0) avgscore
from customers
where country='USA';
END $$
delimiter ;
call sp_country_avgscore;
-- PARAMETERS: Placeholders used to pass values as input from the user to the procedure ,
-- allowing dynamic data to be processed
-- For German cus find the total number of customers & the avg score
delimiter $$
create procedure sp_customer_summary_country(in sp_country varchar(50))
begin
select
count(*) totalcustomers,
round(avg(score),0) avgscore
from customers
where country=sp_country;
end $$
delimiter $$;
call sp_customer_summary_country('Germany');
-- MULTIPLE STATEMENTS
delimiter $$
create procedure sp_customer_summary_country(in sp_country varchar(50))
begin
select
count(*) totalcustomers,
round(avg(score),0) avgscore
from customers
where country=sp_country;
-- Find the total no. of orders and total sales
select
count(*) totalcustomers,
sum(sales) totalsales
from orders o
join customers c on o.customerid = c.customerid
where country=sp_country;
end $$
delimiter $$;
call sp_customer_summary_country('Germany');
-- VARIABLES: Placeholder used to store values to be used later in the procedure
-- Parameters pass values into a stored procedure or return values back to the caller
-- Variables temporarily store values and manipulate data during its EXEC
-- 3 steps: 1. Declare Variables 2. Assign values to the variables 3. UseVariables
delimiter $$
create procedure sp_customer_summary_country(in sp_country varchar(50))
begin
declare totalcustomers int;
declare avgscore float;
select
count(*) totalcustomers,
round(avg(score),0) avgscore
into totalcustomers, avgscore
from customers
where country=sp_country;
-- Total customers from germany : 2
-- Avg score from germany: 425
SELECT CONCAT('Total customers from ', sp_country, ' : ', cast(totalcustomers as char)) as declare_var1;
SELECT CONCAT('Avg score from ', sp_country, ' : ', cast(avgscore as char)) as declare_var2;
-- Find the total no. of orders and total sales
select
count(*) totalcustomers,
sum(sales) totalsales
from orders o
join customers c on o.customerid = c.customerid
where country=sp_country;
end $$
delimiter $$;
call sp_customer_summary_country('Germany');
-- CONTROL FLOW: IF - ELSE
delimiter $$
create procedure sp_customer_summary_country(in sp_country varchar(50))
begin
declare totalcustomers int;
declare avgscore float;
-- Prepare & Cleaup data
if exists(select 1 from customers where score is null and country = sp_country ) then
SELECT 'Updating NULL Score to 0' AS Message;
update customers set score=0 where score is null and country=sp_country;
else
SELECT 'No null scores found' AS Message;
end if;
-- Generating Reports
select
count(*) totalcustomers,
round(avg(score),0) avgscore
into totalcustomers, avgscore
from customers
where country=sp_country;
-- Total customers from germany : 2
-- Avg score from germany: 425
SELECT CONCAT('Total customers from ', sp_country, ' : ', cast(totalcustomers as char)) as declare_var1;
SELECT CONCAT('Avg score from ', sp_country, ' : ', cast(avgscore as char)) as declare_var2;
-- Find the total no. of orders and total sales
select
count(*) totalcustomers,
sum(sales) totalsales
from orders o
join customers c on o.customerid = c.customerid
where country=sp_country;
end $$
delimiter $$;
call sp_customer_summary_country('USA');
-- ERROR HANDLING - TRY_CATCH
/*
BEGIN TRY
-- SQL statements
END TRY
BEGIN CATCH
-- SQL statements
END CATCH
*/
-- It does not support in MySQL but supports in SQL server
-- EXCEPTION HANDLER
/*
DECLARE EXIT(CONTINUE) HANDLER FOR SQLEXCEPTION
BEGIN
select 'Error' as message
END
*/
delimiter $$
create procedure sp_customer_summary_country(in sp_country varchar(50))
begin
declare totalcustomers int;
declare avgscore float;
-- ===================================
-- Step 1: Prepare & Cleaup data
-- ===================================
if exists(select 1 from customers where score is null and country = sp_country ) then
SELECT 'Updating NULL Score to 0' AS Message;
update customers set score=0 where score is null and country=sp_country;
else
SELECT 'No null scores found' AS Message;
end if;
-- ==============================
-- Step 2:Generating Reports
-- ==============================
select
count(*) totalcustomers,
round(avg(score),0) avgscore
into totalcustomers, avgscore
from customers
where country=sp_country;
-- Total customers from germany : 2
-- Avg score from germany: 425
SELECT CONCAT('Total customers from ', sp_country, ' : ', cast(totalcustomers as char)) as declare_var1;
SELECT CONCAT('Avg score from ', sp_country, ' : ', cast(avgscore as char)) as declare_var2;
-- Find the total no. of orders and total sales
select
count(*) totalcustomers,
sum(sales) totalsales,
10/0
from orders o
join customers c on o.customerid = c.customerid
where country=sp_country;
end $$
delimiter $$;
call sp_customer_summary_country('USA');