How to Get a Value from the Last Inserted Row

Accessing last inserted row in mysql

Use this

mysql_insert_id(&mysql);  

as its basic structure are

mysql_insert_id ([ resource $link_identifier = NULL ] )

Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).

or in mysql use

   SELECT LAST_INSERT_ID();

here is the ref links

http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

http://php.net/manual/en/function.mysql-insert-id.php

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/

How to get last inserted row from table?

row_number() is the typical way of doing this:

select t.*
from (select t.*,
row_number() over (partition by id order by date desc) as seqnum
from t
) t
where seqnum = 1;

How to get last inserted row value from a column in string format?

use below way

    $this->db->select('i_stud_id');
$this->db->order_by('i_stud_id','desc'); //i_stud_id is auto_increment
$this->db->limit(1);
$query = $this->db->get('iproj_stud_course');
$studentId=$query->row_array();
echo $studentId['i_stud_id'];

How to get a value from the last inserted row using Codeigniter

try this code:

$this->db->insert_id();

Refer:
https://www.codeigniter.com/user_guide/database/helpers.html

How to get a value from the last inserted row?

With PostgreSQL you can do it via the RETURNING keyword:

PostgresSQL - RETURNING

INSERT INTO mytable( field_1, field_2,... )
VALUES ( value_1, value_2 ) RETURNING anyfield

It will return the value of "anyfield". "anyfield" may be a sequence or not.

To use it with JDBC, do:

ResultSet rs = statement.executeQuery("INSERT ... RETURNING ID");
rs.next();
rs.getInt(1);

mysql how to get the last inserted row values in a trigger

You could use OLD. or NEW. since this trigger is fired after the insert both values are the same. You can access all properties of the interred row (if it is a row level trigger) by using:

NEW.orderItemID
NEW.rate

etc.

how to get id of last inserted row

So a little trick to do this is to change you statement to this:

INSERT INTO tbl (...) VALUES (...); SELECT LAST_INSERT_ID()

and then insert the row with ExecuteScalar which will return you the id; you can then update the object accordingly. So the entire snippet might look like this:

using (var conn = new MySqlConnection(connString))
using (var cmd = new MySqlCommand(sql, conn))
{
var result = cmd.ExecuteScalar();
int id;
if (int.TryParse(result, out id))
{
// set the object's id here
}
}


Related Topics



Leave a reply



Submit