Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL LAST_INSERT_ID not working as intended. How do I fix?

I wrote MySQL StoredProcedure to create and return new ID for each table value, however, it gets wrong value on last_insert_id() from MySQL WorkBench and Java application.

This procedure will be called from multiple sessions.

CALL `GET_NEW_ID`('test', @test);
select @test;

It gives me "141215000000" and this means last_insert_id() returns 0 all the time. I see it correctly inserts new data into seq_data as supposed though.

CREATE PROCEDURE `GET_NEW_ID`(IN V_TABLE VARCHAR(10), OUT V_ID VARCHAR(12))
BEGIN

    INSERT INTO seq_data ( id, `name`, `stat_date`)
    SELECT IFNULL(MAX(id), 0)+1, V_TABLE, DATE_FORMAT(NOW(),'%Y%m%d') FROM seq_data WHERE name = V_TABLE AND stat_date = DATE_FORMAT(NOW(),'%Y%m%d');
    SET V_ID = concat(DATE_FORMAT(NOW(),'%y%m%d'),LPAD(LAST_INSERT_ID(), 6, '0'));

END

Table looks like this.

CREATE TABLE `seq_data` (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) COLLATE utf8_bin NOT NULL,
  `stat_date` varchar(8) COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`id`,`name`,`stat_date`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

My goal is like...

CALL `GET_NEW_ID`('test', @test);
select @test;

return 141215000001

CALL `GET_NEW_ID`('test', @test);
select @test;

return 141215000002

CALL `GET_NEW_ID`('hello', @test);
select @test;

return 141215000001

like image 887
handicop Avatar asked Dec 15 '14 01:12

handicop


People also ask

What is LAST_INSERT_ID in MySQL?

The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted or updated in a table.

What will be the value of LAST_INSERT_ID for the newly created table?

With no argument, LAST_INSERT_ID() returns a 64-bit value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement. The value of LAST_INSERT_ID() remains unchanged if no rows are successfully inserted.

How do I get the unique ID for the last inserted row?

If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function.

How do I get the last insert ID from a specific table?

How to get last inserted id of a MySQL table using LAST_INSERT_ID() We will be using the LAST_INSERT_ID() function to get the last inserted id. Last_insert_id() MySQL function returns the BIG UNSIGNED value for an insert statement on an auto_increment column.


1 Answers

As stated in the MySQL documentation, LAST_INSERT_ID() returns a BIGINT (64-bit) value representing the first automatically generated value that was set for an AUTO_INCREMENT column by the most recently executed INSERT statement to affect such a column.

In your case, you are inserting the id, so an AUTO_INCREMENT value is not generated, thus LAST_INSERT_ID returns 0.

You may try something like:

CREATE PROCEDURE `GET_NEW_ID`(IN V_TABLE VARCHAR(10), OUT V_ID VARCHAR(12))
BEGIN

    INSERT INTO seq_data (`name`, `stat_date`)
    SELECT V_TABLE, DATE_FORMAT(NOW(),'%Y%m%d') FROM seq_data WHERE name = V_TABLE AND stat_date = DATE_FORMAT(NOW(),'%Y%m%d');
    SET V_ID = concat(DATE_FORMAT(NOW(),'%y%m%d'),LPAD(LAST_INSERT_ID(), 6, '0'));

END
like image 131
Victor Dodon Avatar answered Nov 07 '22 12:11

Victor Dodon