Created
August 9, 2011 10:29
-
-
Save jpstacey/1133723 to your computer and use it in GitHub Desktop.
Causing MySQL to raise an exception from a trigger
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
-- MySQL does not implement raising exceptions within triggers. Here's a hacky model for doing so. | |
-- This permits unwanted conditions to bubble up to the application layer, to be dealt with | |
-- Only use this during debugging. Don't rely on this in production code as it's a hack. | |
DELIMITER $$ | |
-- Procedure guaranteed to raise an exception | |
CREATE PROCEDURE die_horribly() SQL SECURITY INVOKER DETERMINISTIC | |
BEGIN | |
INSERT INTO does_not_exist (nid) VALUES (NULL); | |
END; | |
$$ | |
-- Procedure guaranteed to raise an exception | |
CREATE TRIGGER call_die BEFORE UPDATE ON node_revisions | |
FOR EACH ROW | |
BEGIN | |
IF NEW.status = 0 THEN CALL die_horribly(); | |
END IF; | |
END; | |
$$ | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment