CF Snippets

Get Generated Primary Key from an INSERT statement

If you use MySQL to generate database record ids for you (e.g. a PRIMARY KEY created via an autonumber field or UUID() call), use the result option to specify a variable name which will contain the result of the query - including a generatedKey field for retrieving the primary key of the new record.

var queryOptions = {
  result: "newUser"
var data = queryExecute(
  "INSERT INTO users (username, password)
  VALUES ( 'mike.born', UUID() )",
writeOutput( "New user id: #newUser.generatedKey#" );