Created
August 31, 2016 14:29
-
-
Save aaizemberg/6b00f6a7f4c1927fb102015c5f8d693b to your computer and use it in GitHub Desktop.
no dejando insertar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE emp ( | |
empname text, | |
salary integer, | |
last_date timestamp, | |
last_user text | |
); | |
CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$ | |
BEGIN | |
-- Check that empname and salary are given | |
IF NEW.empname IS NULL THEN | |
RAISE EXCEPTION 'empname cannot be null'; | |
END IF; | |
IF NEW.salary IS NULL THEN | |
RAISE EXCEPTION '% cannot have null salary', NEW.empname; | |
END IF; | |
-- Who works for us when she must pay for it? | |
IF NEW.salary < 0 THEN | |
RAISE EXCEPTION '% cannot have a negative salary', NEW.empname; | |
END IF; | |
-- Remember who changed the payroll when | |
NEW.last_date := current_timestamp; | |
NEW.last_user := current_user; | |
RETURN NEW; | |
END; | |
$emp_stamp$ LANGUAGE plpgsql; | |
CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp | |
FOR EACH ROW EXECUTE PROCEDURE emp_stamp(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment