Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call MySql procedures from Node JS

I want to call a stored procedure from MySql node:

How do I call it? The documentation says:

You can call stored procedures from your queries as with any other mysql driver. If the stored procedure produces several result sets, they are exposed to you the same way as the results for multiple statement queries

I tried searching for it on internet but got very old results which do not work anymore.

I tried:

connection.query('procedure_name()', {84,Bhuwan}, function(err, result) {
    connection.destroy();
    if (err)
      throw err;
    callback(err, result);
});

But I am getting error.

Can anyone provide a proper syntax for it??

like image 556
writeToBhuwan Avatar asked Sep 29 '14 00:09

writeToBhuwan


1 Answers

You have to use 'call' command and if you have parameters to pass to the query, you need to add '?' marks. Check the code.

connection.query("call procedure_name(?,?)", [param1, param2], function (err, result) {
    if (err) {
        console.log("err:", err);
    } else {
        console.log("results:", result);
    }

});
like image 155
Madura Pradeep Avatar answered Sep 29 '22 23:09

Madura Pradeep