我有以下声明来创建存储过程,但我不断得到一个"意外字符':'在835和#34;错误。语句中唯一的冒号位于start_loop:循环中。为什么我会收到此错误,我该如何解决?
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `get_gauge_values` (in maxdate timestamp, in tagid int)
BEGIN
declare finished boolean;
declare line_timestamp timestamp;
declare line_tagid int;
declare line_name varchar(50);
declare line_value varchar(50);
-- CREATE OUR TEMP TABLE
DROP TABLE IF EXISTS GAUGE_VALUES_TEMP;
CREATE TABLE GAUGE_VALUES_TEMP (
LINE_NAME varchar(255),
LINE_VALUE varchar(50)
);
-- CREATE OUR CURSOR
DECLARE cid CURSOR FOR
SELECT MAX(hd.timestamp), hd.tag_id
FROM dashboard_lines dl
JOIN historical_data hd ON hd.tag_id = dl.gauge_tag_id
WHERE (hd.timestamp is null OR hd.timestamp <= maxdate)
AND (dl.gauge_tag_id is null OR dl.gauge_tag_id = tagid);
set finished=false;
declare continue handler for not found set finished=true;
open cid;
start_loop: loop
fetch cid into line_timestamp, line_tagid;
if finished <> false then
leave start_loop;
else
insert into gauge_values_temp (line_name, line_value)
select ol.name, hd.value
from dashboard_lines dl
join operation_lines ol on ol.line_id = dl.line_id
join historical_data hd on hd.tag_id = dl.gauge_tag_id
where dl.gauge_tag_id = line_tagid;
end if;
end loop;
close cid;
select * from gauge_values_temp;
END
$$
答案 0 :(得分:3)
这是一个非常恼人的MySQL语法相关问题:在您的过程中某处有一个TAB导致此解析问题:仅对任何缩进或对齐使用空格。
我的版本(MariaDB 10.x)也不喜欢使用DECLARE
订单开始您的程序,所以我也更新了它。看看它是否仍适合您在该位置的目的。
我的数据库创建的调整过程没有语法错误:
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `get_gauge_values` (in maxdate timestamp, in tagid int)
BEGIN
declare finished boolean;
declare line_timestamp timestamp;
declare line_tagid int;
declare line_name varchar(50);
declare line_value varchar(50);
DECLARE cid CURSOR FOR
SELECT MAX(hd.timestamp), hd.tag_id
FROM dashboard_lines dl
JOIN historical_data hd ON hd.tag_id = dl.gauge_tag_id
WHERE (hd.timestamp is null OR hd.timestamp <= maxdate)
AND (dl.gauge_tag_id is null OR dl.gauge_tag_id = tagid);
declare continue handler for not found set finished=true;
set finished=false;
DROP TABLE IF EXISTS GAUGE_VALUES_TEMP;
CREATE TABLE GAUGE_VALUES_TEMP (
LINE_NAME varchar(255),
LINE_VALUE varchar(50)
);
open cid;
start_loop: loop
fetch cid into line_timestamp, line_tagid;
if finished <> false then
leave start_loop;
else
insert into gauge_values_temp (line_name, line_value)
select ol.name, hd.value
from dashboard_lines dl
join operation_lines ol on ol.line_id = dl.line_id
join historical_data hd on hd.tag_id = dl.gauge_tag_id
where dl.gauge_tag_id = line_tagid;
end if;
end loop;
close cid;
select * from gauge_values_temp;
END
$$