Error declaring integer variable inside MySQL stored function

前端 未结 2 778
南笙
南笙 2021-01-18 10:08

I\'m getting an error when trying to declare a new stored function in MySQL (Server version: 5.5.13)

Basically, I have a large table which classifies strings dependi

相关标签:
2条回答
  • 2021-01-18 10:22

    DECLARE part inside stored procedure must be at top and SET and other statemnts starts after that.

    0 讨论(0)
  • 2021-01-18 10:37

    There were some syntax errors -

    One of them is - all declarations must be at the begining of the BEGIN...END clause.

    DELIMITER $
    
    DROP FUNCTION IF EXISTS get_string_class$
    CREATE FUNCTION get_string_class(mystring VARCHAR(15))
    
    RETURNS VARCHAR(15)
    READS SQL DATA
    BEGIN
    
    DECLARE i INT;
    DECLARE segment VARCHAR(15);
    DECLARE String_Class VARCHAR(15);
    DECLARE mystringlength INT;
    
    SET i = 2;
    SET mystringlength = LENGTH(mystring);
    SET String_Class = NULL;
    
    WHILE i <= mystringlength DO
    
       SET segment = LEFT(mystring, i);
    
       SET String_Class = (SELECT String_Class FROM string_class_list WHERE String_Begins = segment);
    
       IF (SELECT FOUND_ROWS()) = 1 THEN
          RETURN String_Class;
       END IF;
    
       SET i = i + 1;
    
    END WHILE;
    
    RETURN String_Class;
    
    END$
    DELIMITER ;
    
    0 讨论(0)
提交回复
热议问题