I want to create a PREPARED STATEMENT where inputs are some integers whose count is not known. The way I'm trying to do is
PREP开发者_开发技巧ARE prep_select_data AS SELECT ARRAY[$1, id], $1, * FROM tbl_data WHERE id = ANY($1);
The error I'm getting is:
ERROR: op ANY/ALL (array) requires array on right side
LINE 1: ...ELECT ARRAY[$1, id], $1, * FROM tbl_data WHERE id = ANY($1);
^
I'm not able to figure out the issue.
Please advise.
Regards,
MayankAs error says you need to specify your $1
as array (of integers):
-- DEALLOCATE prep_select_data;
PREPARE prep_select_data (int[]) AS
SELECT
$1 || ARRAY[id], $1, *
FROM
tbl_data
WHERE
id = ANY($1);
I changed ARRAY[$1, id]
to $1 || ARRAY[id]
, so result looks like:
EXECUTE prep_select_data ('{1, 2}');
?column? | ?column? | id | value
----------+----------+----+-------
{1,2,1} | {1,2} | 1 | aaa
{1,2,2} | {1,2} | 2 | bbb
It's because (from doc):
multidimensional arrays must have matching extents for each dimension. A mismatch causes an error report.
精彩评论