代码之家  ›  专栏  ›  技术社区  ›  Hellpless

set语句中的MySQL脚本

  •  1
  • Hellpless  · 技术社区  · 7 年前

    这就是我创造的:

    创建函数change_int(colres VARCHAR(500))返回int(11)
    开始

        DECLARE res int(11);
        DECLARE leng int(11);
        DECLARE newres int(11);
        DECLARE mult int(11);
        DECLARE temp1 int(11);
        DECLARE temp2 int(11);  
    
    SET res = CAST(colres AS UNSIGNED);
    SET leng = CHAR_LENGTH(CAST( colres AS CHAR)); 
    SET newres = 0;
    SET mult = 1;
    SET temp1 = 0;
    SET temp2 = 0;
    
    
    WHILE (res > 0) DO
    
        SET temp1 = MOD(res , 10 );
        SET res = (res DIV 10);
        SET temp2 = MOD(res , 10 );
        SET newres = (newres +((temp1 + temp2 ) * mult));       
        SET mult = mult*10;
    END WHILE;
    SET newres = SUBSTRING (newres, 1, leng );
    RETURN newres;  
    END $$
    DELIMITER ;
    

    但当我在第6行尝试运行它时,会出现错误:

    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''res' = CAST(colres AS CHAR)' at line 6
    

     #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET newres = SUBSTRING (newres, 1, leng );
        RETURN newres;    
        END' at line 27
    

    格雷特:现在它起作用了。 但我无法唤起她:

    SHOW FUNCTION STATUS;
    

    它是存在的。

    当我尝试这样运行它时:

    UPDATE `table` SET `col1` = function_name(`col1`);
    

    UPDATE `table` SET `col1` = db.function_name(`col1`);
    

    不走运。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Derviş Kayımbaşıoğlu    7 年前

    您以前缺少列 END WHILE END WHILE;

    Working example