someguy321 Posted October 20, 2010 Share Posted October 20, 2010 I'm using MySQL 5.1.41-3 (with phpMyAdmin) and the below stored procedure throws a syntax error about the "DECLARE EXIT HANDLER" line. If I remove that, it goes through fine. (NOTE: I know this is a bad proc, it's just for testing) (I set the delimiter to //) DROP PROCEDURE `spTest`// CREATE DEFINER=`root`@`localhost` PROCEDURE `spTest` ( IN _name varchar(255), IN _age int ) BEGIN DECLARE statusCode int; DECLARE statusMessage varchar(255); SET statusCode = 0; SET statusMessage = 'OK'; #THIS THROWS SYNTAX ERROR! DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN ROLLBACK; SELECT status as -1, statusMessage as "Unknown Error Occurred."; call cleanup( "exception handler called in spTest"); END; IF (SELECT name FROM Test WHERE name = _name) IS NOT NULL THEN SET statusMessage = "name already exists"; SET statusCode = 1; ELSEIF (SELECT age FROM Test WHERE age = _age) IS NOT NULL THEN SET statusMessage = "age already exists"; SET statusCode = 2; ELSE INSERT INTO Test (name, age) VALUES(_name, _age); END IF; SELECT statusCode as "status", statusMessage as "message"; END // Quote Link to comment https://forums.phpfreaks.com/topic/216420-help-unable-to-declare-exit-handler-in-stored-proc/ Share on other sites More sharing options...
someguy321 Posted October 21, 2010 Author Share Posted October 21, 2010 Anyone have any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/216420-help-unable-to-declare-exit-handler-in-stored-proc/#findComment-1125021 Share on other sites More sharing options...
fenway Posted October 23, 2010 Share Posted October 23, 2010 You know, if you're going to post your question elsewhere, at least have the courtesy to post the solution when you find it. Quote Link to comment https://forums.phpfreaks.com/topic/216420-help-unable-to-declare-exit-handler-in-stored-proc/#findComment-1125636 Share on other sites More sharing options...
someguy321 Posted November 12, 2010 Author Share Posted November 12, 2010 You know, if you're going to post your question elsewhere, at least have the courtesy to post the solution when you find it. Yep, sorry about that! For anyone facing this issue: In any block, DECLARE must precede all other code. Quote Link to comment https://forums.phpfreaks.com/topic/216420-help-unable-to-declare-exit-handler-in-stored-proc/#findComment-1133636 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.