开发者

MySQL: What's wrong with this CREATE PROCEDURE statement?

DROP PROCEDURE IF EXISTS CreateTopic;
CREATE PROCEDURE CreateTopic
(
   i_forum_id INT,
   i_user_id INT,
   i_title VARCHAR(255),
   i_language VARCHAR(50),
   i_content TEXT,
   i_stickied TINYINT,
   i_announce TINYINT,
   i_closed TINYINT
)
BEGIN
   INSERT INTO forum_topics (forum_id, user_id, title, language)
       VALUES (i_forum_id, i_user_id, i_title, i_language);
   SET @tid := LAST_INSERT_ID();
   INSERT INTO forum_posts (topic_id, user_id, subject, content) VALUES (@tid, i_user_id, i_title, i_content);
   INSERT INTO core_logs (obj_id, user_id, type, action) VALUES (@tid, i_user_id, 'Topics', 'Topic Created');
END;

I'm not sure what's wrong with it. MySQL tells me all sorts of things are incorrect, it j开发者_如何学Goust doesn't want to be created. Also, the parameters are identical types and lengths to their respective tables.


It is your first ";" that breaks procedure definition and mysql thinks you are done and treats whatever goes after ";" as another query. You have to use delimiter for stored procedures.

DELIMITER $$
DROP PROCEDURE IF EXISTS CreateTopic$$
CREATE PROCEDURE CreateTopic
(
   i_forum_id INT,
   i_user_id INT,
   i_title VARCHAR(255),
   i_language VARCHAR(50),
   i_content TEXT,
   i_stickied TINYINT,
   i_announce TINYINT,
   i_closed TINYINT
)
BEGIN
   INSERT INTO forum_topics (forum_id, user_id, title, language)
       VALUES (i_forum_id, i_user_id, i_title, i_language);
   SET @tid := LAST_INSERT_ID();
   INSERT INTO forum_posts (topic_id, user_id, subject, content) VALUES (@tid, i_user_id, i_title, i_content);
   INSERT INTO core_logs (obj_id, user_id, type, action) VALUES (@tid, i_user_id, 'Topics', 'Topic Created');
END
$$
DELIMITER ;


There are a few problems. I hope I got them all:

DROP PROCEDURE IF EXISTS CreateTopic;
CREATE PROCEDURE CreateTopic
(
   i_forum_id INT,
   i_user_id INT,
   i_title VARCHAR(255),
   i_language VARCHAR(50),
   i_content TEXT,
   i_stickied TINYINT,
   i_announce TINYINT,
   i_closed TINYINT
)
BEGIN
   DECLARE tid INT;
   INSERT INTO forum_topics (`forum_id`, `user_id`, `title`, `language`)
       VALUES (i_forum_id, i_user_id, i_title, i_language);
   SET tid = LAST_INSERT_ID();
   INSERT INTO forum_posts (`topic_id`, `user_id`, `subject`, `content`) VALUES (tid, i_user_id, i_title, i_content);
   INSERT INTO core_logs (`obj_id`, `user_id`, `type`, `action`) VALUES (tid, i_user_id, 'Topics', 'Topic Created');
END;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜