Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

40
Views
SQL assign infinite params

Usually we will use SQL queries like below, we will pass params with predifined number $1

queryRunner.query('SELECT * FROM sample_data WHERE code IN ($1)', ['1'])

But I want to pass multiple params without predifined $1. Any way to resolve this?

queryRunner.query('SELECT * FROM sample_data WHERE code IN ($$)', ['1','2','3'])
7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

One approach dynamically builds the IN clause based on the expected number of parameters. Consider:

var params = ['1','2','3'];
var inClause = '?' + ', ?'.repeat(params.length - 1);
var sql = 'SELECT * FROM sample_data WHERE code IN (' + inClause + ')';
console.log(sql);

Once we have a statement with the right number of placeholders, we can simply bind the collection or array with no trouble.

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs