· 6 years ago · Sep 12, 2019, 04:58 PM
1drop table if exists ArchivoTerapie;
2drop table if exists tracciadump;
3
4create table ArchivioTerapie (
5 Paziente char(16) not null,
6 Patologia char(100) not null,
7 Farmaco char(100) not null,
8 Annoinizio integer not null,
9 Durata integer not null,
10 TotaleCompresse integer not null,
11 primary key(Paziente, Patologia , Farmaco)
12) engine = InnoDB;
13
14create table tracciadump(
15 DataEvento date not null,
16 terapie integer not null,
17 primary key(dataevento)
18)engine = InnoDB;
19
20drop procedure if exists dump_therapies;
21drop event if exists aggiornatraccia;
22
23
24delimiter $$
25create procedure dump_therapies()
26begin
27 declare pazientes char(16);
28 declare patologias char(100);
29 declare farmacos char(100);
30 declare anno integer;
31 declare durata integer;
32 declare assunte integer;
33 declare cur cursor for select paziente , patologia , farmaco , year(datainizioterapia) , datediff(datafineterapia, datainizioterapia) , datediff(datafineterapia, datainizioterapia)*posologia
34 from terapia
35 where datafineterapia < current_date - interval 6 month;
36 declare finito integer default 0;
37
38 declare continue handler
39 for not found
40 set finito = 1;
41
42 open cur;
43 scan : loop
44
45 fetch cur into pazientes , patologias , farmacos , anno , durata , assunte;
46
47 if finito = 1 then
48 leave scan;
49 end if;
50
51 insert into ArchivioTerapie values (
52 pazientes ,
53 patologias ,
54 farmacos ,
55 anno ,
56 durata ,
57 assunte);
58end loop;
59end $$
60delimiter ;
61
62
63delimiter $$
64create event aggiornatraccia
65on schedule every 1 month
66do
67begin
68 call dump_therapies();
69 set @eliminate = (select count(*) from terapia where datafineterapia < current_date - interval 6 month);
70 delete from terapia where datafineterapia < current_date - interval 6 month;
71
72 truncate tracciadump;
73
74 insert into tracciadump values(
75 current_date,
76 @eliminate);
77
78end $$
79delimiter ;