这个mySql脚本中的语法错误是什么

时间:2014-03-18 18:52:38

标签: mysql sql

我写了一个存储函数:

  USE sakila;

DROP FUNCTION IF EXISTS sumOfFilm;

DELIMITER $$

CREATE FUNCTION sumOfFilm(
    mStore_id TINYINT, mMonth VARCHAR(4), mYear VARCHAR(4))
BEGIN
    DECLARE @sumOfFilm INT;
    SELECT COUNT(*) INTO @sumOfFilm FROM rental
    INNER JOIN inventory ON rental.inventory_id=inventory.inventory_id
    INNER JOIN store ON store.store_id=inventory.store_id
    WHERE mStore_id=store.store_id AND
            mMonth=month(rental_date) AND
            mYear=year(rental_date)
    GROUP BY store.store_id;
    RETURN @sumOfFilm;
END$$

DELIMITER ;

当我执行它时,我收到一条错误消息:

  

错误1064(42000):您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'BEGIN附近使用正确的语法           DECLARE @sumOfFilm INT;           SELECT COUNT(*)INTO @sumOfFilm FROM rental           INN'在第3行

请帮帮我:)。

1 个答案:

答案 0 :(得分:1)

  USE sakila;

DROP FUNCTION IF EXISTS sumOfFilm;

DELIMITER $$

CREATE FUNCTION sumOfFilm(
    mStore_id TINYINT, mMonth VARCHAR(4), mYear VARCHAR(4))
BEGIN
    SELECT @sumOfFilm := COUNT(*) FROM rental
    INNER JOIN inventory ON rental.inventory_id=inventory.inventory_id
    INNER JOIN store ON store.store_id=inventory.store_id
    WHERE mStore_id=store.store_id AND
            mMonth=month(rental_date) AND
            mYear=year(rental_date)
    GROUP BY store.store_id;
    RETURN INT @sumOfFilm;
END$$

DELIMITER ;

也可以尝试

SET @sumOfFilm = (SELECT COUNT(*) FROM rental)