mysql存储过程语法错误1064_MySQL存储过程(SQL执行错误#1064)

bd96500e110b49cbb3cd949968f18be7.png

CREATE PROCEDURE get_scheduledMsg()

BEGIN

DECLARE _dateformat varchar(100);

SET _dateformat = SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i');

SELECT * FROM studentdetails WHERE classCode IS IN (SELECT class FROM messagedetails WHERE isStatus ='1' AND datecreated = _dateformat)

AND sectionCode IS IN (SELECT class FROM messagedetails WHERE isStatus ='1' AND datecreated = _dateformat);

END;

SQL execution error #1064. Response from database:

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 'SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i');

SELECT * FROM studentdetails WHERE' at line 4

解决方案Firstly you appear to be using a system variable instead of a local variable ... that should be @dateformat

Also, depending on the version of MySQL you don't need to declare local variables - MySQL will declare them via the SET

You also don't need that SELECT in SET _dateformat = SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i');

Try the following insteadCREATE PROCEDURE get_scheduledMsg()

BEGIN

SET @dateformat = DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i');

SELECT * FROM studentdetails WHERE classCode IS IN (SELECT class FROM messagedetails WHERE isStatus ='1' AND datecreated = @dateformat)

AND sectionCode IS IN (SELECT class FROM messagedetails WHERE isStatus ='1' AND datecreated = @dateformat);

END;

If you are still having issues then try the following insteadDELIMITER

CREATE PROCEDURE get_scheduledMsg()

BEGIN

SET @dateformat = DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i');

SELECT * FROM studentdetails WHERE classCode IS IN (SELECT class FROM messagedetails WHERE isStatus ='1' AND datecreated = @dateformat)

AND sectionCode IS IN (SELECT class FROM messagedetails WHERE isStatus ='1' AND datecreated = @dateformat);

END

DELIMITER ;

I've now noticed that you are comparing datecreated with @dateformat which implies that that column is a varchar on table messagedetails - it is a very bad idea to store date/datetime values in varchar columns - use the proper datatype

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值