Skip navigation links

MySQL Forums :: Stored Procedures :: Using last_insert_id() in a stored procedure


Advanced Search

Using last_insert_id() in a stored procedure
Posted by: Tom L ()
Date: October 06, 2009 10:17AM

I am having a strange issue trying to use last_insert_id() within a stored procedure. For some reason it only gets the id of the first record inserted in the procedure, even if I am doing separate inserts. My code is something like this:

begin

declare id1 int(11);
declare id2 int(11);
declare id3 int(11);

INSERT INTO exampleTable (Name, Number) VALUES ("Example 1", 5235233);

set id1 = last_insert_id();

INSERT INTO exampleTable (Name, Number) VALUES ("Example 2", 2652623);

set id2 = last_insert_id();

INSERT INTO exampleTable (Name, Number) VALUES ("Example 3", 95636363);

set id3 = last_insert_id();

//Other code here

end

In this example id1, id2 and id3 ALWAYS end up as the same id which is the first record inserted. Is there a syntax error that I am missing here?

Options: ReplyQuote


Subject Views Written By Posted
Using last_insert_id() in a stored procedure 7321 Tom L 10/06/2009 10:17AM
Re: Using last_insert_id() in a stored procedure 2152 Tom L 10/07/2009 03:54AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.