简体   繁体   中英

Affected rows for stored procedure with node mssql object

I can't seem to get this working. I have an update procedure in Azure SQL.

CREATE PROCEDURE foobar
    @a int
AS
        update foo set bar=@a;
RETURN 0

I was returning the @@rowcount and trying to work with that but it means two resultsets and sloppy code on the client side. The client being a node.js Azure Custom API.

exports.post = function(request, response) {
    var mssql = request.service.mssql;
    var sqlParams = [request.body.a];

    var sql = "foobar ?";

    mssql.query(sql, sqlParams, {
        success: function(results) {
                response.send(statusCodes.OK, results); //return affected rows
        }
    })
};

I've tried using results.affectedRows. I've tried using additional parameters in the function to get a return value. I've queried the database directly and receive '1 Records Affected' as a response. Even when I returned the @@rowcount I had problems specifying it in the javascript. As in I was returning for results: [{"affected":1}] and attempting to access it with results[0].affected/results[0]["affected"] and various other permutations. I tried JSON.parse(results) and tried to access its properties then but still no go. It takes the Azure portal so long to update each time I'm just blindly trying different things at the minute.

Stephen

Of course after all that I found the correct documentation that suggests using queryRaw instead of query and wouldn't you know it, that returns a RowCount.

Stephen

Maybe it is an update since 2015, but the answer is right here .

Also, seems like affectedRows is of mysql, while mssql uses rowsAffected . No queryRaw is required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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