Mayank
Mayank

Reputation: 5728

Postgresql prepare statement with operator ANY

SELECT * FROM tbl_emp WHERE interest = $1 AND emp_id = ANY(?)

Is the above statement correct to be used in function PQprepare?

If yes, what should be the value for nParams and how would PQexecPrepared be called?

Regards, Mayank

Upvotes: 0

Views: 1953

Answers (1)

Peter Eisentraut
Peter Eisentraut

Reputation: 36729

If you're trying to prepare something like = ANY (1, 2, 3), this won't work directly, because 1, 2, 3 is a syntactic construct, and not an expression. (Of course you could do = ANY ($2, $3, $4), but that only works if you know exactly how many values you have.)

But you can do it with arrays. The above is equivalent to = ANY(ARRAY[1, 2, 3]), and so you'd write

SELECT * FROM tbl_emp WHERE interest = $1 AND emp_id = ANY($2)

and the types of the parameters are, say, int and int[].

To call PQexecPrepared, you will need an array as string literal. Something like "{1, 2, 3}" (as a C string) will do. See the documentation for details.

Upvotes: 6

Related Questions