开发者

Postgresql prepare statement with operator ANY

开发者 https://www.devze.com 2023-03-01 16:39 出处:网络
SELECT * FROM tbl_emp WHERE interest = $1 AND emp_id = ANY(?) Is the above statement correct to be used in function PQprepare?

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 nPara开发者_如何学编程ms and how would PQexecPrepared be called?

Regards, Mayank


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消