mysql中我写了一个存储函数 为什么会报错 请指点 着急

表名:total字段:id,tablenames,time_colum,fix_timedelimiter&&createfunctionalarm_info()retu... 表名:total 字段:id,tablenames,time_colum,fix_time

delimiter &&
create function alarm_info()
returns varchar(100) reads sql data
begin
declare counts int(11);
declare n int(11);
declare fixs int(11);
declare times varchar(20);
declare diff int(11);
declare names varchar(20);
declare result varchar(100);
set n=1;
select count(*) into counts from total;
while counts>=n
select tablenames,time_colum,fix_time into names,times,fixs from total where id=n;
execute immediate 'select round(time_to_sec(timediff(sysdate(),(select times from names order by id desc limit 1)/60)' into diff;
if(diff>fix_time) then result=tablenames;
end if;
n=n+1;
end &&;

delimiter;
展开
 我来答
wangzhiqing999
2011-09-09 · TA获得超过1.6万个赞
知道大有可为答主
回答量:7048
采纳率:100%
帮助的人:3235万
展开全部
MySQL 里面, 好像没有
execute immediate 'select round(time_to_sec(timediff(sysdate(),(select times from names order by id desc limit 1)/60)' into diff;

这种写法的。
可以修改为:
select round(time_to_sec(timediff(sysdate(),(select times from names order by id desc limit 1)/60) into diff;

或者
SET diff = round(time_to_sec(timediff(sysdate(),(select times from names order by id desc limit 1)/60)

还有后面的 n=n+1;
修改为
SET n=n+1;
更多追问追答
追问
可是 还是继续报错
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 near ';
if(diff>fix_time) then result=tablenames;
end if;
set n=n+1;
end while;
end' at line 15
追答
WHILE  的语法不正确


WHILE 条件 DO

END WHILE;

下面是一个例子。

mysql> DELIMITER //
mysql> CREATE PROCEDURE TestWhile()
-> BEGIN
-> DECLARE v_index INT;
->
-> SET v_index = 0;
->
-> WHILE v_index SET v_index = v_index + 1;
-> SELECT v_index;
-> END WHILE;
->
-> END//
Query OK, 0 rows affected (0.00 sec)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式