Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array value Passing into SQL Server IN Query

I am using Nodejs Typescript and SQL Server. I wrote query to find array matching values from database. If I pass static array values then data is fetching. When add dynamic array values query is not executing. My query is below.

SELECT Name,id,date FROM venue where name IN('Bangalore','Delhi')

below query is not executing

let input = ["Bangalore","Delhi"]
SELECT Name,id,date FROM venue where name IN('${input}')

What I observed is When I pass dynamic values into query its taking "" quotes ie "Bangalore","Delhi" But query is expecting is single quotes ''

I know In Nodejs we can pass ? and values as parameter but in our case we using typescript and sequelize it has limitation. I unable to use that format.

like image 499
Jay Avatar asked Aug 25 '26 12:08

Jay


1 Answers

You need to use array#map to create quoted city name which will be passed inside IN clause.

const input = ["Bangalore","Delhi"],
      query = `SELECT Name,id,date FROM venue where name IN (${input.map(city => `'${city}'`)})`;
console.log(query);
like image 152
Hassan Imam Avatar answered Aug 27 '26 01:08

Hassan Imam