I need help with a stored procedure or two.
My stored procedures are supposed to check if a certain record exists. If it does exist, then I select everything from that row, as well as a value for the exit status of the SP. If the record doesn't exist, it selects an error message as the exit status.
I've recreated the SP (while changing field names) and restructured the table, just to get straight to the point.
The table I'm working with looks something like this:
Expand|Select|Wrap|Line Numbers
- DROP TABLE IF EXISTS `test_table`;
- CREATE TABLE `test_table` (
- `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- `name` VARCHAR(32));
As for the stored procedures, here is the first one:
Expand|Select|Wrap|Line Numbers
- DROP PROCEDURE IF EXISTS `test_sp2`;
- DELIMITER //
- CREATE PROCEDURE `test_sp2` (IN `p_id` INT)
- BEGIN
- SELECT @num := COUNT(`id`) FROM `test` WHERE `id` = `p_id`;
- IF @num > 0 THEN
- SELECT * FROM `test` WHERE `id` = `p_id`;
- SELECT 0 AS `status`; -- 'Return value,' to be used by the app. 0 if SP executed as desired, otherwise it will contain an error message
- ELSE
- SELECT 'Record does not exist.' AS `status`;
- END IF;
- END;
- //
- DELIMITER ;
Then here is the second SP, which I tried to rewrite with the IF EXISTS statement, but still no luck.
Expand|Select|Wrap|Line Numbers
- DELIMITER //
- CREATE PROCEDURE `test_sp` (IN `p_id` INT)
- BEGIN
- IF EXISTS(SELECT @num := COUNT(`id`) FROM `test` WHERE `id` = `p_id`) THEN
- SELECT * FROM `test` WHERE `id` = `p_id`;
- SELECT 0 AS `status`; -- 'Return value,' to be used by the app. 0 if SP executed as desired, otherwise it will contain an error message
- ELSE
- SELECT 'Record does not exist.' AS `status`;
- END IF;
- END;
- //
- DELIMITER ;
The error I get (for either SP) is:
Expand|Select|Wrap|Line Numbers
- ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM `test` WHERE `id` = `p_id`;
I've tried completely stripping down the SP, and it looks like the problem lies in the first SELECT statement.
What baffles me is that these were working just the other day, and now they're not.
I'm currently running MySQL 5.0.51a.
Any help or advice is greatly appreciated. If anyone needs more information out of me regarding this, I'd be glad to answer any questions.