question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IN clause array parameter

See original GitHub issue

Hi,

Is it possible to give a array as a parameter. for example in a IN clause?

For example:

var query = "SELECT * FROM myTable WHERE code IN (@datas)"

var params = [
    {
        name: 'datas',
        type: sql.VarChar,
        value: ['095578','095602']
    }
];


async.waterfall([
    function (callback) {
        createConnection(callback);
    },
    function (connection, callback) {
        var request = connection.request();

        params.forEach(function (param) {
            request.input(param.name, param.type, param.value);
        });

    request.query(query, function (err, recordset) {
    if (err) {

        callback(err, null);
    }
    else {
        callback(null, recordset);
    }
    });
    }
], function (err, recordset) {
    if (err) {
        next(err, null);
    }
    else {
        next(null, recordset);
    }
});

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:12

github_iconTop GitHub Comments

29reactions
goforgoldcommented, Aug 2, 2018

For now, I’ve created a function which adds parameters dynamically like below. It is SQL Injection safe and can work for any type (no need to worry about quotes) as I’ve taken type as an argument.

Function:

/**
 * @param request sql request object
 * @param {string} columnName sql table column name
 * @param {string} paramNamePrefix prefix for parameter name
 * @param type parameter type
 * @param {Array<string>} values an array of values
 */
function parameteriseQueryForIn(request, columnName, parameterNamePrefix, type, values) {
  var parameterNames = [];
  for (var i = 0; i < values.length; i++) {
    var parameterName = parameterNamePrefix + i;
    request.input(parameterName, type, values[i]);
    parameterNames.push(`@${parameterName}`);
  }
  return `${columnName} IN (${parameterNames.join(',')})`
}

Usage:

var query = 'SELECT * FROM TableName WHERE ' + parameteriseQueryForIn(queryRequest, 'Name', 'nameParam', sql.NVarChar, ['Name1', 'Name2'])

I still wish if something like below should support, so programmers don’t have to all this and also don’t have to worry about sanitization to prevent injection.

var query = "SELECT * FROM myTable WHERE code IN (@datas)"

var param =  {
    name: 'datas',
    type: sql.VarChar,
    value: ['095578','095602']
};

request.input(param.name, param.type, param.value);

Thanks,

8reactions
zeroowscommented, Mar 3, 2021

I know it’s too late but for anyone else that’s looking for the solution to this problem you can use this

SELECT *
FROM myTable
WHERE code IN (
        SELECT value
        FROM STRING_SPLIT(@datas, ',')
    );

and provided as a comma-separated string

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing an array as a parameter in a WHERE IN clause
I am trying pass in an array of strings (internal_id) to a query. The portion below counts how many elements are in the...
Read more >
Parameterised query Problem with IN clause when passing ...
In my SQL query there is IN condition in where clause and the array of string values are used in IN condition. The...
Read more >
How to pass int array in SQL parameterized query to in clause ...
I have an SQL query that uses string format to insert the int array(pubIds) into the query argument which is set to CommandText...
Read more >
Use a query parameter as a list in an IN clause - Google Support
UNNEST takes an ARRAY and returns a table with a single row for each element in the ARRAY . OR: SELECT [ 1234,5678,9012,3456,7890...
Read more >
Getting error while passing array parameters to select statement
It is not correct for array. Your SQL uses array of employee and array of batchid - 2 arrays. Your input has one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found