tediousjs / node-mssql

Microsoft SQL Server client for Node.js
https://tediousjs.github.io/node-mssql
MIT License
2.23k stars 465 forks source link

Get newly added data in result #1522

Closed sleepinzombie closed 1 year ago

sleepinzombie commented 1 year ago

I am using a Stored Procedure to add a new data from my Express app to the database. The creation of the new entity is successfully inserted but I need the newly added entity to the database in the response.

Is there a way I can achieve this? Currently the response I get is as follows:

{
   recordsets: [],
   recordset: undefined,
   output: {},
   rowsAffected: [
     0, 1, 1, 1, 1,
     1, 1, 1, 1, 1,
     1, 1
   ],
   returnValue: 0
 }
sleepinzombie commented 1 year ago

I am thinking that I will need to add a SELECT statement in my stored procedure using SCOPE_IDENTITY to get the newly inserted record.

But is there any other option?

dhensby commented 1 year ago

The library supports the OUTPUT clause in SQL so nothing special needs to be done for this to work in the library.

You'll need to write an SP/query that outputs the data you need.