如何将SQL查询的数组输出传入PostgreSQL(PL/pgSQL)函数?
我可以在SQL中执行以下操作,其中将用户ID数组传递到SQL查询的where子句中如何将SQL查询的数组输出传入PostgreSQL(PL/pgSQL)函数?,sql,postgresql,select,plpgsql,Sql,Postgresql,Select,Plpgsql,我可以在SQL中执行以下操作,其中将用户ID数组传递到SQL查询的where子句中 select * from users where id in (select user_id from profiles); 我想做同样的事情,但将数组传递到PostgreSQL PL/pgSQL函数中,如下所示。如何声明函数并在函数中使用数组 select * from users_function(select user_id from profiles); CREATE OR REPLACE FUNC
select * from users where id in (select user_id from profiles);
我想做同样的事情,但将数组传递到PostgreSQL PL/pgSQL函数中,如下所示。如何声明函数并在函数中使用数组
select * from users_function(select user_id from profiles);
CREATE OR REPLACE FUNCTION users_function(....)
RETURNS void AS
$BODY$
....
在函数中声明数组数据类型[],然后使用聚合函数array\u agg将select语句转换为数组
CREATE OR REPLACE FUNCTION users_function(myints integer[])
$$
BEGIN
-- you need to find the bounds with array_lower and array_upper
FOR i in array_lower(myints, 1) .. array_upper(myints, 1) LOOP
Raise Notice '%', myints[i]::integer;
END LOOP;
END;
$$
select * from users_function(array_agg((select user_id from profiles)));
我无法像上面所描述的那样获得nate c的array_agg方法。这是一个选项:
select * from test_array('{1,2}');
CREATE OR REPLACE FUNCTION test_array(user_ids integer[])
RETURNS void AS
$$
declare
begin
FOR i in array_lower(user_ids, 1) .. array_upper(user_ids, 1) LOOP
RAISE NOTICE '%', user_ids[i]::integer;
END LOOP;
end
$$
LANGUAGE plpgsql;
我正在运行PostgreSQL 8.4.4,并出现此错误。有什么想法吗?从用户限制2中选择数组\u agglesect id;错误:select处或附近出现语法错误添加另一组括号,如:select array\u aggselect id from users limit 2。忘记这一点-任何时候你在一个函数中有一个子查询,你都需要两个集合:一个用于函数,一个用于子查询。很抱歉一直打扰你,但这是我得到的:选择数组\u agglose id from users limit 2;错误:用作表达式的子查询返回多行