MySQL server stored procedure syntax error

半腔热情 提交于 2019-12-29 09:21:27

问题


I am having some trouble with creating a Stored Procedure for a MySQL database.

Here is a Select statement that works:

use canningi_db_person_cdtest;
SELECT *
FROM pet
WHERE name = 'Puffball';

Here is my Stored Procedure that does not work:

use canningi_db_person_cdtest;
CREATE PROCEDURE GetAllPets() 
BEGIN 
SELECT *
FROM pet
WHERE name = 'Puffball'; 
END

I am getting the following error:

#1064 - 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 '' at line 5

How can I get this working?

EDIT

How do I call this Stored Procedure? I have tried this with no result:

use canningi_db_person_cdtest;
CALL GetAllPets();

This is the error:

#1312 - PROCEDURE canningi_db_person_cdtest.GetAllPets can't return a result set in the given context 

回答1:


Add a delimiter to end your procedure. Example:

use canningi_db_person_cdtest;

DELIMITER //

CREATE PROCEDURE GetAllPets() 
BEGIN 
    SELECT *
    FROM pet
    WHERE name = 'Puffball'; 
END//

If you need more information, please refer to http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html




回答2:


In this particular case, since you have only one statement in your procedure, you don't need to change DELIMITER nor use BEGIN...END block.

You procedure might look like this

CREATE PROCEDURE GetAllPets() 
  SELECT *
    FROM pet
   WHERE name = 'Puffball'; 

And use it like this

CALL GetAllPets();

Here is SQLFiddle demo



来源:https://stackoverflow.com/questions/19062150/mysql-server-stored-procedure-syntax-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!