I'm using mssql. After inserting a record,I want to get the id of the data. But I don't know hot to do that. My code is below. please give me answer.
var mssql = require('mssql');
mssql.connect(config.mssql, function(err) {
var request = new mssql.Request();
request.query('insert -----'),function(err, data) {
console.log(data);
}
Insert worked properly,but console log is [undefined] ....
this is the ddl of the table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Feature](
[id] nvarchar NOT NULL CONSTRAINT [DF_Feature_id] DEFAULT (CONVERT(nvarchar,newid(),(0))),
[createdAt] datetimeoffset NOT NULL CONSTRAINT [DF_Feature_createdAt] DEFAULT (CONVERT(datetimeoffset,sysutcdatetime(),(0))),
[updatedAt] datetimeoffset NULL,
[version] [timestamp] NOT NULL,
[deleted] [bit] NULL DEFAULT ((0)),
[title] nvarchar NULL,
[text] nvarchar NULL,
[period_from] datetimeoffset NULL,
[period_to] datetimeoffset NULL,
[priority] [float] NULL,
PRIMARY KEY NONCLUSTERED
(
[id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF)
)
GO
SELECT SCOPE_IDENTITY()after theINSERTstatement in the same batch. Alternatively, specify anOUTPUTclause (OUTPUT inserted.ID)), which can also handle multi-row inserts. See learn.microsoft.com/en-us/sql/t-sql/queries/…