英文:
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]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论