Question

I am trying to create stored procedure in mysql admin. I am hosting a website in POWWEB.com. For this purpose i am trying to create stored procedure in mysqladmin.

The Mysql version is 5.0.45. I am able to create a stored procedure with only 1 line of code.

CREATE PROCEDURE TEST(input INT)
    INSERT INTO TEST(COL1) VALUES(input);

But this is of no use. I want to write a stored procedure with more commands. So i try doing like

CREATE PROCEDURE TEST(input INT)
BEGIN
    INSERT INTO TEST(COL1) VALUES(input);
    INSERT INTO TEST1(COL1) VALUES(input);
    INSERT INTO TEST2(COL1) VALUES(input);
END

But this gives a syntax error. But the same code works fine in my local machine. Please let me know if you have any idea of how to solve this. Any help or advice is greatly appreciated.

Was it helpful?

Solution

The default delimiter is ;, so it interprets the code as multiple queries, which obviously doesn't work. Try something like this:

delimiter //
CREATE PROCEDURE TEST(input INT)
BEGIN
    INSERT INTO TEST(COL1) VALUES(input);
    INSERT INTO TEST1(COL1) VALUES(input);
    INSERT INTO TEST2(COL1) VALUES(input);
END//

OTHER TIPS

Ignore PHPMyAdmin, it is as useless as a motorcycle-ashtray. Log in to your shell and use the mysql command line interface, this is the only supportable, correct way of scripting mysql.

The DELIMITER command is not a mysql server command, it is a mysql command-line client command. To use it you must use the proper mysql command line client.

It is difficult (although not impossible) to create stored procs otherwise.

In phpMyAdmin, in the SQL editor you can set the delimiter in a form field labeled 'delimiter'. It is by default. Set it to anything you like, do NOT type in delimiter // in your SQL but DO end the sql with your delimiter as END// where // is the delimiter of your choice.

the Best answer is just @Barry's comment:

Stored procedures with multiple queries work fine when putting a BEGIN .. END block around it. – Barry Staes

Plus, putting ; at end of each query.

example:

BEGIN
    INSERT INTO passenger (fname,lname,tel,ID,sex)
    VALUES (fname,lname,tel,ID, sex);
    INSERT INTO passenger 
    select * from reservation where 1;
END

MySQL admin is outdated. Please use MySQL workbench for more functionalities and and rich GUI based operations. https://www.mysql.com/products/workbench/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top