哪里出错?

时间:2012-03-28 07:26:54

标签: mysql sql

请帮我找出错误的地方?

delimiter //
CREATE FUNCTION `count_photos_in_gallery`(`tmp_gallery_id` INT(11) NOT NULL) RETURNS TINYINT(3) UNSIGNED
BEGIN
    DECLARE `tmp_count` TINYINT(3) UNSIGNED DEFAULT 0;

    SELECT COUNT(`id`) INTO `tmp_count` FROM `photos_items` WHERE `gallery_id`=`tmp_gallery_id` LIMIT 1;

    RETURN `tmp_count`;
END//

错误:

  

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法   'NOT NULL附近'返回TINYINT(3)UNSIGNED BEGIN DECLARE tmp_count   TINYINT(3)UNSI'在第1行

2 个答案:

答案 0 :(得分:2)

更改函数声明,参数不能有NOT NULL选项 -

...
CREATE FUNCTION `count_photos_in_gallery`(`tmp_gallery_id` INT(11))
  RETURNS TINYINT(3) UNSIGNED
...

答案 1 :(得分:1)

试试这个

DELIMITER //
CREATE FUNCTION `count_photos_in_gallery`(`tmp_gallery_id` INT(11)) RETURNS TINYINT(3) UNSIGNED
BEGIN
    DECLARE `tmp_count` TINYINT(3) UNSIGNED DEFAULT 0;

    SELECT COUNT(`id`) INTO `tmp_count` FROM `photos_items` WHERE `gallery_id`=`tmp_gallery_id` LIMIT 1;

    RETURN `tmp_count`;
END//