我已经编写了一个小代码块(后来在MySQL的一个事件中使用,但我在声明一个看似标准的INT变量时遇到错误:
BEGIN
DECLARE myvar INT;
SELECT TIMESTAMPDIFF INTO myvar (MINUTE,(select user_hb_stamp from eclipse_users where username = 'user1'),(SELECT NOW()));
IF (myvar > 5)
UPDATE eclipse_users SET logged=0 WHERE username = 'user1';
END
我做错了什么?
提前致谢。
答案 0 :(得分:1)
您的select
声明格式错误。试试这个:
SELECT myvar := TIMESTAMPDIFF(MINUTE,
(select user_hb_stamp from eclipse_users where username = 'user1'),
NOW());
虽然可以使用into
来表示变量,但我更喜欢直接设置它们 - 除非您编写的代码需要与Oracle兼容。 (然后所有函数调用都不起作用。)
更典型的写法是:
SELECT myvar := TIMESTAMPDIFF(MINUTE, user_hb_stamp, NOW())
FROM eclipse_users
WHERE username = 'user1';
编辑:
对于declare
本身,问题可能是缺少delimiter
语句。尝试:
delimiter $$
create procedure . . .
begin
declare p_myvar int;
select p_myvar := TIMESTAMPDIFF(MINUTE, user_hb_stamp, NOW())
from eclipse_users
where username = 'user1';
. . .
end$$
delimiter ;