question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with getting last inserted ID

See original GitHub issue

Anybody have issues with retrieving the last inserted ID from db after an insert statement?

Assume a table student contains 3 fields (id, age, name). Here’s my code:

        var query = "INSERT INTO student(age, tagname) values (6, 'Bob') ";
        db = new sql.Request();
        db.query(query, function(err, data) {
        if (!err)
        {
            var str = "SELECT @@IDENTITY AS 'identity'";    
            db = new sql.Request();
            db.query(str, function(suberr, subdata)
            {
                console.log(" this is LAST inserted id : ");
                console.log(subdata);   // -> RETURNED NOTHING
            });
        }

The above code returned nothing as the ID eventhough the student record was captured correctly into the db.

OUTPUT

 this is last LAST inserted id :
[ { identity: null } ]

I am wondering if there’s something wrong with my code, or if this is a bug. Any feedbacks are appreciated.

PS. If I do another query in between the INSERT and SELECT @@IDENTITY, be it a select query, or insert query, then it would give me the ID that I am looking for.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8

github_iconTop GitHub Comments

8reactions
Lokslycommented, Jun 2, 2016

Just for the record, as you cannot warranty you are using the same connection of the pool, for both the insert and the select statement, you may choose to use a PreparedStatement or simply use the same query for both of them. Use it like this:

var query = "INSERT INTO demoTable(someValue) values ('someValue')";
query = query + ';select @@IDENTITY AS \'identity\''; // ---- this is what worked

request.query(str, function(suberr, subdata){
    console.log("Querying @@IDENTITY : ");
    console.log(subdata[0].identity);   // -> RETURNED VALUE
});
0reactions
dhensbycommented, Apr 8, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with Mysql and using the function last insert ID
By the time you SELECT the last inserted id , another session could have INSERT ed a new id , and that would...
Read more >
Issue in getting last inserted Id from table - Help
Dear Folks, I am trying to save my file data in sql database. Table 2 PageContent which have column ,Page Number, Page Content...
Read more >
mysqli::$insert_id - Manual - PHP
Returns the ID generated by an INSERT or UPDATE query on a table with a column having the AUTO_INCREMENT attribute. In the case...
Read more >
[resolved] Get last insert ID of a specific table
I tried to use the tMySQLLastInsertId component, but I still have the problem : for the same iteration, no value returned. I used...
Read more >
Problem getting inserted ID via PDO - Slim Framework
I am having a bit of a problem for getting the last id for my insert, i have a customer_id field and its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found