Phil Phil - 4 years ago 155
SQL Question

MySQL Stored Procedure Error Unexpected character ":"

I have the following statement to create a stored procedure, but I keep getting an "Unexpected character ':' at position 835" error. The only colon in the statement is in the start_loop: loop. Why am I getting this error and how can I fix?

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
$$

Answer Source

It is a very annoying MySQL syntax related issue: There is a TAB somewhere in you procedure leading to this parse issue: Use spaces only for any indentation or alignment.

My version (MariaDB 10.x) also does not like the start of your procedure with the DECLARE order, so I also updated that. See if it still fits your purpose at that location.

The adjusted procedure which my database creates without syntax errors:

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
$$
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download