Questions tagged [last-insert-id]

LAST_INSERT_ID is MySQL specific functionality to get the value of the AUTO_INCREMENT column most recently inserted into. It is the recommended means of getting the value in MySQL, because SELECT MAX(auto_increment) ... is not reliable, as concurrency problems can occur.

LAST_INSERT_ID is MySQL specific functionality to get the value of the AUTO_INCREMENT column most recently inserted into. It is the recommended means of getting the value in MySQL, because SELECT MAX(auto_increment) ... is not reliable, as concurrency problems can occur.

That said, this functionality is not ANSI. Sequences are now ANSI, supported by DB2, Oracle, PostgreSQL, and SQL Server "Denali"). The ANSI equivalent to LAST_INSERT_ID would be: CURRVAL ( NEXTVAL is used to get the next value).

Documentation:

215 questions
0
votes
1 answer

MySQL - Get ID value (PK) during the INSERT, not after

I am trying to obtain the value of an auto increment ID during an insert and save that value in another field in the same table during the insert. I know of LAST_INSERT_ID() but I only get that value after the record has been entered. I am looking…
gunnersboy
  • 25
  • 2
0
votes
0 answers

Laravel AppServiceProvider public function boot DB::listen bindings=0

public function boot() { DB::listen(function ($query) { $sql = $query->sql; $bindings = $query->bindings; if(stripos(substr($sql, 0, 10),'update') !== false){ dd($query->bindings); …
戴廷逸
  • 11
  • 2
0
votes
1 answer

Laravel funtion lastInsertID is not supported by PDO driver

I am using Firebird and having the following error: SQLSTATE[IM001]: Driver does not support this function: driver does not support lastInsertId() I dig a bit on Laravel sources, and found this piece: public function processInsertGetId(Builder…
0
votes
0 answers

LAST_INSERT_ID() returns NULL on INSERT attempts after first

Working with MySql 8.0.32, I've created this stored procedure: CREATE DEFINER=`root`@`%` PROCEDURE `spTimersCreate`(IN paction bigint, IN preseller bigint, IN pinterval bigint) BEGIN INSERT INTO timers (action, reseller, `interval`,…
PdxRene
  • 1
  • 1
0
votes
1 answer

SQL Query working in Workbench but getting `ER_PARSE_ERROR` via NodeJS

I'm sending a query to create a user in the DB by doing: let sql = `INSERT INTO Users (Username, Password, FirstName, LastName, Permissions) VALUES ('${req.body.username}', '${hashedPassword}','${req.body.FirstName}','${req.body.LastName}',…
Mel Habip
  • 46
  • 4
0
votes
0 answers

Stored Procedure with both IN and OUT, with OUT parameter being the last_insert_id() fails

I have this MYSQL Stored procedure that will insert a record and I need the output to give the last_insert_id(). I have tried several different commands, (listed below the procedure) to get this number, but nothing works. here is the stored…
M. Reed
  • 23
  • 3
0
votes
1 answer

MySQL issue on INSERT ... SELECT ON DUPLICATE KEY UPDATE and LAST_INSERT_ID()

In MySQL, I have INSERT ... SELECT ON DUPLICATE KEY UPDATE query as below: $sql = "INSERT INTO user ( name , mobile , email , sex , username …
ugsgknt
  • 115
  • 8
0
votes
0 answers

pdo lastInsertId() returning 0

my pdo last insert id returns 0 here is the code $bdd->beginTransaction(); $requete = $bdd->prepare('INSERT INTO reservation (code_salle,date_res,code_cr,code_profe) VALUE (:code_salle,:date_res,:code_cr,:code_profe)'); …
0
votes
1 answer

Mysql stored procedure returns LAST_INSERT_ID as zero

I have a problem with return output parameter from mysql stored procedure to VBA macro. I wrote this mysql stored procedure InsertProduct to insert new product into database: DELIMITER $$ CREATE PROCEDURE `InsertProduct`(IN `p_modelno` VARCHAR(40),…
0
votes
3 answers

Difference between returning auto increment key and last insert ID in SQL

In our codebase, we use two different ways to retrieve the auto incremented key of a newly inserted row: INSERT ... RETURNING id SELECT LAST_INSERT_ID() Where id is INT UNSIGNED NOT NULL AUTO_INCREMENT when created. Both return the same value…
ranieri
  • 2,030
  • 2
  • 21
  • 39
0
votes
1 answer

Get LAST_INSERT_ID() after INSERT and use it in following queries

I'm inserting a row in one table like: INSERT INTO first (name) VALUES ('something'); and then get the auto-incremented ID of that row like: SELECT LAST_INSERT_ID(); Then, I want to use this particular ID in a series of INSERTs like: INSERT INTO…
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
0
votes
1 answer

PHP SQL assign LAST_INSERT_ID() into a variable

I am trying to assign LAST_INSERT_ID() into a variable. When I use LAST_INSERT_ID() it works but when I do $id = LAST_INSERT_ID(); it doesn't work. Works: INSERT INTO pooruser (userid, name) values (LAST_INSERT_ID(), 'Jack') Doesn't Work: $id =…
Pone
  • 3
  • 1
  • 3
0
votes
0 answers

The identity ID is returned but not recorded in the table

I am building a project in Zand 1.1. I created 'email' table and I want to enter record with the insert method. My code looks like: $this->_db_table = new Mconsole_Model_DbTable_Emailmessages(array( 'db' =>…
0
votes
1 answer

Does last_insert_id() gives session specific value or global value across all sessions(multi-client environment)?

I have a table with AUTO_INCREMENT column in vertica db and using this column as foreign key for some other table. For that i need last inserted value for AUTO_INCREMENT column. CREATE TABLE orders.order_test ( order_id …
Cmannu
  • 1
  • 1
0
votes
2 answers

How do I use SET to create a LAST_INSERT_ID user-defined variables in mysql?

I need to insert values into multiple tables where the primary key (id) of the first table (users) is the foreign key (user_id) of subsequent tables (email etc.). I worked out the following query in MySQL Workbench, which works perfectly fine there.…
miketruk
  • 3
  • 2