首页 文章

plpgsql:使用动态setof返回类型创建函数

提问于
浏览
0

我正在尝试创建一个返回动态setof数据类型的函数 . 数据类型都是预先创建的,可以作为第4个变量调用 .

CREATE OR REPLACE FUNCTION gethistoryrecord(text, text, text, text)
RETURNS setof $4 AS
$BODY$
declare
    r record;
begin
    for r in EXECUTE 'SELECT * FROM ' || $1 || ' where ref_id = ' || $2 || ' and create_date < ' || quote_literal($3) || '::timestamp and (archive_date is null or archive_date >= ' || quote_literal($3) || '::timestamp)' loop
        return next r;
    end loop;
    return;
end
$BODY$
  LANGUAGE plpgsql;

我会把这个函数称为

select * from gethistoryrecord('view_all_history','3540','2012-08-21 17:43:39.855852','holder_name')

是否有可能我不必声明输出,也没有得到错误

返回“记录”的函数需要列定义列表

1 回答

  • 0

    你可以指定输出记录,但它可能是你想要的 . PostgreSQL具有相对严格类型的系统,不可能编写过于通用的SRF函数 .

相关问题