简体   繁体   中英

Not able to create a MySQL stored procedure

I am trying to create a stored procedure:

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 //

But this is giving me an error

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BEGIN UPDATE bookmark bk, (SELECT count(Distinct user_bookmarks.user_id) AS' at line 2 Any idea? What is wrong here?

You have to define the IN and OUT parameter in the header. If you do not have parameter, you have to add ()

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;

The procedure name should end with () even if there is no in and out specified 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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM