Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

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

0

36 views
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 just doesn't want to be created. Also, the parameters are identical types and lengths to their respective tables.

asked May 18, 2011 1:44 pm CDT
posted via StackOverflow

3 Answers

0
 

Take out the AS keyword before BEGIN?

This is in the screenshot but you've just added it in an edit. I've checked the 5.x docs and it isn't mentioned at all for CREATE PROCEDURE

answered May 18, 2011 2:23 pm CDT
gbn
0
 

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;

answered May 18, 2011 2:23 pm CDT
3
 

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 ;

answered May 20, 2011 12:18 pm CDT

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions