3

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.

James Z
  • 12,209
  • 10
  • 24
  • 44
Jay
  • 187
  • 2
  • 13

2 Answers2

5

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);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

May be the values inside IN clause is passing as an array. Please take a look at this array value passing inside IN clause