2

I am trying to execute an update statement in node.js on DB2 database. The update operation works but I don't know how to get the count of updated records in DB2 after query execution. Any help will be appreciated. Below is my code:

conn.beginTransaction(function (err) {
if (err) {
//could not begin a transaction for some reason. 
console.log(err);
return conn.closeSync();
}
var stmt = ("Update tableA set col1='abc' where col2='xyz'");
var result = conn.querySync(stmt);
conn.commitTransaction(function (err){
if (err) {
console.log(err);
return conn.closeSync();
}
});
console.log('Rows affected :: '+ result.numRows());
});
saurabhg07
  • 23
  • 3

1 Answers1

4

The NPM documentation seems a bit incomplete. For DML statements you probably should use ODBCStatement.executeNonQuerySync(), which, according to the source code (line 413), returns the number of affected rows.

If you're working with a recent version of DB2 for LUW, as an alternative you can wrap your UPDATE in a data change table reference:

var stmt = ("select count(*) from final table (" + 
            "Update tableA set col1='abc' where col2='xyz'"+
            ")");
var result = conn.querySync(stmt);

result will contain the number of affected rows.

mustaccio
  • 18,234
  • 16
  • 48
  • 57