[英]ERROR 1064 (42000): You have an error in your SQL syntax; check the manual mysql stored procedure error
I am just trying mysql stored procedure but I am getting the following error:我只是在尝试 mysql 存储过程,但出现以下错误:
ERROR 1064 (42000): You have an error in your SQL syntax;
错误 1064 (42000):您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ';
检查与您的 MySQL 服务器版本相对应的手册,以获取在 '; 附近使用的正确语法;
DECLARE cur CURSOR for select birth_date from employees;
DECLARE cur CURSOR for selectbirth_date 来自员工;
' at line 3
' 在第 3 行
Code:代码:
delimiter //
create procedure getminmaxbirthdate(OUT minage date,OUT maxage date)
begin
DECLARE minsofar,maxsofar,curage,done;
DECLARE cur CURSOR for
select birth_date from employees;
set minsofar = 2040-12-12;
set maxsofar = 0;
set done = 0;
DECLARE continue handler for not found
set done = 1;
open cur;
while done = 0 do
fetch birthdate into curage;
if curage > maxsofar then
set maxsofar = curage;
end if;
if curage < minsofar then
set minsofar = curage;
end if;
end while;
close cur;
set minage = minsofar;
set maxage = maxsofar;
end //
delimiter ;
You have some bugs in your code您的代码中有一些错误
you must check the datatypes for minsofar,maxsofar,curage,done您必须检查minsofar、maxsofar、curage、done的数据类型
stored procedure:存储过程:
delimiter //
create procedure getminmaxbirthdate(OUT minage date,OUT maxage date)
begin
DECLARE minsofar,maxsofar,curage,done VARCHAR(10);
DECLARE cur CURSOR for
select birth_date from employees;
DECLARE continue handler for not found
set done = 1;
set minsofar = 2040-12-12;
set maxsofar = 0;
set done = 0;
open cur;
while done = 0 do
fetch cur into curage;
if curage > maxsofar then
set maxsofar = curage;
end if;
if curage < minsofar then
set minsofar = curage;
end if;
end while;
close cur;
set minage = minsofar;
set maxage = maxsofar;
end //
delimiter ;
Is there a reason you are not using a simple query?您是否有理由不使用简单查询?
select min(birth_date), max(birth_date)
from employee;
This could be incorporated into a stored procedure, but that seems superfluous.这可以合并到存储过程中,但这似乎是多余的。 Using a cursor is just a really, really, really bad practice -- even if you are learning SQL.
使用 cursor 只是一个非常非常非常糟糕的做法——即使您正在学习 SQL。 Cursors should be avoided, except in the situations where they are necessary -- such as calling a stored procedure or invoking dynamic SQL for each row.
应该避免使用游标,除非在必要的情况下——例如调用存储过程或为每一行调用动态 SQL。
Your code also seems to be mixing up "ages" and "dates".您的代码似乎也混淆了“年龄”和“日期”。 It is a bit unclear what you really want to accomplish.
有点不清楚你真正想要完成什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.