MySQL:这个CREATE PROCEDURE语句出了什么问题?

时间:2011-05-18 18:44:16

标签: mysql stored-procedures

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;

我不确定它有什么问题。 MySQL告诉我各种各样的事情是不正确的,它只是不想创建。此外,参数与各自的表格的类型和长度相同。

2 个答案:

答案 0 :(得分:5)

这是你的第一个“;”打破了程序定义,mysql认为你已经完成并且处理“;”之后发生的任何事情作为另一个查询。 您必须为存储过程使用分隔符。

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 ;

答案 1 :(得分:1)

有一些问题。我希望我能得到所有这些:

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;