0

Hi I have the following statement that I execute using node-oracle

await connection.execute(`SELECT * FROM TABLE WHERE NAME LIKE '%And%'`)

But now I want to bind a parameter instead of using a hard coded value

const queryText = 'And';
await connection.execute(`SELECT * FROM TABLE WHERE NAME LIKE '%:queryText%'`, {queryText});

it throws Error: ORA-01036: illegal variable name/number

What is the correct way of binding a parameter here, since the documentation doesn't cover this situation?

3 Answers 3

2

Try with the following:

const queryText = 'And';
await connection.execute(

"SELECT * FROM TABLE WHERE NAME LIKE :queryText", 

{

queryText: { dir: oracledb.BIND_IN, val: '%'+ queryText +'%', type: oracledb.STRING }

});


Sign up to request clarification or add additional context in comments.

1 Comment

Same error: Error: ORA-00906: missing left parenthesis
0

Use string concatenation:

SELECT * FROM TABLE WHERE NAME LIKE '%' || :queryText || '%'

3 Comments

It throws Error: ORA-00906: missing left parenthesis
@GeorgianStan: it is very unlikely that this query would raise that error. There are no partentheses in the query to start with.
@GeorgianStan TABLE is a reserved word and cannot be used as an (unquoted) identifier. The expected syntax is for a table collection expression SELECT * FROM TABLE(an_array_object) which is why you get the ORA-00906 exception as you are missing the brackets for the rest of the table collection expression following the TABLE keyword - instead name your table something else like SELECT * FROM table_name. fiddle
0

Here is a working example.

let queryText = "John"
let sql = "SELECT * FROM TABLE WHERE NAME LIKE :queryText"
let binds = {queryTarget: {dir: oracledb.BIND_IN, val: queryText, type: oracledb.STRING}}

let result = await connection.execute(sql, binds, options)

Do not add '%' like the other people suggested.

1 Comment

The '%' are needed to do pattern matching. But they are not needed otherwise.

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.