-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggers.sql
85 lines (72 loc) · 1.93 KB
/
triggers.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
CREATE TRIGGER remover_quesito
BEFORE DELETE ON quesito FOR EACH ROW
EXECUTE PROCEDURE remover_quesito_q1();
CREATE OR REPLACE FUNCTION remover_quesito_q1()
RETURNS TRIGGER AS $$
BEGIN
DELETE FROM nota
WHERE qid = OLD.qid;
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
-----------------------------------------------
CREATE TRIGGER min_musica_nota_up
BEFORE UPDATE ON nota FOR EACH ROW
EXECUTE PROCEDURE min_musica_nota();
CREATE OR REPLACE min_musica_nota()
RETURNS TRIGGER AS $$
BEGIN
IF (NEW.qid IN (
SELECT * FROM quesito q
WHERE NEW.qid = q.qid
AND q.qdescricao = 'musica'
AND NEW.nota <= 2))
THEN
RETURN NULL;
ELSE
RETURN NEW;
END IF;
END;
$$ language plpgsql;
-------------------------------------------
CREATE OR REPLACE FUNCTION calc_nota_final (escola INT, quesito INT)
RETURNS VOID AS $$
DECLARE
julgadores cursor IS (SELECT j.jid
FROM julgador j)
EXCEPT
(SELECT j2.jid
FROM julgador j2, nota n
WHERE n.qid = quesito
AND n.eid = escola
AND n.jid = j2.jid);
um_julgador INT;
nota_minina REAL;
BEGIN
SELECT MIN (n.nota) INTO nota_minina
FROM nota n
WHERE n.eid = escola;
OPEN julgadores;
FETCH julgadores INTO um_julgador;
LOOP
EXIT WHEN NOT FOUND;
INSE
-----------------------------------------------------
CREATE OR REPLACE FUNCTION min_max()
RETURNS SETOF RECORD AS $$
BEGIN
EXECUTE QUERY SELECT n.nid, min(n.nota), max(n.nota)
FROM nota n
GROUP BY n.eid;
RETURN;
END;
$$ language plpgsql;
SELECT min_max();
CREATE FUNCTION blog_get_pessoas()
RETURNS SETOF RECORD AS $$
BEGIN
RETURN QUERY SELECT id_pessoa, nome, sobrenome, sexo, cpf
FROM pessoa_fisica;
RETURN;
END;
$$ LANGUAGE plpgsql;