-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggersUpdate.sql
294 lines (286 loc) · 10.9 KB
/
triggersUpdate.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
Use `northwind`;
DROP TRIGGER IF EXISTS trigger_update_customers;
DELIMITER $$
CREATE TRIGGER trigger_update_customers
AFTER UPDATE ON `northwind`.customers
FOR EACH ROW
BEGIN
DECLARE id INT;
DECLARE op,cp,jt,ct,sp,cr VARCHAR(50);
DECLARE fln VARCHAR(100);
SET op = 'UPDATE',
id = new.id;
IF(new.company is null) THEN SET cp = "N/A";
ELSE SET cp = new.company;
END IF;
IF(new.first_name is null AND new.last_name is null) THEN SET fln = "N/A";
ELSE SET fln = concat(new.first_name,' ', new.last_name);
END IF;
IF(new.job_title is null) THEN SET jt = "N/A";
ELSE SET jt = new.job_title;
END IF;
IF(new.city is null) THEN SET ct = "N/A";
ELSE SET ct = new.city;
END IF;
IF(new.state_province is null) THEN SET sp = "N/A";
ELSE SET sp = new.state_province;
END IF;
IF(new.country_region is null) THEN SET cr = "N/A";
ELSE SET cr = new.country_region;
END IF;
INSERT INTO `trabalho-ar`.aud_dim_customer values(id,cp,fln,jt,ct,sp,cr,op);
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS trigger_update_employee;
DELIMITER $$
CREATE TRIGGER trigger_update_employee
AFTER UPDATE ON `northwind`.employees
FOR EACH ROW
BEGIN
DECLARE id INT;
DECLARE op,cp,jt,ct,sp,cr VARCHAR(50);
DECLARE fln VARCHAR(100);
SET op = 'UPDATE',
id = new.id;
IF(new.company is null) THEN SET cp = "N/A";
ELSE SET cp = new.company;
END IF;
IF(new.first_name is null AND new.last_name is null) THEN SET fln = "N/A";
ELSE SET fln = concat(new.first_name,' ', new.last_name);
END IF;
IF(new.job_title is null) THEN SET jt = "N/A";
ELSE SET jt = new.job_title;
END IF;
IF(new.city is null) THEN SET ct = "N/A";
ELSE SET ct = new.city;
END IF;
IF(new.state_province is null) THEN SET sp = "N/A";
ELSE SET sp = new.state_province;
END IF;
IF(new.country_region is null) THEN SET cr = "N/A";
ELSE SET cr = new.country_region;
END IF;
INSERT INTO `trabalho-ar`.aud_dim_employee values(id,cp,fln,jt,ct,sp,cr,op);
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS trigger_update_shipper;
DELIMITER $$
CREATE TRIGGER trigger_update_shipper
AFTER UPDATE ON `northwind`.shippers
FOR EACH ROW
BEGIN
DECLARE id INT;
DECLARE op,cp,jt,ct,sp,cr VARCHAR(50);
DECLARE fln VARCHAR(100);
SET op = 'UPDATE',
id = new.id;
IF(new.company is null) THEN SET cp = "N/A";
ELSE SET cp = new.company;
END IF;
IF(new.first_name is null AND new.last_name is null) THEN SET fln = "N/A";
ELSE SET fln = concat(new.first_name,' ', new.last_name);
END IF;
IF(new.job_title is null) THEN SET jt = "N/A";
ELSE SET jt = new.job_title;
END IF;
IF(new.city is null) THEN SET ct = "N/A";
ELSE SET ct = new.city;
END IF;
IF(new.state_province is null) THEN SET sp = "N/A";
ELSE SET sp = new.state_province;
END IF;
IF(new.country_region is null) THEN SET cr = "N/A";
ELSE SET cr = new.country_region;
END IF;
INSERT INTO `trabalho-ar`.aud_dim_shipper values(id,cp,fln,jt,ct,sp,cr,op);
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS trigger_update_supplier;
DELIMITER $$
CREATE TRIGGER trigger_update_supplier
AFTER UPDATE ON `northwind`.suppliers
FOR EACH ROW
BEGIN
DECLARE id INT;
DECLARE op,cp,jt,ct,sp VARCHAR(50);
DECLARE fln VARCHAR(100);
SET op = 'UPDATE',
id = new.id;
IF(new.company is null) THEN SET cp = "N/A";
ELSE SET cp = new.company;
END IF;
IF(new.first_name is null AND new.last_name is null) THEN SET fln = "N/A";
ELSE SET fln = concat(new.first_name,' ', new.last_name);
END IF;
IF(new.job_title is null) THEN SET jt = "N/A";
ELSE SET jt = new.job_title;
END IF;
IF(new.city is null) THEN SET ct = "N/A";
ELSE SET ct = new.city;
END IF;
IF(new.state_province is null) THEN SET sp = "N/A";
ELSE SET sp = new.state_province;
END IF;
INSERT INTO `trabalho-ar`.aud_dim_supplier values(id,cp,fln,jt,ct,sp,op);
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS trigger_update_products;
DELIMITER $$
CREATE TRIGGER trigger_update_products
AFTER UPDATE ON `northwind`.products
FOR EACH ROW
BEGIN
DECLARE id INT;
DECLARE nm,qpu,ct,op VARCHAR(50);
DECLARE sc,lp DECIMAL(19,4);
DECLARE dc TINYINT(1);
SET op= 'UPDATE',
id = new.id,
lp = new.list_price,
dc= new.discontinued;
IF(new.product_name is null) THEN SET nm = "N/A";
ELSE SET nm = new.product_name;
END IF;
IF(new.standard_cost is null) THEN SET sc = "N/A";
ELSE SET sc = new.standard_cost;
END IF;
IF(new.quantity_per_unit is null) THEN SET qpu = "N/A";
ELSE SET qpu = new.quantity_per_unit;
END IF;
IF(new.category is null) THEN SET ct = "N/A";
ELSE SET ct = new.category;
END IF;
INSERT INTO `trabalho-ar`.aud_dim_products VALUES(id,nm,sc,lp,qpu,dc,ct,op);
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS trigger_update_orders;
DELIMITER $$
CREATE TRIGGER trigger_update_orders
AFTER UPDATE ON `northwind`.order_details
FOR EACH ROW
BEGIN
DECLARE od,pd,sd,idc,idp,ids,ide INT;
DECLARE op VARCHAR(50);
DECLARE up,qt DECIMAL(18,4);
DECLARE newDate, last_date, nod,npd,nsd DATETIME;
SET nod = (SELECT O.order_date FROM northwind.orders AS O
WHERE new.order_id = O.id);
SET npd = (SELECT O.paid_date FROM northwind.orders AS O
WHERE new.order_id = O.id);
SET nsd = (SELECT O.shipped_date FROM northwind.orders AS O
WHERE new.order_id = O.id);
SET newDate = (SELECT GREATEST(nod,npd,nsd));
SET last_date = (SELECT max(data) FROM `trabalho-ar`.pre_dim_time);
IF(newDate>last_date) THEN CALL `trabalho-ar`.inc_datas_trigger(newDate,last_date);
END IF;
SET op = 'UPDATE',
up= new.unit_price,
qt = new.quantity,
od = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
WHERE DT.date = (SELECT O.order_date FROM northwind.orders AS O
WHERE new.order_id = O.id)),
pd = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
WHERE DT.date = (SELECT O.paid_date FROM northwind.orders AS O
WHERE new.order_id = O.id)),
sd = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
WHERE DT.date = (SELECT O.shipped_date FROM northwind.orders AS O
WHERE new.order_id = O.id)),
idc = (SELECT O.customer_id FROM northwind.orders AS O
WHERE new.order_id = O.id),
idp = new.product_id,
ids = (SELECT O.shipper_id FROM northwind.orders AS O
WHERE new.order_id = O.id),
ide = (SELECT O.employee_id FROM northwind.orders AS O
WHERE new.order_id = O.id);
IF(up is null) THEN SET up = -1.0;
END IF;
IF(qt is null) THEN SET up = -1.0;
END IF;
IF(od is null) THEN SET od = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
where DT.date = '0000-00-00');
END IF;
IF(pd is null) THEN SET pd = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
where DT.date = '0000-00-00');
END IF;
IF(sd is null) THEN SET sd = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
where DT.date = '0000-00-00');
END IF;
IF(idc is null) THEN SET idc = 0;
END IF;
IF(idp is null) THEN SET idp = 0;
END IF;
IF(ids is null) THEN SET ids = 0;
END IF;
IF(ide is null) THEN SET ide = 0;
END IF;
INSERT INTO `trabalho-ar`.pre_order_facts(unit_price,quantity,order_date,paid_date,shipped_date,id_dim_customer,
id_dim_products,id_dim_shipper,id_dim_employer,operation)
VALUES(up,qt,od,pd,sd,idc,idp,ids,ide,op);
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS trigger_update_purchase;
DELIMITER $$
CREATE TRIGGER trigger_update_purchase
AFTER UPDATE ON `northwind`.purchase_order_details
FOR EACH ROW
BEGIN
DECLARE sd,pd,cd,ad,idp,ide,ids INT;
DECLARE op VARCHAR(50);
DECLARE up,qt DECIMAL(18,4);
DECLARE newDate, last_date,nsd,npd,ncd,nad DATETIME;
SET nsd = (SELECT p.submitted_date FROM northwind.purchase_orders AS p
WHERE new.purchase_order_id = p.id);
SET npd = (SELECT p.payment_date FROM northwind.purchase_orders AS p
WHERE new.purchase_order_id = p.id);
SET ncd = (SELECT p.creation_date FROM northwind.purchase_orders AS p
WHERE new.purchase_order_id = p.id);
SET nad = (SELECT p.approved_date FROM northwind.purchase_orders AS p
WHERE new.purchase_order_id = p.id);
SET newDate = (SELECT GREATEST(nsd,npd,ncd,nad));
SET last_date = (SELECT max(data) FROM `trabalho-ar`.pre_dim_time);
IF(newDate>last_date) THEN CALL `trabalho-ar`.inc_datas_trigger(newDate,last_date);
END IF;
SET op = 'UPDATE',
up= new.unit_cost,
qt = new.quantity,
sd = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
WHERE DT.date = (SELECT p.submitted_date FROM northwind.purchase_orders AS p
WHERE new.purchase_order_id = p.id)),
pd = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
WHERE DT.date = (SELECT p.payment_date FROM northwind.purchase_orders AS p
WHERE new.purchase_order_id = p.id)),
cd = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
WHERE DT.date = (SELECT p.creation_date FROM northwind.purchase_orders AS p
WHERE new.purchase_order_id = p.id)),
ad = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
WHERE DT.date = (SELECT p.approved_date FROM northwind.purchase_orders AS p
WHERE new.purchase_order_id = p.id)),
idp = new.product_id,
ide = (SELECT p.created_by FROM northwind.purchase_orders AS p
WHERE new.purchase_order_id = p.id),
ids = (SELECT p.supplier_id FROM northwind.purchase_orders AS p
WHERE new.purchase_order_id = p.id);
IF(up is null) THEN SET up = -1.0;
END IF;
IF(sd is null) THEN SET sd = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
where DT.date = '0000-00-00');
END IF;
IF(pd is null) THEN SET pd = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
where DT.date = '0000-00-00');
END IF;
IF(cd is null) THEN SET cd = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
where DT.date = '0000-00-00');
END IF;
IF(ad is null) THEN SET ad = (SELECT DT.id_dim_time FROM trabalho.dim_time AS DT
where DT.date = '0000-00-00');
END IF;
IF(idp is null) THEN SET idp = 0;
END IF;
IF(ids is null) THEN SET ids = 0;
END IF;
IF(ide is null) THEN SET ide = 0;
END IF;
INSERT INTO `trabalho-ar`.pre_purchase_facts(unit_cost,quantity,submitted_date,payment_date,created_date,approved_date,id_dim_products,id_dim_employer,id_dim_supplier,operation)
VALUES(up,qt,sd,pd,cd,ad,idp,ide,ids,op);
END$$
DELIMITER ;