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
2 answers

Get last id from MySQL

I'm trying to get the last ID from MySQL and assign the value to string a. I have a table admin with a adminID column. adminAPI api= new adminAPI(); try { String id= api.selectLastID(); …
AI.
  • 934
  • 2
  • 14
  • 30
0
votes
2 answers

Get the last inserted string Id in SQL Server & C#

I have a DataTable BonLivraison with a primary key that is a string with the format 2016/ + increment number. I used this code to generate this primary key: SqlCommand cmdRow = new SqlCommand("select TOP(1) CodeBonLivraison from BonLivraison ORDER…
user4340666
  • 1,453
  • 15
  • 36
0
votes
1 answer

Spring Batch Could not obtain last_insert_id(); nested exception is java.sql.SQLException: Lock wait timeout exceeded;

I am using spring batch 3.0.5.RELEASE on MySQL database. I have a job which reads from multiple tables and process the records and mark their status after completion. The job data is designed in such a way that multiple instances of the same job can…
0
votes
1 answer

Is it possible to use PDO lastInsertId() when the table was previously locked?

This question is again important to me. Does anyone have a solution? $conn = new PDO('mysql:dbname=test;host=127.0.0.1', 'root', '********'); $conn->exec('CREATE TABLE testIncrement ' . '(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name…
Nikola F
  • 21
  • 2
0
votes
1 answer

MySQL insert - for each row, return insert id

Let's say I have an array of data to insert. I'm going to have an insert that looks like this: INSERT INTO `table` (`name`, `value`) VALUES('name1','value1'),('name2','value2') We're assuming that table has a primary key. Is there a way to, while…
James Spence
  • 2,020
  • 1
  • 16
  • 23
0
votes
1 answer

Could not retrieve generated id after insert nhibernate sql server

I am building an C# application using NHibernate and SQL Server. When I try to save an entity (using identity generator) I get the error 'Could not retrieve generated id after insert'. Does anyone know why I actually get this error? Did I…
Dominic Jordan
  • 65
  • 1
  • 12
0
votes
2 answers

PHP MySQL PDO: get last inserted ROW not ID

I have currently a script to insert new data automatically when using the form to add a new element. I use public string PDO::lastInsertId ([ string $name = NULL ] ) Logic of course. Now I am only wondering if this might cause any problems in case…
Alex
  • 1,223
  • 1
  • 19
  • 31
0
votes
1 answer

How to get last inserted id in SQL Server 2000 using ODBC?

I'm using this code to insert new record to database: function newProtocol($date, $type, $text, $who, $reff) { $sql = "INSERT INTO c_rozhovor (ref_kunde, protokol_typ, besuch_protokol, datum_vzniku, kto_vytvoril) VALUES ('" …
Axel Stone
  • 1,521
  • 4
  • 23
  • 43
0
votes
2 answers

PDO transaction last inserted id in table1 to be used in table2

I have a form where I publish data to two different db tables. You can see my Transaction below. $db->beginTransaction(); $sql = "INSERT INTO clients (name, contact_person, phone, email, url) VALUES (:name, :contact_person, :phone,…
user1379533
0
votes
1 answer

Do BEFORE UPDATE triggers affect mysqli_insert_id results?

I have a mysql table which has a trigger attached, that logs changes in this table to a second one CREATE TRIGGER log_table BEFORE UPDATE ON table1 FOR EACH ROW BEGIN INSERT INTO log_table(filed) VALUES(NEW.field); END; // Now if I perform…
JochenJung
  • 7,183
  • 12
  • 64
  • 113
0
votes
2 answers

how to get array of latest auto incremented id's?

One single Query to insert multiple rows : $My query = "INSERT INTO exp_channel_titles(channel_id,author_id,ip_address) VALUES(11,1,'203.109.119.2'),(11,1,'203.109.119.2'),(11,1,'203.109.119.2')"; What I want : array of auto incremented id. what…
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
0
votes
1 answer

MySQL - Output of last select in procedure with update and insert

I have a stored procedure that executes the following statements: A select to populate a variable An update utilizing said variable Another select to populate a second variable An insert using the second variable A select of last_insert_id() to…
TheBeege
  • 134
  • 1
  • 1
  • 13
0
votes
1 answer

How to get last id for relationships in a transaction between models in Codeigniter?

I have this code inside a function in user-controller. I'm, using Codeigniter. $name_user = $this->input->post('contact-company'); $company_name = $this->input->post('name-company'); $company_orgnr =…
0
votes
2 answers

MySQL INSERT ... SELECT from 1 table into 2 tables

I'm seeing a fair number of questions dealing with INSERTing into a single table from multiple tables. I'm trying to do the opposite, and using only MySQL. I have a temporary table that contains denormalized data. I need to iterate through each row…
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
0
votes
1 answer

How to best efficiently get multiple last_insert_ids

I have an SQL INSERT statement like this: INSERT INTO my_tickets( tk_type, tk_qty) VALUES('typeValue1','qtyValue1'),('typeValue2', 'qtyvalue2'),('typeValue3', 'qtyValue3'); SET @lastId = LAST_INSERT_ID(); I realise that LAST_INSERT_ID() will only…
okey_on
  • 2,888
  • 8
  • 28
  • 36