Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL syntax error when creating a stored procedure in MySQL

I have a hard time locating an error when trying to create a stored procedure in mysql.

If I run every single line of the procedure independently, everything works just fine.

CREATE PROCEDURE cms_proc_add_child 
(
    param_parent_id INT, param_name CHAR(255),
    param_content_type CHAR(255)
)
BEGIN
    SELECT @child_left := rgt FROM cms_tree WHERE id = param_parent_id;
    UPDATE cms_tree SET rgt = rgt+2 WHERE rgt >= @child_left;
    UPDATE cms_tree SET lft = lft+2 WHERE lft >= @child_left;
    INSERT INTO cms_tree (name, lft, rgt, content_type) VALUES 
    (
        param_name,
        @child_left,
        @child_left+1,
        param_content_type
    );
END

I get the following (helpful) error:

ERROR 1064 (42000): 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 '' at line 3

I just don't know where to start debugging, as every single one of these lines is correct.

Do you have any tips?

like image 990
Pierre Spring Avatar asked Mar 12 '09 15:03

Pierre Spring


People also ask

What is the syntax to in stored procedure?

The stored procedure syntax has the following parameters: Schema_name: It is the name of your database or schema. By default, a procedure is associated with the current database, but we can also create it into another database by specifying the DB name.

Can I write stored procedure in MySQL?

Create a simple stored procedure. DELIMITER ; To create the MySQL Stored Procedure, open the MySQL workbench Connect to the MySQL Database copy-paste the code in the query editor window click on Execute. You can view the procedure under stored procedures.

How do I return an error from a stored procedure in MySQL?

BEGIN select "error message '%s' and errorno '%d'"; -- use missed the semicolon ROLLBACK; END; So it will actually display error msg with column name as 'ROLLBACK' instead of print and rollback the transaction.


1 Answers

As line 3 contains the first ; perhaps you have a problem with your delimiters.

See http://dev.mysql.com/doc/refman/5.0/en/stored-programs-defining.html

DELIMITER //
CREATE PROCEDURE dorepeat(p1 INT)
BEGIN
    SET @x = 0;
    REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
END//
DELIMITER ;
like image 50
Uwe Mesecke Avatar answered Oct 10 '22 15:10

Uwe Mesecke