Page 1 of 1

Get auto-numbered primary key on insert?

Posted: 15.07.2006, 05:41
by thenickdude
Hey all,

I'm inserting a row into a table, the MySQL table has an AUTO_INCREMENT primary key. The problem is, I need to make an insertion into another table which references the first row I inserted, so I need to know what id was generated. How do you solve this? In PHP I'd just use mysql_insert_id().

Posted: 15.07.2006, 09:22
by Terence
You can query for it with same params as for insert and "select id" afterwards , you can use mysql method in sql call "SELECT LAST_INSERT_ID()" or you can use TZUpdateSQL.OnAfterInsertStament Event for mysql only and only available for latest testing branch.

Also see here for detailed discusssion.
http://zeos.firmos.at/viewtopic.php?t=613&highlight=

Posted: 16.07.2006, 02:23
by thenickdude
SELECT LAST_INSERT_ID() sounds great, thanks.