最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - node-mssql Transaction insert - Returning the inserted id..? - Stack Overflow

matteradmin1PV0评论

I'm using node-mssql 3.2.0 and I need to INSERT INTO a table and return the id of the inserted record.

I can successfully use sql.Transaction() to insert data, but the only parameters given to callbacks (request.query() and transactionmit()) are:

const request = new sql.Request();
request.query('...', (err, recordset, affected) => {});

const transaction = new sql.Transaction();
transactionmit((err) => {});

So recordset is undefined for INSERT, UPDATE and DELETE statements, and affected is the number of rows affected, in my case 1.

Does anyone know a good way to obtain an inserted records id (just a primary key id) after a transactionmit() using node-mssql..?

I'm using node-mssql 3.2.0 and I need to INSERT INTO a table and return the id of the inserted record.

I can successfully use sql.Transaction() to insert data, but the only parameters given to callbacks (request.query() and transaction.mit()) are:

const request = new sql.Request();
request.query('...', (err, recordset, affected) => {});

const transaction = new sql.Transaction();
transaction.mit((err) => {});

So recordset is undefined for INSERT, UPDATE and DELETE statements, and affected is the number of rows affected, in my case 1.

Does anyone know a good way to obtain an inserted records id (just a primary key id) after a transaction.mit() using node-mssql..?

Share Improve this question edited Apr 22, 2016 at 15:21 Stephen Last asked Apr 20, 2016 at 13:50 Stephen LastStephen Last 5,78111 gold badges48 silver badges90 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

Instead of just doing an INSERT INTO... statement, you can add a SELECT... statement as well:

INSERT INTO table (...) VALUES (...); SELECT SCOPE_IDENTITY() AS id;

The SCOPE_IDENTITY() function returns the inserted identity column, which means recordset now contains the id:

const request = new sql.Request();
request.query('...', (err, recordset, affected) => {});

I don't think request.multiple = true; is required, because although this includes multiple statements, only one of them is a SELECT... and so returns.

So the answer was SQL related and is not specific to node-mssql.

I know this question has accepted answer. I made the following way:

    let pool = await sql.connect(config);
    let insertItem = await pool.request()
    .input('ItemId',sql.NVarChar, 'itemId1234')
    .input('ItemDesc',sql.NVarChar, 'nice item')
    .query("insert into itemTable (Id, ItemId,ItemDesc) OUTPUT INSERTED.ID 
    values (NEWID(), @ItemId, @ItemDesc);
    var insertedItemId = insertItem.recordset[0].ID

This adds unique identifier to data that is saved to db (if table is created so)

create table itemTable(

       Id UNIQUEIDENTIFIER primary key default NEWID(),
       ItemId nvarchar(25),
       ItemDesc nvarchar(25)
)
Post a comment

comment list (0)

  1. No comments so far