How to Get Last Inserted Id

How do I get the last inserted ID of a MySQL table in PHP?

If you're using PDO, use PDO::lastInsertId.

If you're using Mysqli, use mysqli::$insert_id.

If you're still using Mysql:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

But if you have to, use mysql_insert_id.

Get the last inserted row ID (with SQL statement)

If your SQL Server table has a column of type INT IDENTITY (or BIGINT IDENTITY), then you can get the latest inserted value using:

INSERT INTO dbo.YourTable(columns....)
VALUES(..........)

SELECT SCOPE_IDENTITY()

This works as long as you haven't inserted another row - it just returns the last IDENTITY value handed out in this scope here.

There are at least two more options - @@IDENTITY and IDENT_CURRENT - read more about how they works and in what way they're different (and might give you unexpected results) in this excellent blog post by Pinal Dave here.

LAST_INSERT_ID() MySQL

You could store the last insert id in a variable :

INSERT INTO table1 (title,userid) VALUES ('test', 1); 
SET @last_id_in_table1 = LAST_INSERT_ID();
INSERT INTO table2 (parentid,otherid,userid) VALUES (@last_id_in_table1, 4, 1);

Or get the max id from table1 (EDIT: Warning. See note in comments from Rob Starling about possible errors from race conditions when using the max id)

INSERT INTO table1 (title,userid) VALUES ('test', 1); 
INSERT INTO table2 (parentid,otherid,userid) VALUES (LAST_INSERT_ID(), 4, 1);
SELECT MAX(id) FROM table1;

(Warning: as Rob Starling points out in the comments)

how to get last insert id after insert query in codeigniter active record

Try this

function add_post($post_data){
$this->db->insert('posts', $post_data);
$insert_id = $this->db->insert_id();

return $insert_id;
}

In case of multiple inserts you could use

$this->db->trans_start();
$this->db->trans_complete();

How get last inserted ID in SQL Server 2012

Assuming table is

create table test
(
id int IDENTITY(1,1) PRIMARY KEY
, testData varchar(100) NOT NULL
)

this works

INSERT INTO dbo.Test (testData) VALUES ('test')
SELECT @@IDENTITY AS ID

as does this (better answer)

INSERT INTO dbo.Test (testData) VALUES ('test')
SELECT SCOPE_IDENTITY() AS ID

I don't have enough information to know why yours does not work. It is probably in the table structure? I would need more information to answer this.

How to obtain last insert id?

You should alter a bit your sql statement:

string iquery = "INSERT INTO [User] 
(username, password, role)
VALUES (@username,@password,@role)
SELECT SCOPE_IDENTITY()";

Basically, SCOPE_INDETITY

Returns the last identity value inserted into an identity column in
the same scope.

For further info, please have a look here.

Furthermore, you don't need cmd.ExecuteNonQuery(); before

int id = Convert.ToInt32(cmd.ExecuteScalar());

Get the Last Inserted Id Using Laravel Eloquent

After save, $data->id should be the last id inserted.

$data->save();
$data->id;

Can be used like this.

return Response::json(array('success' => true, 'last_insert_id' => $data->id), 200);

For updated laravel version try this

return response()->json(array('success' => true, 'last_insert_id' => $data->id), 200);

Get the LIST of last inserted id(s) after INSERT INTO in the same mysql table

I believe you are looking for this simple query:

select id
from my_table
where id >= LAST_INSERT_ID();

As explained in the documentation:

With no argument, LAST_INSERT_ID() returns a BIGINT UNSIGNED
(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.

Laravel: get last Insert id from query builder

Try it once :-

$id = DB::getPdo()->lastInsertId();

Select id from last inserted row

You can use the SCOPE_IDENTITY() in your stored procedure to get last inserted record id.

SELECT SCOPE_IDENTITY() :- It will returns the last identity value inserted into an 
identity column in the same scope.

SELECT @@IDENTITY :- @@IDENTITY will return the last identity value entered
into a table in your current session.

SELECT IDENT_CURRENT(‘tablename’) :- IDENT_CURRENT is not limited by scope and
session; it is limited to a specified table.

There are other options also such as

1. Select Top 1 id From Table Order by id desc

2. SELECT max(id) FROM table

Refer msdn http://msdn.microsoft.com/en-us/library/ms190315.aspx
and
http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/



Related Topics



Leave a reply



Submit