无法创建MySQL存储过程

时间:2015-03-25 07:02:44

标签: php mysql stored-procedures

我正在尝试创建一个存储过程:

delimiter //
CREATE PROCEDURE update_counter
BEGIN 
 UPDATE bookmark bk,
 (SELECT count(Distinct user_bookmarks.user_id) AS bookmark_counter, bookmark_id 
  FROM user_bookmarks 
  LEFT JOIN bookmarks ON user_bookmarks.bookmark_id = bookmarks.id 
  GROUP BY user_bookmarks.bookmark_id 
 ) t
 set bk.counter = t.bookmark_counter
 where bk.id = t.bookmark_id
END //

但是这给了我一个错误

  

1064 - 您的SQL语法出错;查看与MySQL服务器版本对应的手册,以便在'BEGIN UPDATE bookmark bk附近使用正确的语法,(第2行的SELECT count(Distinct user_bookmarks.user_id)AS'有什么想法吗?这里有什么问题?

2 个答案:

答案 0 :(得分:1)

您必须在标题中定义IN和OUT参数。如果您没有参数,则必须添加()

delimiter //
CREATE PROCEDURE update_counter()
BEGIN 
 UPDATE bookmark bk,
 (SELECT count(Distinct user_bookmarks.user_id) AS bookmark_counter, bookmark_id 
  FROM user_bookmarks 
  LEFT JOIN bookmarks ON user_bookmarks.bookmark_id = bookmarks.id 
  GROUP BY user_bookmarks.bookmark_id 
 ) t
 set bk.counter = t.bookmark_counter
 where bk.id = t.bookmark_id;
END//

 delimiter;

答案 1 :(得分:1)

即使没有指定()in out

,程序名称也应以update_counter()结尾
delimiter //
CREATE PROCEDURE update_counter()
BEGIN 
 UPDATE bookmark bk,
 (SELECT count(Distinct user_bookmarks.user_id) AS bookmark_counter, bookmark_id 
  FROM user_bookmarks 
  LEFT JOIN bookmarks ON user_bookmarks.bookmark_id = bookmarks.id 
  GROUP BY user_bookmarks.bookmark_id 
 ) t
 set bk.counter = t.bookmark_counter
 where bk.id = t.bookmark_id;
END;//

delimiter ;