Hambach
Thomas

2015-07-20 04:36:03

Node ODBC MSSQL Last Insert ID

Breaking your head over how to get theĀ last insert id using node and ODBC for MSSQL? So was I. If you perform an insert query such as:

db.query('INSERT INTO Users (name) VALUES ("Jane Doe");

Followed by

db.query('SELECT SCOPE_IDENTITY()');

You will be out of scope and get a nice NULL object in your callback. The solution is to perform both queries in once call.

db.query('INSERT INTO Users (name) VALUES ("Jane Doe"); SELECT SCOPE_IDENTITY() as lastInsertId');