2
我需要一个简单的函数来返回动态的一组列。我发现在SO几个例子,并具有下列结束:查询结构与函数结果类型不匹配,返回表
CREATE or replace FUNCTION getColumn(_column1 text, _column2 text, _column3 text, _table text)
RETURNS TABLE(cmf1 text, cmf2 text, cmf3 text) AS $$
BEGIN
RETURN QUERY EXECUTE
'SELECT '
|| quote_ident(_column1)::text || ' as cmf1,'
|| quote_ident(_column2)::text || ' as cmf2,'
|| quote_ident(_column3)::text || ' as cmf3'
' FROM '
|| quote_ident(_table);
END;
$$ LANGUAGE plpgsql;
我需要这个功能,只为varchar /文本列工作,所以我创造了这个测试表:
create table test20130205 (
a text,
b text,
c varchar,
d text)
;
最后,我可以运行一些测试:
select * from getColumn('a','b','d','test20130205');
-- ok
select * from getColumn('a','b','c','test20130205');
-- error
ERROR: structure of query does not match function result type
DETAIL: Returned type character varying does not match expected type text in column 3.
CONTEXT: PL/pgSQL function getcolumn(text,text,text,text) line 3 at RETURN QUERY
好像类型列c(VARCHAR)铸造前检查 - 这似乎很奇怪,但我想我已经错过了一些东西。
我该如何解决我的功能?
(PostgreSQL的9.1)