Hi there,
I’m using an AquaScript to execute an insert statement into a MySQL DB table… is there a way to get the last inserted row ID easily? If not, is there a way to generate a unique string that I can then look up the row again to get the ID? Ideally I’d like to avoid this option.
Thanks!
Tom
Response
Niels Gron over 11 years ago
MySQL has a function called LAST_INSERT_ID(). Here is the documentation …
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
This allows you to query for the last inserted row ID. I’ve attached a screenshot of an example executed script with the results to illustrate how you would do it. In AquaScript you just need to execute a new query using “SELECT LAST_INSERT_ID()” to get the result.
——— Script ————-
CREATE TABLE t ( id INT AUTO_INCREMENT NOT NULL PRIMARY KEY, name VARCHAR(10) NOT NULL )
go
INSERT INTO t VALUES (NULL, ‘Bob’)
go
SELECT * FROM t
go
SELECT LAST_INSERT_ID()