Skip to main content
UteFanJason
Inspiring
October 21, 2011
Answered

MySQL Stored Procedure Syntax Errors

  • October 21, 2011
  • 1 reply
  • 1965 views

I have an online application I would like to use transactions to either commit all db updates or none at all. The code I have created is as follows, not only am I getting a syntax error on lines 5 and 16, but quite frankly I am unsure the code will actually do what I want. Please let me know if you see the syntax errors and could explain them to me.

CREATE PROCEDURE increase_maint_priority
    (maint_item INT, new_priority INT, cur_priority INT, center_id INT)

BEGIN
    START TRANSACTION; -- line 5 syntax error
    -- start updates
    UPDATE maintitem SET priority = (priority+1)
        WHERE priority > cur_priority
        AND priority <= new_priority
        AND centerID = center_id;
       
    UPDATE maintitem SET priority = new_priority
        WHERE id = maint_item;
   
    COMMIT;
END; -- line 16 syntax error

Thank you in advance for helping a newb to stored procedures.

If you would like to help me figure out if this code will actually work for what I want the following describes the situation.

Main idea: business location managers submit requests for maintenance at their center. Requests can be added anywhere in the priority list (1=highest priority). They can also change the priority of a request at any time.

Previously I have been using php to control a loop of mysql update queries (yikes, I know now, 2 sql statements could do the trick) and lately we have been experiencing duplicate priorities, and other problems that seem to be most likely linked to the updating of priorities of multiple items.

Ex: moving priority item 6 to priority 1 requires 1 update query for the current 6th priority item, but a second query (or loop of several more since I was very new to sql statements at time of development 2 years ago) to update the other priorities.

Thus the desire for the transaction method.

Again, thanks in advance for any help on this issue.

P.S. - I am using MySQL workbench to write this and that's where I get the errors.

This topic has been closed for replies.
Correct answer bregent

You know what though, as I was looking closer it isn't giving me an error, it is only a warning that the line with the BEGIN command is breaking syntax. I guess at this point I need to fill my maintitem table with data and test out the procedure.

If you notice anything that will eliminate the warning please let me know. Thank you.


I don't use MySQL and its syntax is slightly different from other dbms's regarding stored procedures. In any case, once you eliminate the warning, you'll also need to add a handler to make sure the updates roll back on error:

http://khanrahim.wordpress.com/2010/05/16/transaction-with-stored-procedure-in-mysql-server/

1 reply

Participating Frequently
October 21, 2011
UteFanJason
Inspiring
October 23, 2011

So I added the following line at the beginning of my code:

DELIMITER //

and the following lines at the end of my code:

END //

DELIMITER ;

and I am still getting a syntax error but now it is on the line with the BEGIN command. I tried adding a semicolon after the BEGIN command but I still get a syntax error there.

Any ideas why?

Participating Frequently
October 24, 2011

Can you show the complete code you have now?