MySQL Stored Procedure Syntax Errors
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.
