我正在开发一个大学SQL项目,我在编写事件时遇到语法错误。代码如下:
SET GLOBAL event_scheduler = 1;
DROP TABLE IF EXISTS InAndOut;
CREATE TABLE InAndOut(
Time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Notification VARCHAR (50));
DROP EVENT IF EXISTS NoOfCustomerIn;
DELIMITER //
CREATE EVENT NoOfCustomerIn
ON SCHEDULE EVERY 10 SECOND
DO
DECLARE vInCustomer Integer Default 0;
SET vInCustomer = ROUND((9 * RAND()), 0);
INSERT InAndOut(NoOfCustomerInAndOut) VALUES (Concat(vInCustomer, 'Customer comes!')); //
END; //
DELIMITER ;
......错误是:
错误代码:1064。您输入错误 你的SQL语法;查看与MySQL对应的手册 服务器版本,用于在' DECLARE vInCustomer附近使用正确的语法 整数默认值0; SET vInCustomer = ROUND((9 * RAND()),0); '在第4行
答案 0 :(得分:0)
将变量更改为前面有@符号。
SET GLOBAL event_scheduler = 1;
DROP TABLE IF EXISTS InAndOut;
CREATE TABLE InAndOut(
Time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Notification VARCHAR (50));
DROP EVENT IF EXISTS NoOfCustomerIn;
DELIMITER //
CREATE EVENT NoOfCustomerIn
ON SCHEDULE EVERY 10 SECOND
DO
DECLARE @vInCustomer Integer Default 0;
SET @vInCustomer = ROUND((9 * RAND()), 0);
INSERT InAndOut(NoOfCustomerInAndOut) VALUES (Concat(@vInCustomer, 'Customer comes!')); //
END; //
DELIMITER ;
答案 1 :(得分:0)
如错误消息所示,您已将DECLARE
写入意外(即无效)位置:
DROP EVENT IF EXISTS NoOfCustomerIn;
DELIMITER //
CREATE EVENT NoOfCustomerIn
ON SCHEDULE EVERY 10 SECOND
DO
DECLARE vInCustomer Integer Default 0;
^^^^^^^
来自DECLARE
page in the documentation:
DECLARE只允许在BEGIN ... END复合语句中使用,并且必须在任何其他语句之前的开头。有趣的是,你已经拥有
END
部分。
您还要两次结束声明:
DELIMITER //
-- [...]
INSERT InAndOut(NoOfCustomerInAndOut) VALUES (Concat(vInCustomer, 'Customer comes!')); // -- Here
END; // -- And here
答案 2 :(得分:0)
您可以在mySQL
中的oneline set命令中设置变量的MySQL
SET @vInCustomer = ROUND((9 * RAND()), 0);
select @vInCustomer
MS SQL
DECLARE @vInCustomer Integer;
SET @vInCustomer = ROUND((9 * RAND()), 0);
select @vInCustomer