1

I would like to get back the primary key of the last inserted row in DB2 form node.js using ibm_db package. I tried using

select id from NEW TABLE (insert into (val1, val2, ...) values ('lorem', 'ipsum', ...))

and .prepare(sql, callback) to make DB call in node.js. The result in the callback doesn't contain any property or method to get the auto-generated key. Any help is appreciated.

1 Answer 1

2

Yes, you need to fetch the data from result after execute as below:

  conn.prepare("select * from mytab1", function (err, stmt) {
    if(err) {
      console.log(err);
      return conn.closeSync();
    }
    stmt.execute([], function(err, result) {
      if(err) console.log(err);
      else {
        data = result.fetchAllSync();
        console.log("Fetched Data = " );
        console.log(data);
        result.closeSync();
        conn.close(function () { console.log('done'); });
      }
    });
  });

Check the ibm_db/test/test-basic-test.js file for details. Thanks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.