function - 如何将字符串数组传递给函数并在 SQL 的“IN”语句中使用它
问题描述
这可能很简单,但不知何故,我仍然不正确。如何将字符串数组传递给函数并在 SQL 的“IN”语句中使用它
CREATE OR REPLACE function test_function(array_para text)
RETURNS TABLE (data text)
AS $$
begin
return query select col1 from my_table where my_table.col2 in(array_para)
END
$$
LANGUAGE 'plpgsql';
我怎么称呼它?也许 -
select test_function('''ABC'', ''MITT''')
解决方案
您需要将参数声明为和数组,例如text[]
. 您也不需要 PL/pgSQL:
CREATE OR REPLACE function test_function(array_para text[])
RETURNS TABLE (data text)
AS $$
select col1
from my_table
where my_table.col2 = any (array_para)
$$
LANGUAGE sql;
然后调用它:
select *
from test_function(array['ABC', 'MITT']);
另一种选择是使用variadic
参数:
CREATE OR REPLACE function test_function(variadic array_para text[])
RETURNS TABLE (data text)
AS $$
select col1
from my_table
where my_table.col2 = any (array_para)
$$
LANGUAGE sql;
然后你可以调用它:
select *
from test_function('ABC', 'MITT', 'DEF');
推荐阅读
- react-native - React Native - 在滑动时隐藏组件
- postgresql - 较新的 postgres 版本中的 string_agg 函数
- php - 在 Woocommerce 订单页面中,创建与订单 ID 相关的自定义文本字段
- ansible - ansible playbook 从 /etc/resolv.conf 文件中读取名称服务器(DNS)
- javascript - 隐藏除选定 Autodesk-Forge 查看器之外的所有查看器
- tensorflow - 在everystep keras模型上打印输出
- javascript - 如何通过 NavDropdown.Item 链接传递 prop 以响应路由器组件
- python - Python 翻译器 pig latin - for 循环迭代
- docker - Docker 在单独的存储库中使用应用程序自动构建
- python - 服务器以 100 Hz 的采样率发送数据,客户端使用 Python 接收和处理时间约为 0.1s