3

I am trying to get rows where id in 1,2,3.

Here is my code:

app.use('/', async function(req, res, next){
    try{
        var query = await connection.execute(`SELECT * FROM ERROR_LIST WHERE ID IN :1`, [[1,2,3]]);
        console.log(query);
        res.send(query.rows)
    } catch(err) {
        next(err);
    }
   next();
});

As the result: ORA-01484: arrays can only be bound to PL/SQL statements

2 Answers 2

3

The node-oracledb documentation has a whole section Binding Multiple Values to a SQL WHERE IN Clause.

You'll need one of the solutions, the simplest of which is like:

const sql = `SELECT last_name FROM employees WHERE first_name IN (:bv1, :bv2, :bv3, :bv4)`;
const binds = ['Alyssa', 'Christopher', 'Hazel', 'Samuel'];
const result = await connection.execute(sql, binds);
Sign up to request clarification or add additional context in comments.

2 Comments

what if you do not know how many elements come inside binds array?
The documentation link in the answer talks about that and gives solutions.
0

You can use join() on an Array like so:

app.use('/', async function(req, res, next){
    try{
        var numbers = [1,2,3]
        var query = await connection.execute(`SELECT * FROM ERROR_LIST WHERE ID IN :1`, [numbers.join(',')]);
        console.log(query);
        res.send(query.rows)
    } catch(err) {
        next(err);
    }
   next();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.