简体   繁体   English

错误 1064 (42000):您的 SQL 语法有错误; 检查手册 mysql 存储过程错误

[英]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您的代码中有一些错误

  • DECLARE without a tyoe is nocht valid没有 tyoe 的 DECLARE 是无效的
  • SET are only possible after DECLARE SET 只能在 DECLARE 之后
  • and your CORSOR is cur not birthdate并且您的 CORSOR 不是生日

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.

相关问题 ERROR 1064 (42000):您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册 - ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version 错误1064(42000):您的SQL语法有错误; 查看与您的MySQL服务器版本相对应的手册以获取正确的语法 - 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 错误1064(42000):您的SQL语法有错误; 检查与您的MySQL服务器版本相对应的手册以获取正确的语法? - 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? ERROR 1064 (42000):您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册以了解要使用的正确语法 - 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 MySQL ERROR 1064 (42000):您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册 - MySQL ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version 1064(42000):您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法使用 - 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 MySQL:错误 1064 (42000):您的 SQL 语法有错误; - MySQL: ERROR 1064 (42000): You have an error in your SQL syntax; mysql ERROR 1064 (42000): 你的 SQL 语法有错误; - mysql ERROR 1064 (42000): You have an error in your SQL syntax; Python MySQL, 1064 (42000), “您的 SQL 语法有错误;...” - Python MySQL, 1064 (42000), "You have an error in your SQL syntax;..." 错误1064(42000):您的SQL语法有错误; 检查与您的MariaDB服务器版本错误相对应的手册 - ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM