MySQL multiple statement execution order

533 views Asked by At

I have created a routine which inserts a record in one table, and after that it searches for that id (with a select statement) and updates another table's field with that id.Is this possible? It's one routine so my question is if the statements are executed in a sequential order?

Thanks in advance

1

There are 1 answers

0
JHS On
  1. You do not need to search for the id. You can use LAST_INSERT_ID() to get the id of the last inserted row.

    INSERT INTO tablename (<columns>) VALUES (<columnvalues>);

    SELECT LAST_INSERT_ID() INTO somevariable;

  2. Then you can write your UPDATE statement.

    UPDATE sometable SET sometable.col = somevariable WHERE sometable.something = @something

  3. Statements in a routine are executed in the order they are written.