-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrigger_vendas.sql
More file actions
33 lines (28 loc) · 889 Bytes
/
Copy pathtrigger_vendas.sql
File metadata and controls
33 lines (28 loc) · 889 Bytes
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
DELIMITER $$
create TRIGGER tri_vendas_ai
after insert on comivenda
FOR EACH row
begin
declare ITENS_V_TOTAL float(10,2) default 0;
declare TOTAL_V_ITEM float(10,2);
declare ITEM_TOTAL float(10,2) default 0;
declare ITEM_QUANTIDADE int default 0;
declare LOOPEND int default 0;
declare busca_itens cursor FOR
select n_valoivenda, n_qtdeivenda
from comivenda
where n_numevenda = NEW.n_numevenda;
declare CONTINUE HANDLER FOR SQLSTATE '02000' set LOOPEND = 1;
open busca_itens;
itens : loop
if LOOPEND = 1 then
LEAVE itens;
end if;
FETCH busca_itens into ITEM_TOTAL, ITEM_QUANTIDADE;
set ITENS_V_TOTAL = ITENS_V_TOTAL + (ITEM_TOTAL * ITEM_QUANTIDADE);
end loop itens;
close busca_itens;
update comvenda set n_totavenda = ITENS_V_TOTAL - (NEW.n_valoivenda * NEW.n_qtdeivenda)
where n_numevenda = NEW.n_numevenda;
end $$
DELIMITER ;