CREATE OR REPLACE FUNCTION seattleDelete RETURNS trigger AS DECLARE re

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
CREATE OR REPLACE FUNCTION seattleDelete()
RETURNS trigger AS $$
DECLARE
result Boolean;
BEGIN
SELECT age(OLD.hire_date) < '21 years'::interval INTO result;
if result then
DELETE FROM employees
where first_name = OLD.first_name AND
last_name = OLD.last_name AND
hire_date = OLD.hire_date;
RETURN OLD;
END IF;
RETURN NULL;
END;
$$ LANGUAGE PLPGSQL SECURITY DEFINER;
DROP TRIGGER seattle_employees_delete ON seattle_employees;
CREATE TRIGGER seattle_employees_delete INSTEAD OF delete on
seattle_employees FOR EACH ROW EXECUTE
PROCEDURE seattleDelete();