简体   繁体   English

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

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

I am using node-sql . 我正在使用node-sql recordSet is undefined when i try to insert record in a table using Prepare Statement. 当我尝试使用Prepare Statement在表中插入记录时,recordSet未定义。 Below is the sample line of code. 下面是示例代码行。

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

Although record is successfully inserted into database but it gives me undefined variable in callback function. 虽然记录已成功插入数据库,但它在回调函数中给了我未定义的变量。

You didn't share what your statement actually do. 您没有分享您的陈述实际上所做的事情。 however if you are inserting record using prepare statements. 但是,如果要使用prepare语句插入记录。 then instead of recordSet you can use "returnValue" or "affected" call back parameters. 然后可以使用“ returnValue”或“受影响的”回调参数来代替recordSet。 As node-sql states record set will have values if you are running selection queries that will return some recordSet. 作为节点SQL状态,如果您正在运行将返回一些recordSet的选择查询,则记录集将具有值。 See 看到

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

    // ...
});

According to node-mssql documentation (I've gone through it all, believe me) , the recordset or recordsets are sets returned when you execute a select query. 根据node-mssql文档(我已经讲完了,相信我),记录集或记录集是在执行选择查询时返回的集。 Since you are executing an insert query, it is most likely to be undefined. 由于您正在执行插入查询,因此很可能是未定义的。 Because even if recordset has to be returned, it would be the whole set (including your newly inserted row) which isn't very useful in insertion :) 因为即使必须返回记录集,它也会是整个集合(包括新插入的行),在插入中不是很有用:)

In short, if your query doesn't contain selection, you'll get recordset undefined. 简而言之,如果您的查询不包含选择,您将获得未定义的记录集。 Hope this helps. 希望这可以帮助。

Yes i found an answer, We need to specify records to be returned using the OUTPUT clause. 是的,我找到了答案,我们需要指定要使用OUTPUT子句返回的记录。 Like: 喜欢:

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

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

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