简体   繁体   English

Node mssql:SQL插入后返回id

[英]Node mssql: return id after SQL insertion

I am working on an integration between Mongodb and SQL Server, inserting and updating registers from MongoDb to SQL Server database with a scheduled process, using the mssql package to achieve this. 我正在开发Mongodb和SQL Server之间的集成,使用mssql包实现这一点,使用调度进程从MongoDb插入和更新寄存器到SQL Server数据库。

Each time a new register is inserted, I want the new row id to be retrieved back, because I need it to perform another insertions of subregisters. 每次插入新寄存器时,我都希望检索新的行ID,因为我需要它来执行另一次子寄存器的插入。

I tried the following, expecting to have some information retrieved in the result of the query. 我尝试了以下方法,期望在查询结果中检索到一些信息。 The query actually inserts the row, but result is returned as undefined : 查询实际上插入了行,但result返回为undefined

var sql = require('mssql');
var connectionConfig = 'xxxxxxxxxxxx';
var insertionQuery =   'xxxxxxxxxxxx';

sql.connect(connectionConfig).then(pool => {
    pool
      .request()
      .query(insertionQuery);    

}).then(result => {
        //I expect 'result' to have information of the inserted row, but it comes undefined instead
        cosole.log(result);

}).catch(err => {
        console.err(err);
});

I think using stored procedures may work around this, but I just want to avoid using a stored procedure for simple inserts. 我认为使用存储过程可能会解决这个问题,但我只是想避免使用存储过程进行简单的插入。

Does anyone knows how to have the new row id back after insertion? 有谁知道如何在插入后重新获得新的行ID?

EDIT 编辑

I did include OUTPUT SCOPE_IDENTITY() in my SQL statement - my query now looks like: 我在我的SQL语句中包含了OUTPUT SCOPE_IDENTITY() - 我的查询现在看起来像:

INSERT INTO TABLE (columns...) 
OUTPUT SCOPE_IDENTITY() 
VALUES (values...)

If I run the query in my SQL Server Management Studio, it works just fine and I got my new row id returned, but when running the query through the mssql package's query method, result is returned like this: 如果我在我的SQL Server Management Studio中运行查询,它工作正常,我返回了新的行ID,但是当通过mssql包的query方法运行查询时, result返回如下:

[ { '': null } ]

I quote from node-mssql insert returning undefined recordset : 我引用node-mssql insert返回undefined recordset

INSERT statement doesn't return a recordset so recordset is undefined. INSERT语句不返回记录集,因此recordset未定义。 Please see this section of the docs to learn more about how to get number of affected rows. 请参阅文档的此部分 ,以了解有关如何获取受影响行数的详细信息。

An option to solve this is to add a SELECT statement after your INSERT and ask for SCOPE_IDENTITY() or @@IDENTITY . 解决此问题的一个选项是在INSERT之后添加SELECT语句并请求SCOPE_IDENTITY()@@IDENTITY That will get you the ID of the last inserted objects, ie: INSERT INTO table (...) VALUES (...); SELECT SCOPE_IDENTITY() AS id; 这将获得最后插入的对象的ID,即: INSERT INTO table (...) VALUES (...); SELECT SCOPE_IDENTITY() AS id; INSERT INTO table (...) VALUES (...); SELECT SCOPE_IDENTITY() AS id;

If anyone comes having problems with GUID, since you cannot set it as an Identity column, you must do something like: 如果有人遇到GUID问题,因为您无法将其设置为Identity列,您必须执行以下操作:

const result = await pool
    .request()
    .input('orgName', sql.VarChar, orgName)
    .input('tier', sql.Int, tier)
    .input('status', sql.Int, status)
    .input('createDate', sql.Date, new Date())
    .input('statusChangeDate', sql.Date, new Date())
    .query(`INSERT INTO app.Organizations (OrgName, 
Tier, Status, CreateDate, StatusChangeDate)
 OUTPUT inserted.OrgID
 VALUES (@orgName, @tier, @status, @createDate, @statusChangeDate);
`)

Where the orgID is the auto generated GUID. orgID是自动生成的GUID。 Also don't follow my new Date() example as it inserts the date as tomorrow... I need to fix that. 也不要按照我的新Date()示例,因为它将日期作为明天插入...我需要解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM