请考虑以下功能定义。我在MySQL 5.1上创建并设置它,但它在MariaDB 5.5中失败了
CREATE DEFINER=`root`@`127.0.0.1` FUNCTION `weighted_mean_by_kpi`(`KPIID` INT, `employee_id` INT, `date` DATE)
RETURNS decimal(6,3)
LANGUAGE SQL
DETERMINISTIC
READS SQL DATA
SQL SECURITY DEFINER
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE rating_number INT DEFAULT 0;
DECLARE rating_count INT DEFAULT 0;
DECLARE rating_total INT DEFAULT 0;
DECLARE weighted_total DOUBLE DEFAULT 0;
DECLARE cur CURSOR FOR
SELECT COUNT(rating), rating FROM employees_KPIs WHERE kpi_id = KPIID AND employee_id = employee_id AND employees_KPIs.created_at LIKE CONCAT("%",DATE_FORMAT(date,'%Y-%m'),"%") GROUP BY rating;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
RATING: LOOP
FETCH cur INTO rating_count, rating_number;
IF done = 1 THEN
LEAVE RATING;
END IF;
SET weighted_total = weighted_total + (rating_number * rating_count);
SET rating_total = rating_total + rating_count;
END LOOP RATING;
return (weighted_total/rating_total);
#return (weighted_total);
CLOSE cur;
END
我收到以下错误:
第1行的错误1064(42000):您的SQL语法出错;查看与您的MariaDB服务器版本对应的手册,以获得在''附近使用的正确语法。在第8行
由于
答案 0 :(得分:0)
Mysql看到';'函数中的分隔符会破坏您的CREATE FUNCTION
语句。
要避免这种情况,请在定义函数之前更改分隔符,然后再将其更改回来:
喜欢:
DELIMITER //
-- your create function definition statement here
//
DELIMITER ;
在您的代码中,在;
找到了第一个line 8
分号,它尝试将代码执行到';',并且语法无效,因为它不完整({{ 1}}没有BEGIN
)。