CREATE OR REPLACE FUNCTION seattleUpdate 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
25
26
27
28
29
30
31
32
33
34
CREATE OR REPLACE FUNCTION seattleUpdate()
RETURNS trigger AS $$
DECLARE
result RECORD;
BEGIN
SELECT count(*) as t_count, d.department_id as depID INTO result
FROM departments d, locations l, countries c
WHERE l.location_id=d.location_id AND
l.country_id=c.country_id AND
c.country_name='United States of America' AND
l.city='Seattle' AND
d.department_name=NEW.department_name
GROUP BY d.department_id;
if result.t_count > 0 then
UPDATE employees set
first_name=NEW.first_name,
last_name=NEW.last_name,
salary=NEW.salary,
department_id=result.depID
WHERE first_name = OLD.first_name AND
last_name = OLD.last_name AND
hire_date = OLD.hire_date;
RETURN NEW;
END IF;
RETURN NULL;
END;
$$ LANGUAGE PLPGSQL SECURITY DEFINER;
DROP TRIGGER seattle_employees_update ON seattle_employees;
CREATE TRIGGER seattle_employees_update INSTEAD OF update on
seattle_employees FOR EACH ROW EXECUTE
PROCEDURE seattleUpdate();