繁体   English   中英

使用Prepare语句插入数据不会返回插入的记录

[英]Inserting data using Prepare statement doesn't return inserted record

我正在使用node-sql 当我尝试使用Prepare Statement在表中插入记录时,recordSet未定义。 下面是示例代码行。

ps.execute(data, function (err, recordSet, affected) { });

虽然记录已成功插入数据库,但它在回调函数中给了我未定义的变量。

您没有分享您的陈述实际上所做的事情。 但是,如果要使用prepare语句插入记录。 然后可以使用“ returnValue”或“受影响的”回调参数来代替recordSet。 作为节点SQL状态,如果您正在运行将返回一些recordSet的选择查询,则记录集将具有值。 看到

request.execute('record', function(err, recordsets, returnValue, affected) {
    // ... error checks

    console.log(recordsets.length); // count of recordsets returned by the procedure
    console.log(recordsets[0].length); // count of rows contained in first recordset
    console.log(returnValue); // procedure return value
    console.log(recordsets.returnValue); // same as previous line
    console.log(affected); // number of rows affected by the statemens
    console.log(recordsets.rowsAffected); // same as previous line

    console.log(request.parameters.output_parameter.value); // output value

    // ...
});

根据node-mssql文档(我已经讲完了,相信我),记录集或记录集是在执行选择查询时返回的集。 由于您正在执行插入查询,因此很可能是未定义的。 因为即使必须返回记录集,它也会是整个集合(包括新插入的行),在插入中不是很有用:)

简而言之,如果您的查询不包含选择,您将获得未定义的记录集。 希望这可以帮助。

是的,我找到了答案,我们需要指定要使用OUTPUT子句返回的记录。 喜欢:

INSERT INTO Table OUTPUT.Id VALUES(...);

暂无
暂无

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

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