0

我想遍历模式并获得如下所示的结果集:

Count
5
834
345
34
984

但是,我无法让它使用动态 sql 返回任何内容……我已经尝试了所有方法,但 8.2 真的很痛苦。无论我输入什么返回类型,我都会收到此错误:

ERROR: ERROR: RETURN cannot have a parameter in function returning
set

这是我的功能:

CREATE OR REPLACE FUNCTION dwh.adam_test4()
 RETURNS void
 LANGUAGE plpgsql
AS $function$
DECLARE
   myschema text;
   rec RECORD;  
BEGIN 

FOR myschema IN select distinct c.table_schema, d.p_id
from information_schema.tables t inner join information_schema.columns c 
  on (t.table_schema = t.table_schema and t.table_name = c.table_name) 
  join dwh.sgmt_clients d on  c.table_schema  = lower(d.userid)
where c.table_name = 'fact_members' and c.column_name = 'debit_card'
  and t.table_schema NOT LIKE 'pg_%'
  and t.table_schema NOT IN ('information_schema', 'ad_delivery', 'dwh', 'users', 'wand', 'ttd') 
order by table_schema

LOOP
    EXECUTE 'select count(ucic) from '|| myschema || '.' ||'fact_members where debit_card = ''yes''' into rec;
    RETURN rec;
END LOOP;
END
$function$
4

1 回答 1

3

也许我误解了这个问题,但你不应该在返回记录时告诉 Postgres 你想返回记录吗?

在底部,您有:

RETURN rec;

在函数定义中你说:

RETURNS void

除此之外,您将返回 1 个结果(返回结束函数),因此无论如何它都不会返回第一个元素之外的任何内容。

我猜你需要这样的东西:

RETURNS SETOF record

而不是RETURN rec你需要:

RETURN NEXT rec

由于您想返回一个整数,因此不确定您是否真的想要或需要 aRECORD返回。

于 2013-10-29T16:04:26.743 回答