0

我做了这个 plpgsql 函数,它没有返回任何东西!而如果我取出查询部分并在单独的 sql 窗口中执行它,它会返回正确的行。

我还认为该查询并不是真正的最佳选择,因此不胜感激(对 plpgsql 来说非常新)

CREATE OR REPLACE FUNCTION get_members(in_company_uuid uuid, in_start integer, in_limit integer, in_sort character varying, in_order character varying, OUT out_status integer, OUT out_status_description character varying, OUT out_value character varying[]) RETURNS SETOF record
LANGUAGE plpgsql
AS $$DECLARE

temp_record RECORD;
temp_out_value VARCHAR[];
temp_iterator INTEGER := 0;

BEGIN

FOR temp_record IN EXECUTE '
SELECT DISTINCT ON
(' || in_sort || ')
u.user_uuid,
u.firstname,
u.preposition,
u.lastname,
array(SELECT email FROM emails WHERE user_uuid = u.user_uuid) as emails,
array(SELECT mobilenumber FROM mobilenumbers WHERE user_uuid = u.user_uuid) as mobilenumbers,
array(SELECT c.name FROM targetgroupusers AS tgu LEFT JOIN membercategories as mc ON mc.targetgroup_uuid = tgu.targetgroup_uuid LEFT JOIN categories AS c ON mc.category_uuid = c.category_uuid WHERE tgu.user_uuid = u.user_uuid) as categories,
array(SELECT color FROM membercategories WHERE targetgroup_uuid IN(SELECT targetgroup_uuid FROM targetgroupusers WHERE user_uuid = u.user_uuid)) as colors
FROM
    membercategories AS mc
LEFT JOIN
    targetgroups AS tg
ON
    tg.targetgroup_uuid = mc.targetgroup_uuid
LEFT JOIN
    targetgroupusers AS tgu
ON
    tgu.targetgroup_uuid = tg.targetgroup_uuid
LEFT JOIN
    users AS u
ON
    u.user_uuid = tgu.user_uuid
WHERE
    mc.company_uuid = \'' || in_company_uuid || '\'
ORDER BY
   ' || in_sort || ' ' || in_order || '
OFFSET
    ' || in_start || '
LIMIT
    ' || in_limit

LOOP
  temp_out_value[temp_iterator] = ARRAY[temp_record.user_uuid::VARCHAR(36), temp_record.firstname::CHARACTER VARYING, temp_record.preposition::CHARACTER VARYING, temp_record.lastname::CHARACTER VARYING, temp_record.emails::CHARACTER VARYING, temp_record.mobilenumbers::CHARACTER VARYING, temp_record.categories::CHARACTER VARYING, temp_record.colors::CHARACTER VARYING];
  temp_iterator = temp_iterator+1;
END LOOP;

out_status := 0;
out_status_description := 'Members retrieved';
out_value := temp_out_value;
RETURN;

END$$;

非常感谢!

4

3 回答 3

1

您需要使用RETURN NEXT temp_record而不是“仅”RETURN 来返回完整的结果集。

这是手册中解释如何处理“RETURNS SETOF”函数的部分:

在这种情况下,要返回的各个项目由一系列 RETURN NEXT 或 RETURN QUERY 命令指定,然后使用不带参数的最终 RETURN 命令来指示函数已完成执行

于 2010-12-13T14:22:10.143 回答
0

你需要使用

CREATE OR REPLACE FUNCTION get_members(...) RETURNS record LANGUAGE plpgsql AS
...

即删除setof哪个是你的问题,因为 a_horse 提到

但请记住,你只得到这样的 1 行。如果您想要多行,可以按照以下示例取消嵌套该数组:

create or replace function unnest(anyarray) returns setof anyelement as $$
  select $1[i] from generate_series(array_lower($1,1), array_upper($1,1)) i;
$$ language'sql' immutable;

create function func(out text, out text[]) returns record language plpgsql as $$
begin
  $1='success';
  $2[1]='hello';
  $2[2]='there';
  $2[3]='Koen';
  return;
end;$$;

select * from unnest((select column2 from func()));

 unnest
--------
 hello
 there
 Koen

如果您使用的是 8.4 或更高版本,则无需unnest像我在这里所做的那样创建自己的函数。

于 2010-12-13T16:54:56.423 回答
0

不要使用 \ 转义,使用引号:

\'' || in_company_uuid || '\'

''' || in_company_uuid || '''

但是,我不会为此使用函数,只是一个普通的 SQL 查询。使用 EXPLAIN 查看它是如何执行的以及在哪里可以找到性能问题。这不是一段非常好的 SQL ......

附言。您的函数也容易受到 SQL 注入的影响,您将用户输入放入 SQL 中而没有任何转义。

于 2010-12-13T14:34:19.503 回答