-
Notifications
You must be signed in to change notification settings - Fork 463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with getting last inserted ID #32
Comments
Is anyone else having the same problem? Or is this not a bug? Cheers. |
I would like to recommend POST these type of questions in StackOverflow, there is tag for this project: I did this I created this table: CREATE TABLE demoTable( id INT IDENTITY(1,1) NOT NULL, someValue VARCHAR(20) NOT NULL, ) Then I created this demo code and it worked fine: var connection = new self.sql.Connection(DBConfig, function (err) { console.log("Demo Query"); var query = "INSERT INTO demoTable(someValue) values ('someValue') "; var request = new sql.Request(connection); request.query(query, function(err, data) { console.log("Request Query 1 - Insert"); if (!err) { var str = "SELECT @@IDENTITY AS 'identity'"; request = new sql.Request(connection); request.query(str, function(suberr, subdata) { console.log("Querying @@Indentity : "); console.log(subdata); // -> RETURNED NOTHING }); } else{ console.log(err); } }); }); |
Closing due to inactivity. |
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:
|
Hi guys, I know this issue is closed but still decided to ask. I currently need the same thing as the author of the thread. I need to insert a row and as a result get back its id. Now, I see that the answers here would do the job but is it really true that this functionality is not part of the mssql library? I mean I kind of expected when I insert a row to get back what's been inserted in the result along with any fields that were set upon inserting (like id). @ocpuso , you probably did research before posting this so what do you think? |
I am having the same question. there are no ways to return the last insert id
|
This comment has been minimized.
This comment has been minimized.
This was answered 3 years ago #32 (comment) |
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:
The above code returned nothing as the ID eventhough the student record was captured correctly into the db.
OUTPUT
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.
The text was updated successfully, but these errors were encountered: