可以将值列表传递给PostgreSQL中的预处理语句吗?

huangapple go评论57阅读模式
英文:

Is it possible to pass a list of value into prepared statement in Postgresql?

问题

我可以使用以下SQL来定义一个准备好的语句来查询数据记录:

prepare query_data(int) as
    select column1 from table1 where id = $1;

但是如果要支持将一个ID列表传递给准备好的语句,该怎么办?这个列表的长度是不确定的,可能包含100个ID,也可能包含5000个ID。

英文:

I can use below sql to define a prepared statement to query a data record:

prepare query_data(int) as
    select column1 from table1 where id = $1;

but what should I support a list of ids passed into the prepared statement? the list length is uncertain, it can contains of 100 ids or maybe 5000 ids.

答案1

得分: 2

使用数组:

PREPARE stmt(int[]) AS SELECT column1 FROM table1 WHERE id = ANY ($1);

EXECUTE stmt(ARRAY[1,2]);

英文:

Use an array:

PREPARE stmt(int[]) AS SELECT column1 FROM table1 WHERE id =ANY ($1);

EXECUTE stmt(ARRAY[1,2]);

huangapple
  • 本文由 发表于 2023年3月8日 17:34:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定