Harsh Vardhan
Harsh Vardhan

Reputation: 35

Calling multi-parameterized Azure SQL stored procedure from Nodejs

I am looking forward to know how can I run an Azure SQL stored procedure with multiple input parameters from Nodejs.

For example, if I have a stored procedure FIND_USERS(activity_status, color, gender) which runs a query

select * from users where isActive = activity_status and bay_color = color and userGender = gender; 

I should be able to call this stored procedure from nodejs with the input parameters. The thing to understand here is that I want have a SQL transaction service that can take any CALL PROCEDURE type command along with the set of input parameters and call the procedure using those input parameters irrespective of the number of input parameters.

What I know is that for MySQL, there is a mysql library which lets me run procedures with multiple parameters. I have encapsulated it in MySQLConnector.js service as

var mysql   = require('mysql');

exports.query = function(sql, values, next) {

    if (arguments.length === 2) {
        next = values;
        values = null;
    }
    var connection = mysql.createConnection({
        host:host,
        user:user,
        password:password,
        database:database
    });
    connection.connect(function(err) {
        if (err !== null) {
            console.log("[MYSQL] Error connecting to mysql:" + err+'\n');
            console.log(err == 'Error: ER_CON_COUNT_ERROR: Too many connections')
            if(err == 'Error: ER_CON_COUNT_ERROR: Too many connections'){
              connection.end();
            }
        }
    });
    connection.query(sql, values, function(err) {
        connection.end();
        if (err) {
            throw err;
        }
        next.apply(this, arguments);
    });
}

With this, I can call a stored procedure from nodejs with a function like

MySQLConnector.query('CALL FIND_USERS (?, ?, ?)', [1, 'blue', 'female'], function(err, userData) {
  //do something with userData
});

How is it possible to do this for Azure MS SQL?

Upvotes: 0

Views: 672

Answers (2)

Alberto Morillo
Alberto Morillo

Reputation: 15698

Make use of Edje.js and you should create a function and send the parameters when you call the function.

getHorarioFarmacia({pSede:'Sucursal Parrita'}, function (error, result){....

}

For more details, read the comments made by Luis Diego Pizarro here.

Upvotes: 0

Suraiya Hameed
Suraiya Hameed

Reputation: 21

You can use tedious driver to connect to SQL Server. It supports both input+output parameter for statements and SPs, you can find the example in http://tediousjs.github.io/tedious/parameters.html

Feels free to raise an issue in GitHub if you need more assistance.

Upvotes: 1

Related Questions