简体   繁体   English

mysql存储过程声明在执行时抛出错误

[英]mysql stored procedure declaration throws error on execution

I"m continiously receiving this error when trying to create this stored procedure. I'm trying to write a procedure that splits a comma delimited string. Similar to explode. I feel I'm close. 尝试创建此存储过程时,我不断收到此错误。我试图编写一个过程,以逗号分隔的字符串进行分割。类似于explode。我感觉到了。

This is the error 这是错误

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE start_pos, end_pos INT; 检查与您的MySQL服务器版本相对应的手册,以在'DECLARE start_pos,end_pos INT附近使用正确的语法; SET start_pos = 1; SET start_pos = 1; SET end_pos = Locat' at line 6 在第6行SET end_pos = Locat'

I copied the logic from a SQL Server example and did my best translate it to MySql syntax. 我从SQL Server示例中复制了逻辑,并尽力将其转换为MySql语法。

This is the entire procedure from start to finish. 这是从头到尾的整个过程。 I'm hoping a well trained eye can explain why I'm getting an error. 我希望训练有素的眼睛可以解释为什么我出错了。

DELIMITER $$

CREATE procedure split_string (in p_string_to_split VARCHAR(255),in p_delimiter CHAR(1) ) 
BEGIN    
    DROP TEMPORARY TABLE IF EXISTS split_channel_ids;
    CREATE TEMPORARY TABLE split_channel_ids (p_channel_id int);

    DECLARE start_pos, end_pos INT;
    SET start_pos = 1;
    SET end_pos = Locate(p_delimiter, p_string_to_split); 
    WHILE (start_pos < CHAR_LENGTH(p_string_to_split) + 1) DO
        IF (end_pos = 0) THEN
            SET end_pos = CHAR_LENGTH(p_string_to_split) + 1;
        END IF;
        --- INSERT split_channel_ids (p_channel_id)  
        --- VALUES(SUBSTRING(p_string_to_split, start_pos, end_pos - start_pos)) ;
        SET start_pos = end_pos + 1;
        SET end_pos = Locate(p_delimiter, p_string_to_split, start_pos);
    END WHILE; 
    -- select * from imob_users;
    select * from split_channel_ids;
END $$
DELIMITER ;

DECLARE statements (in MySQL) must be at the beginning of their enclosing BEGIN...END block. (在MySQL中) DECLARE语句必须在其包围BEGIN...END块的开头。

In MS SQL, they can be anywhere; 在MS SQL中,它们可以在任何地方。 but it is annoying there because they do not have block scope, they have procedure scope, so you can't "re-use" names in independent blocks. 但这很烦人,因为它们没有块作用域,它们有过程作用域,所以您不能在独立的块中“重用”名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM