我在Postgres中有一个返回setof复合类型的函数。返回时我只能使用return next;
但不能使用return query
命令,为什么会这样做?
CREATE TYPE return_type AS
(paramname character varying,
value character varying);
CREATE OR REPLACE FUNCTION test(i_param1 character varying, i_param2 character varying)
RETURNS SETOF return_type AS
--this works just fine returning two rows
r.paramname:='row1';
r.value:='myvalue1';
return next r;
r.paramname:='row1';
r.value:='myvalue1';
return next r;
return;
-- with this command I do not get a single row attached in the resultset
return query
select 'row1' as paraName,'myvalue1' as value
UNION ALL
select 'row2' as paraName,'myvalue2' as value;
return;
答案 0 :(得分:1)
这对我有用,即使在Postgres 8.4:
CREATE OR REPLACE FUNCTION test(i_param1 varchar, i_param2 varchar)
RETURNS SETOF return_type AS
$func$
BEGIN
RETURN QUERY
SELECT 'row1'::varchar, 'myvalue1'::varchar
UNION ALL
SELECT 'row2','myvalue2';
END
$func$ LANGUAGE plpgsql;
由于类型不匹配,您的原始文件应仅返回异常。您需要强制字符串文字到匹配类型。另一方面,列别名与此无关:在函数体外部不可见。