函数SQL错误代码:1046

时间:2015-06-03 02:49:55

标签: mysql sql function error-code

这是我的函数查询:

= articles =
id           |  int(10) Primary
created      |  datetime
modified     |  datetime

= articles_lang =
id_articles  |  int(11)
id_lang      |  int(11)
title        |  varchar(150)
body         |  text
  

错误代码:1046

     
    

你的sql语法有错误:查看与你的Mysql服务器版本相对应的手册,以便在第8行的''附近使用正确的语法

  

1 个答案:

答案 0 :(得分:0)

错误本身说错误在第8行 你的第8行:为表中的变量赋值。语法是 错

您还要在select语句后声明游标并创建临时表。这不是正确的顺序..你应该在开始任何其他声明之前移动上面的声明语句..

此外,您的函数应返回INT值,但您只指定了Return; ..您应该指定值以及Return ..我猜它可以是total_hari_kerja

尝试以下

    DELIMITER $$
DROP FUNCTION IF EXISTS `dtkp`.`Tepat`$$
CREATE FUNCTION `dtkp`.`Tepat`(proses INT) RETURNS INT
    BEGIN
    DECLARE uid INTEGER;
    DECLARE total_hari_kerja INTEGER;
    DECLARE done INT DEFAULT FALSE;
    DECLARE krk_uid CURSOR FOR SELECT DISTINCT krk FROM krk_kartu_kendali WHERE
    proses <= 8 ;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    CREATE TEMPORARY TABLE tempKK (krk INT, terlambat INT) ENGINE=INNODB;


   BEGIN
    SELECT SUM (alokasi) into total_hari_kerja FROM proses;



    OPEN krk_uid;
    tepatLoop:LOOP
        FETCH krk_uid INTO uid;
        IF done THEN
        LEAVE tepatLoop;
        END IF;

    INSERT INTO tempKK SELECT krk, GetTepat(realisasi_tgl_terima, total_hari_kerja) terlambat FROM krk_kartu_kendali WHERE proses=1 and krk=uid ORDER BY RecID LIMIT 1;
    FETCH krk_uid INTO uid;
    END LOOP;

    CLOSE krk_uid;
    DEALLOCATE krk_uid;

    RETURN total_hari_kerja ;
     END$$
    END$$

DELIMITER ;