MySQL Stored Procedure Error Unexpected character “:”

前端 未结 1 1894
长发绾君心
长发绾君心 2021-01-26 05:14

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 t

1条回答
  •  深忆病人
    2021-01-26 05:59

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

    0 讨论(0)
提交回复
热议问题